Salesforce

An effort to bring a revolution in Salesforce.

Friday, 25 September 2015

Recursive trigger & class in salesforce

 

Recursive trigger & class  



public Class checkRecursive{
      private static boolean check = true;
      public static boolean checkOneTime(){
             if(check){
                    check = false;
                    return true;
             }
             else{
                    return check;
             }
      }
}


 Trigger for update Account from Contact 


Description : i have to create one check Box field called Is_Main__c in contact if it will checked then it's Name  copy to Account's  field called Account Site  and update Account and also uncheck  other related contact list's  filed called Is_Main__c .  only one contact par Account have checked that field  and update contact.


trigger test on Contact (after update) {
    List<Contact> cont = new List<Contact>();
    Contact con = Trigger.new[0];
    Contact conOld;
    Account acc = new Account();
     if(Trigger.isUpdate && checkRecursive.checkOneTime()){
      conOld = Trigger.old[0];
           if(con.Is_Main__c != false){
           
                acc = [select id,Site from Account where id =:con.AccountId];
                acc.site= con.FirstName +' '+ con.LastName;
                update acc;
            }
           
            cont = [select id,Name,accountId,Is_Main__c from Contact where accountid = :acc.id];
            for(Contact c :cont){
              
                if(c.Is_Main__c != False && c.Id != con.Id)
                   c.Is_Main__c = False;
                  
            }

            update cont;
           
            List<Contact> contactCount = new List<Contact>();
            contactCount = [select id,Name,accountId,Is_Main__c from Contact where accountid = : con.AccountId and Is_Main__c = true];
         
            if(conOld.Is_Main__c == true && con.Is_Main__c == false){
                con.addError('There must be at least one main contact with account.');
            } 
      }
   
   }
,

Salesforce !! which record is read-only trigger?

Which record is read-only trigger Salesforce.

"execution of AfterUpdate caused by: System.FinalException: Record is read-only


This kind of error occurs if you try to update lists/record which are/is read only in the trigger execution. For example, trigger.new and trigger.old are both read only lists and cannot be applied with a DML operation.

Say if you write Update trigger.new; in your trigger you will get the above mentioned error.
A field value of object can be changed using trigger.new but only in case of before triggers. But in case of after triggers changing the values of the records in trigger.new context will throw exception as "Record is read only"

Examples:

trigger mytrigger on account(before insert,before update){
  for(account ac:trigger.new){
      ac.name ='abc';
  }
}

Above code will update the names of the records present in trigger.new list. But, the below code will throw run time exception "Record is read only".

trigger mytrigger on account(after insert,after update){
  for(account ac:trigger.new){
      ac.name ='abc';
  }
}

Also trigger.old will always be read only no matter where you use it either before trigger or after trigger.
That is both the below codes will throw run time exception as trigger.old is read only

trigger mytrigger on account(after insert,after update){
  for(account ac:trigger.old){
      ac.name ='abc';
  }
}
trigger mytrigger on account(before insert,before update){
  for(account ac:trigger.old){
      ac.name ='abc';
  }
}

Notes:
1. Trigger.new and trigger.old are read only
2. An object can change its own field values only in before trigger: trigger.new
3. In all cases other than mentioned in point 2; fields values cannot be changed in trigger.new and would cause run time exception "record is read only"
,

Salesforce trigger for Convert Lead Owner Queue to Logged in User !!

Salesforce trigger  for Convert Lead Owner Queue to Logged in User


trigger leadConvertToLoggedInUser on Lead (before update) {
   User u = [select Id, Name from User where Id =: UserInfo.getUserId()];

   for(Lead Ld : Trigger.New){  
           String leadOwner = Ld.OwnerId;
           if(leadOwner.startsWith('00G')){
               Ld.OwnerId = u.Id;
           }
   }

}
,

Get Gmail, Docs, Drive, and Calendar for business