Sunday 16 November 2014

Trigger on after insert and update example.

Trigger Fieldupfdate on Employee1__c (after insert, after update) {
  public static Boolean bool = true;
 if(bool){
 bool = false;
  List<Employee1__c> listemp = new  List<Employee1__c> ();
  Set<Id> setid = new Set<id>();
  for(Employee1__c empp:trigger.new){
   setid.add(empp.id);
  }
  List<Employee1__c> listem = [Select id,lastname__c from Employee1__c where id In : Setid];
  for(Employee1__c emp:listem){
   if(emp.LastName__c==null){
    emp.Lastname__c = 'Sampath';
    listemp.add(emp);
   }
  } update listemp;
}
}

Saturday 15 November 2014

Trigger on before insert and update example.




trigger Fieldupfdate on Employee1__c (before insert,before update) {
 for(Employee1__c emp:Trigger.new){
  if(emp.LastName__c==null){
   emp.LastName__c = 'sampath';
  }
 }
}

Friday 14 November 2014

Trigger updation

trigger Fieldupdations on Opportunity_Margin_Template__c (after update) {
List<Opportunity_Margin_Template__c> templist = new List<Opportunity_Margin_Template__c>();
List<Opportunity_Template_Task__c> templatetasklist = new List<Opportunity_Template_Task__c>();
set<id> setid = new set<id>();
for(Opportunity_Margin_Template__c margin: Trigger.new)
{
 setid.add(margin.id);
}
List<Opportunity_Margin_Template__c> tasklist = [select Id,Name,Template_Save_as__c,FX_Rate_to_USD__c from Opportunity_Margin_Template__c 
                                                 where id in:setid];
templatetasklist =[select id, Cost_Hr__c,Opportunity_Margin_Template__c from   Opportunity_Template_Task__c where  Opportunity_Margin_Template__r.id In : setid];                                          
for(Opportunity_Margin_Template__c temp: tasklist)
{
 if(temp.FX_Rate_to_USD__c!=Trigger.oldmap.get(temp.Id).FX_Rate_to_USD__c)
 {
   templist.add(temp);
 }
}
update templatetasklist ;        
update templist;  
                                                                                   
                
}

Friday 7 November 2014

Difference between ISBLANK and ISNULL




  • ISNULL is used for the Numeric & Text field. 
  • ISBLANK is used for both the Text as well as the Number field.
ISBLANK
ISNULL
Determines if an expression has a value and returns TRUE if it does not. If it contains a value, this function returns FALSE.
Determines if an expression is null (blank) and returns TRUE if it is. If it contains a value, this function returns FALSE.
Text fields cannot be null. So use mainly for text fields.
Numeric fields cannot be blank. So use mainly for numeric fields.

Trigger for field update in Salesforce

The below Apex code is used to update a field(Comments__c) in Member__c Object,

where Marital__Status and Comments__c are fields n Member__c object.

Code:

trigger commentsUpdate on Member__c (before Insert,before update)
{
     for(Member__c member : Trigger.new) 
     {     
         if(member.Marital_Status__c != '')   
         {     
             member.Comments__c = member.Marital_Status__c;   
         }      
         else  
         {     
             member.Comments__c = 'No comments';   
         }      
     } 
}



trigger.isInsert is true when a new record is created and inserted.

trigger.isUpdate is true when an existing record is modified.

Thursday 6 November 2014

Salesforce Apex Code for Email Template | How to send

  1. trigger LeadBeforeUpdate on Lead (before update)
  2. {      
  3.  
  4. //Prefetch email Template id
  5. EmailTemplate et = [SELECT id FROM EmailTemplate WHERE Name = 'Lead_Rejected'];
  6.  
  7. //Grab the recipient as Contact
  8. Contact recipient = [SELECT id, firstname FROM Contact where id in :Trigger.newMap.keySet()];    
  9.  
  10. //Map the profile
  11. Map<Id, Profile> ProfileOwner = new Map<Id, Profile>([Select p.Id, p.Name from Profile p where Id =: userinfo.getProfileId()]);
  12.  
  13. //Lead Object Map
  14. Map<Id, Lead> LeadMap = new Map<Id,Lead>([Select Id, From Lead WHERE id in :Trigger.newMap.keySet()]);
  15.  
  16. for (lead myLead : Trigger.new)
  17.     {  
  18.        
  19.         Lead testLead = LeadMap.get(myLead.id);
  20.  
  21.         if(testLead.Status__c=='Approved') {
  22.            
  23.     /*---Mail to the Lead Owner **********/
  24.    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // new mail object
  25.  
  26.    //Depending on Schema your organization have
  27.    recipient.AccountId = opp.AccountId;  // receipient would be owner of Lead  
  28.     mail.setSenderDisplayName(ProfileOwner.get(userInfo.getUserId).Name);//Sender would be owner of Profile
  29.  
  30. //Notice : How I retrieve profile by passing value of current user id (using UserInfo Method)
  31.   mail.setTargetObjectId(recipient.id); //target to receipient
  32.   mail.setTemplateId(et.id);           // pull the email template through id
  33.   Messaging.sendEmail(new Messaging.Singleemailmessage[] {mail});  //send single email
  34.  
  35. } // end if
  36. } // end for

Salesforce Lead Duplication

  1. trigger leadDuplicatePreventer on Lead
  2.                                (before insert, before update) {
  3.  
  4.     Map<String, Lead> leadMap = new Map<String, Lead>();
  5.     for (Lead lead : System.Trigger.new) {
  6.                
  7.         // Make sure we don't treat an email address that  
  8.    
  9.         // isn't changing during an update as a duplicate.  
  10.    
  11.         if ((lead.Email != null) &&
  12.                 (System.Trigger.isInsert ||
  13.                 (lead.Email !=
  14.                     System.Trigger.oldMap.get(lead.Id).Email))) {
  15.                
  16.             // Make sure another new lead isn't also a duplicate  
  17.    
  18.             if (leadMap.containsKey(lead.Email)) {
  19.                 lead.Email.addError('Another new lead has the '
  20.                                     + 'same email address.');
  21.             } else {
  22.                 leadMap.put(lead.Email, lead);
  23.             }
  24.        }
  25.     }