0
trigger LeadOwnerAssignment on Lead (before update,after insert) { 
    if(Trigger.isAfter)
    {       
        List<User> userobj=[Select id from User where Profile.Name='Identity User'];            
        Integer Lead_Number;           
        for(Lead newLeads:trigger.New)     
        {                  
            Lead_Number=integer.valueof(newLeads.Lead_Number__c);
            System.debug('LeadNo'+Lead_Number);
            if(math.mod(Lead_Number,2)==0)             
            {
                newLeads.OwnerId=userobj[0].Id;
                System.debug('New lead owner Id'+newLeads.OwnerId);
            }        
            else
            {
                newLeads.OwnerId=userobj[1].Id;
                System.debug('New Lead Owner Id'+newLeads.OwnerId);
            }              
            newLeadFormedObject.add(newLeads);
        }            
        update newLeadFormedObject;                                   
    }    
}
2

You cannot update context variables in after triggers, you will have to use before trigger - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables_considerations.htm

If you must need to use after trigger then you will have to fetch data using SOQL and then update its field values.

0

trigger LeadOwnerAssignment on Lead (before update,after insert) { if(Trigger.isAfter) {

             List<User> userobj=[Select id from User where Profile.Name='Identity User'];

                Integer Lead_Number;
                Map<id,Lead> leadMap=new Map<id,Lead>();
                List<Lead> newLeadListToUpdate=new List<Lead>();

                for(Lead leadObj :trigger.new)
                {
                    leadMap.put(leadObj.id,leadObj);
                }

                List<Lead> leadList=[select id,ownerid,name,Lead_Number__c from Lead where id in : leadMap.keySet()];

                for(Lead leadObj : leadList)
                {
                    Lead_Number=integer.valueof(leadObj.Lead_Number__c);
                    if(math.mod(Lead_Number,2)==0)             
                     {
                       leadObj.OwnerId=userobj[0].Id;
                       System.debug('New lead owner Id'+leadObj.Ownerid);
                     }

                     else
                     {
                      leadObj.OwnerId=userobj[1].Id;
                      System.debug('New Lead Owner Id'+leadObj.OwnerId);
                     }
                     newLeadListToUpdate.add(leadObj);
                } 
                update newLeadListToUpdate;               

            }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.