Friday, September 27, 2013

Direct download an attachment from a link in Visualforce page

In many cases we need to download a file instead of opening the same.

 A simple HTML5 trick to do the same like below



{!attachment.name}

(this will simply open the file)

 


here main attributes are we should have docType="html-5.0" and download="{!attachment.name}" for the respective components as highlighted above...


Here I used the standard controller you can construct the same using controllers/Extensions to achive the same.



Happy coding...........

Wednesday, August 21, 2013

Drop-down button in a Visualforce Page

creating a drop-down button in a vf page..this is simple hack using standard salesforce styles

Code:
  <apex:page>
  <div class="menuButton" id="MyMenu">
    <div class="menuButtonButton" id="MyMenuButton">
       <span id="menuLabel" tabindex="0" style="">MyMenu</span>
    </div>
    <div class="menuButtonMenu" id="MyMenuMenu">
      <a href="/" class="menuButtonMenuLink firstMenuItem">Left VoiceMail</a>
      <a href="/" class="menuButtonMenuLink">Call in 1 day</a>
      <a href="/" class="menuButtonMenuLink">Call in 3 day</a>
      <a href="/" class="menuButtonMenuLink">Call in 5 day</a>
    </div>
  </div>
<script type="text/javascript">new MenuButton('MyMenu', false);</script>
  
</apex:page>

you can associate the actions by replacing the anchor tags with commandlinks or use ajax


Happy coding.....

Friday, August 2, 2013

Standard Salesforce DatePicker not working?

Some times Visualforce page with rerender sections, that standard DatePicker field based on Date or DateTime inputField tag was not working?

This was happend to me recently when I am developing some vf page. For this, a small hack that we can use is

if you have any field which is date or datetime, try to create the block which executes on every load with out and conditional renderings . This will allow to initialize the script and works for our custom inputField in vf page..

<div style=”display:none”>
    <apex:inputField value=“{!Account.Date_field__c}”/>
</div>


if you don't have any field which is date or datetime on the standard controller then create a property for the extension if you have any(if not you may need to created a date field on the standard controlller..but this doesn't make sense...in order to cover an issue we are not supposed to create  field..) which contains a set up object date or datetime field and make use of the same. like

public opportunity oppObj {get;set;}

<apex:inputField value="{!oppObj.closeddate}" rendered="false"/>

Hope this will help those we stuck with this kind of issue...Thank you.

Friday, July 5, 2013

Setting Id Fields on sObjects for Updates


Starting with Apex code saved using Salesforce.com API version 27.0, the Id field is now writeable

         list<Account> lstA = [select id,name from account limit 1];
Account a1 = new Account();
        a1.id = lstA[0].id;
a1.name = 'update through ID';
update a1;

but attempting to insert these sObject instances results in an error.

Monday, June 17, 2013

Extend the default Number object with a formatMoney() method

// usage: someVar.formatMoney(decimalPlaces, symbol, thousandsSeparator, decimalSeparator)
        // defaults: (2, "$", ",", ".")
        Number.prototype.formatMoney = function(places, symbol, thousand, decimal) {
            places = !isNaN(places = Math.abs(places)) ? places : 2;
            symbol = symbol !== undefined ? symbol : "$";
            thousand = thousand || ",";
            decimal = decimal || ".";
            var number = this,
                negative = number < 0 ? "-" : "",
                i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
                j = (j = i.length) > 3 ? j % 3 : 0;
            return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
        };
        var n = new Number(12000);
        document.getElementById('{!$Component.lblMoney}').value=n.formatMoney(2, "$", ",", ".");

Some useful links:  http://josscrowcroft.github.io/accounting.js/




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

Wednesday, April 24, 2013