Deleting Any Object You Don’t Own

Each new Salesforce release has me revisiting previous solutions and looking at how they can be improved. One little tidbit that was provided in Winter ’13 is a way to get the SObject type from the Id. In the past, you’d have to do quite a bit of code to determine the type of SObject from the Id. Now all we need to do to get the SObject type from an ID is:

id.getSObjectType();

This got me thinking that I could rewrite my Contact deletion webservice and have it be more generic to delete any object I don’t own.

I call this SuperDelete. It is almost identical to my Contact deletion class except now it checks to see what the object type is first and instantiates a new object with that ID so it can be deleted. It then checks to see what access level the requesting user has. In this case, the user needs to have at least Edit Access.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
global without sharing class SuperDelete {
  webService static String deleteRecord(Id id) {
    sObject so = id.getSObjectType().newSObject(id=id);
      try {
        UserRecordAccess ura = [select RecordId, HasEditAccess from UserRecordAccess where UserId = :UserInfo.getUserId() and RecordId = :id];
        if (ura.HasEditAccess)
          delete so;
        else
          throw new deleteException('Error deleting');
        return '';
      } catch (Exception e) {
        return e.getMessage();
      }
   }
}

This webservice can be invoked from a custom JavaScript button that needs to be created on each object you want to use this on:

1
2
3
4
5
6
7
8
9
10
11
12
{!REQUIRESCRIPT('/soap/ajax/22.0/connection.js')}
{!REQUIRESCRIPT('/soap/ajax/22.0/apex.js')}
 
if (confirm('Are you sure?')) {
  var result = sforce.apex.execute('SuperDelete','deleteRecord', {id:'{!Contact.Id}'});
  var errorDiv = document.getElementById('errorDiv_ep');
  if (result != '') {
    errorDiv.innerHTML = result;
    errorDiv.style.display = 'block';
  }
    else window.location.replace('/003/o');
}

Visualforce Charting – Another Way to Visualize Data

Over a year ago, I posted about how to display charts using Google charts. It is about time I revisited this topic using the new feature made generally available in the Winter ’13 release. The cool thing about using Visualforce charting is that it is all native to the platform and hopefully you won’t have to deal with JavaScript troubleshooting.

Let’s set up the scenario again: we want to display a chart on the account page layout to visualize some of the data in a chart. Let’s say we have some custom fields showing annual revenue. There is a field for 2008, 2009, 2010, etc.

What we need to do first is use a little Javascript to build a data array that will be used by the chart. In our case, the array will have three elements: the data value, the label and the name. After we create the data array, we can us it in a Visualforce chart. There are lots of options and parameters for charts, check out the documentation for details there.

Here’s the Visualforce page:

<apex:page standardController='Account';>
<script>
  //use this function to format the hover text
  formatCurrencyLabel = function ( value )
  {
    return '$' + String(value);
  }
 
  // Build the chart data array in JavaScript
  var dataArray = new Array();
  dataArray.push({'data':{!Account.Revenue_2008__c},'label':formatCurrencyLabel({!Account.Revenue_2008__c}),'name':'2008'});
  dataArray.push({'data':{!Account.Revenue_2009__c},'label':formatCurrencyLabel({!Account.Revenue_2009__c}),'name':'2009'});
  dataArray.push({'data':{!Account.Revenue_2010__c},'label':formatCurrencyLabel({!Account.Revenue_2010__c}),'name':'2010'});
  dataArray.push({'data':{!Account.Revenue_2011__c},'label':formatCurrencyLabel({!Account.Revenue_2011__c}),'name':'2011'});
</script>;
 
  <apex:chart data='dataArray' height='200' width='300&'>
    <apex:axis type='Numeric' position='left' fields='data' title='Revenue' grid='true' minimum='0'/>
    <apex:axis type='Category' position='bottom' fields='name' title='Year'/>
    <apex:barSeries title='Annual Sales' orientation='vertical' axis='left' xField='name' yField='data'>
      <apex:chartTips height='20'; width='100'; valueField='label'/>
    </apex:barSeries>
  </apex:chart>
</apex:page>

We can then add this to our page layout and it should look like so: