Sunday, February 28, 2016

How To Call Apex class form JAVA Script button.

Java Script Button:

{!REQUIRESCRIPT(“/soap/ajax/24.0/connection.js”)}
{!REQUIRESCRIPT(“/soap/ajax/24.0/apex.js”)}
try{
var accountId='{!Account.Id}’;
var result = sforce.apex.execute(“UpdateAccount”, “updateEnable”,{accId: accountId});
                                                   {Controller}            {Method}
location.reload();
}
catch(err) {
txt=”There was an error on this page.\n\n”;
txt+=”Error description: ” + err.description + “\n\n”;
txt+=”Click OK to continue.\n\n”;
alert(txt);
}

======================================
Controller class start with global when you calling class from Javascript button.


global class UpdateAccount {
webService static string updateEnable(List<Id> accId){
try{
System.debug(‘Id–>’+accId);
if(accId != NULL && accId.size() > 0)
{
Account accUpdate = [SELECT Id, Enable__c from Account where Id =:accId];
accUpdate.Enable__c = ‘true’;
update accUpdate;
return ‘Updated successfully’;
}
}
catch(exception e){}
return null;
}
}

How to redirect the visualforce page based on the mobile user or browser user in salesforce

Visualforce page:
<apex:page action=”{!checkingAgent}” controller=”agentController”>
</apex:page>


Controller.

public class agentController {
public agentController() {
}
public PageReference checkingAgent() {
String userAgent = ApexPages.currentPage().getHeaders().get(‘User-Agent’);
if (userAgent.contains(‘iPhone’)) {
PageReference pr = Page.iPhoneFriendlyPage;
pr.setRedirect(true);
return pr;
} else if (userAgent.contains(‘iPad’)) {
PageReference pr = Page.iPadFriendlyPage;
pr.setRedirect(true);
return pr;
} else if (userAgent.contains(‘Salesforce’)) {
PageReference pr = Page.SalesforceFriendlyPage;
pr.setRedirect(true);
return pr;
} else if (userAgent.contains(‘BlackBerry’)) {
PageReference pr = Page.BlackBerryFriendlyPage;
pr.setRedirect(true);
return pr;
} else {
PageReference pr = Page.NormalFriendlyPage;
pr.setRedirect(true);
return pr;
}
return null;
}
}

======================
and please make sure to add the following meta tag in your visualforce page:
<apex:page showheader=”false”>
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<title>Visualforce in Mobile View</title>
    <meta name=”viewport” content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;” />
</head>
<body>
<h2>Visualforce in Mobile View:</h2>
</body>
</html>
</apex:page>

Friday, February 19, 2016

Tips for Salesforce

1.Email Template.

      When Record is meet the Criteria  send mail to User/Group/Role.

      By adding below one in email you can send link of the Record to User.

       Account Link :{!Account.Link}.

2. User utility class.


  public static user gtUser()
    {
    
        userProfile=[select id , profile.name from user where id=: userinfo.getuserid()];
        return userProfile;
    }
 
 

 

 

Triggers:How to avoid execution query multiple time in single transction.

1.Create Query Utility class.
  
public class Query_Util{
       public static List<Product2> prdList ;

  
     public static List<Product2> getProducts(){
       prdList  = New List<Product2>();
       prdList = [select id,Name,PRODUCT_CODE__c from Product2 where PRODUCT_CODE__c =111];
       return prdList ;
    }  


}

2.how to user above query in Trigger()

trigger queryUtility on Opportunity (before insert, before update) 
{
      List<Product2> prd;
        if(query_util.prdList ==null)  // Checking query_util.prdList is null for first time 
        {
            prd = Query_Util.getProducts();
        }
        else
        {
            prd = query_util.prdList;   //Using Existing Product list returned from query Util
            
        }
   if(Trigger.isInsert){
          for(Opportunity opp:Trigger.New){
              if(prd != null && prd.size() > 0 && opp.Name == '111' && opp.Product_Name__c == prd[0].id && System.Today() > ncd.Loan_Date__c){
                 system.debug('&&&&&&&&&'+opp.Product_Name__c);
                 opp.Name.addError('111 Opportunity cannot be created');
               }
        
             }
        }
        if(Trigger.isUpdate){
          for(Opportunity opp:Trigger.New){
              if(prd != null && prd.size() > 0 && opp.Name == '111' && opp.Product_Name__c == prd[0].id && opp.CreatedDate.Date() > ncd.Loan_Date__c){
                 system.debug('&&&&&&&&&'+opp.Product_Name__c);
                 opp.Name.addError('111 Opportunity cannot be created');
               }
        
             }
        }
}  

How to Overide New Button based upon Profile it will navigate custome/Standard.

1.Create Visual force page with standard controller.
    
<apex:page standardController="Account" extensions="Accountnew" >
</apex:page> 
 
2.Edit the "NEW" button,Select Visualforce page and choose your vf page and save the button.
 
3.Extension class
 
public  class Accountnew{
    private ApexPages.StandardController controller;
    public String retURL {get; set;}
    public String saveNewURL {get; set;}
    public String rType {get; set;}
    public String cancelURL {get; set;}
    public String ent {get; set;}
    public String Profilename  {get; set;}
    public String confirmationToken {get; set;}
    public ID DEOProfileID;
    public ID BICProfileID;
    public Id branchOAC;
    public Accountnew(ApexPages.
StandardController controller) {
        this.controller = controller;
        retURL = ApexPages.currentPage().getParameters().get('retURL');
        rType = ApexPages.currentPage().getParameters().get('RecordType');
        Profilename = userinfo.getProfileId();
        DEOProfileID = [Select id,name from Profile where  Name =:'BIC' limit 1].id;
        BICProfileID = [Select id,name from Profile where  Name =:'DEO' limit 1].id;
        branchOAC=[Select id,Name FROM Profile where  Name ='Branch OAC' limit 1].id;
        cancelURL = ApexPages.currentPage().getParameters().get('cancelURL');
        ent = ApexPages.currentPage().getParameters().get('ent');
        confirmationToken = ApexPages.currentPage().getParameters().get('_CONFIRMATIONTOKEN');
        saveNewURL = ApexPages.currentPage().getParameters().get('save_new_url');
    }
    public PageReference redirect() {
        PageReference returnURL;
        // Redirect if Record Type corresponds to custom VisualForce page
        If(Profilename == DEOProfileID || Profilename == BICProfileID) {
            returnURL = new PageReference('/apex/SelectMeetingCenter');
         }else if (Profilename == branchOAC){
          returnURL = new PageReference('/apex/JKApplicationTagging');
        }else {
           returnURL = new PageReference('/a0Y/e');
        }
        returnURL.getParameters().put('retURL', retURL);
        returnURL.getParameters().put('RecordType', rType);
        returnURL.getParameters().put('cancelURL', cancelURL);
        returnURL.getParameters().put('ent', ent);
        returnURL.getParameters().put('_CONFIRMATIONTOKEN', confirmationToken);
        returnURL.getParameters().put('save_new_url', saveNewURL);
        returnURL.getParameters().put('nooverride', '1');
        returnURL.setRedirect(true);
        return returnURL;
        
       
    }
}
 =============================================================
Test class.
  Apexpages.StandardController sc = new Apexpages.StandardController(appobj); 
        Accountnew controller = new Accountnew(sc);
        controller.redirect();