Friday, May 24, 2013

Thursday, May 23, 2013

NO_MASS_MAIL_PERMISSION, Single email is not enabled for your organization or profile issue in salesforce


This issue will generally come from spring13 release, when you refreshed a full sandbox and trying to send e-mails via Apex this may lead you to an issue saying that
"NO_MASS_MAIL_PERMISSION, Single email is not enabled for your organization or profile. Single email must be enabled for you to use this feature". 

To get rid of this issue 

go to Administer > Email Administration > Deliverability and then you will have options to select like No access,System email only, All email. select All email from the list. 



• No access: Prevents all outbound email to and from users.

• System email only: Allows only automatically generated emails, such as new user and password reset emails.

• All email: Allows all types of outbound email. Default for new, non-sandbox organizations.







Monday, May 13, 2013

You have uncommitted work pending. Please commit or rollback before calling out issue workaround in salesforce




Sometimes we need to create a record and then update it with information provided by a Web Service. However,a Web Service Callout may not occur after a DML statement within the same transaction.To achieve the required action, the transaction must be separated into two parts so that the DML transaction is completed before the Web Service Callout occurs and one workaround like below.


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<apex:page controller="WsCalloutTest" tabstyle="Account">
    <apex:form >
        <apex:actionFunction action="{!firstcall}" name="firstcall" Rerender="statuses" status="Status1" oncomplete="WebServiceCall();"/>
        <apex:actionFunction action="{!WebServiceCall}" name="WebServiceCall" status="Status2" reRender="statuses, msg"/>
        <apex:outputPanel id="statuses">
            <apex:actionStatus id="Status1" startText="...Inserting Record Into DB..." />
            <apex:actionStatus id="Status2" startText="...Calling Web Service..." />
        </apex:outputPanel>
        <apex:outputPanel id="msg">
            <apex:pageMessages />
        </apex:outputPanel>
        <div><input name="DoAction" class="btn" type="button" value="Do Action" onclick="firstcall();return false;"/></div>
    </apex:form>
</apex:page>



1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class WsCalloutTest{
    
    Contact myContact;
   
    public PageReference firstcall() {
        myContact = new Contact(name = 'Test Contact');
        insert myContact;
        return null;
    }
    
    public PageReference WebServiceCall() {
        
        // Execute a call to a Web Service
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://yourserviceurl.com?id=' + myContact.Id);
        req.setMethod('GET');
        HttpResponse response = new Http().send(req);
        myContact.Name = 'Test Contact 2';
        update myContact;
  //if the update is successfull
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'WebService Called on New Contact: ' + myContact.Name));
        return null;
    }
}

Thursday, May 2, 2013

Number to Word conversion using Salesforce Apex


1:  public with sharing class NumberToWord {  
2:      static String[] to_19 = new string[]{ 'zero', 'one',  'two', 'three', 'four',  'five',  'six',  
3:      'seven', 'eight', 'nine', 'ten',  'eleven', 'twelve', 'thirteen',  
4:      'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' };  
5:    static String[] tens = new string[]{ 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'};  
6:    static String[] denom = new string[]{ '',  
7:      'thousand',   'million',     'billion',    'trillion',    'quadrillion',  
8:      'quintillion', 's!xtillion',   'septillion',  'octillion',   'nonillion',  
9:      'decillion',  'undecillion',   'duodecillion', 'tredecillion',  'quattuordecillion',  
10:      's!xdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion' };  
11:    // convert a value < 100 to English.    
12:   public static String convert_nn(integer val) {  
13:      if (val < 20)  
14:        return to_19[val];  
15:      if(val == 100)  
16:          return 'One Hundred';  
17:      for (integer v = 0; v < tens.size(); v++) {  
18:        String dcap = tens[v];  
19:        integer dval = 20 + 10 * v;  
20:        if (dval + 10 > val) {  
21:          if (Math.Mod(val,10) != 0)  
22:            return dcap + ' ' + to_19[Math.Mod(val,10)];  
23:          return dcap;  
24:        }      
25:      }  
26:      return 'Should never get here, less than 100 failure';  
27:    }  
28:    // convert a value < 1000 to english, special cased because it is the level that kicks   
29:    // off the < 100 special case. The rest are more general. This also allows you to  
30:    // get strings in the form of "forty-five hundred" if called directly.  
31:    public static String convert_nnn(integer val) {  
32:      String word = '';  
33:      integer rem = val / 100;  
34:      integer mod = Math.mod(val,100);  
35:      if (rem > 0) {  
36:        word = to_19[rem] + ' hundred';  
37:        if (mod > 0) {  
38:          word += ' ';  
39:        }  
40:      }  
41:      if (mod > 0) {  
42:        word += convert_nn(mod);  
43:      }  
44:      return word;  
45:    }  
46:    public static String english_number(long val) {  
47:      if (val < 100) {  
48:        return convert_nn(val.intValue());  
49:      }  
50:      if (val < 1000) {  
51:        return convert_nnn(val.intValue());  
52:      }  
53:      for (integer v = 0; v < denom.size(); v++) {  
54:        integer didx = v - 1;  
55:        integer dval = (integer)Math.pow(1000, v);  
56:        if (dval > val) {  
57:          integer mod = (integer)Math.pow(1000, didx);  
58:          integer l = (integer) val / mod;  
59:          integer r = (integer) val - (l * mod);  
60:          String ret = convert_nnn(l) + ' ' + denom[didx];  
61:          if (r > 0) {  
62:            ret += ', ' + english_number(r);  
63:          }  
64:          return ret;  
65:        }  
66:      }  
67:      return 'Should never get here, bottomed out in english_number';  
68:    }  
69:  }  
This will work for integer range values