+ Start a Discussion
Abi V 4Abi V 4 

how to check the old lead status with new lead lead status in apex class

public with sharing class SendNamerFinacialSdlGroup{
i have the method where i will check the lead (lead records related to group members) owners and status is not changed with in 24 hoursthen  i will update the checkbox__x to true.

Currently i have written the code until fetching the leads related to particulr public group of test_Team.Please suggest me how to check the existed old status with new lead status in apex class to process furthur.
 public static void updateNamercheckbox(List<Lead> allleadlist){
       set<id> useridset=new set<id>();
       set<id> leadid=new set<id>();
       
       for(Lead led:allleadlist){
       leadid.add(led.ownerId);
       }
        List<Group> pbgrpid = [select id from Group where type='Regular' AND DeveloperName='test_Team'];
        List<GroupMember> userids = [select UserOrGroupId,GroupId  from GroupMember where GroupId IN:pbgrpid];
          for(GroupMember gmid:userids){
            useridset.add(gmid.UserOrGroupId);
            }
          system.debug('useridset' +leadid);
    
}
}
GauravTrivediGauravTrivedi
Hi Abi,

i changed your code a bit to get old values, in the argument you need to pass trigger.oldMap and your work is done:
public static void updateNamercheckbox(List<Lead> allleadlist, Map<id,Lead> oldLeadMap){
	set<id> useridset=new set<id>();
	set<id> leadid=new set<id>();

	for(Lead led:allleadlist){
		if(led.Status != oldLeadMap.get(led.Id).Status){
			//if your lead status is changed you can put logic to work on your different actions
		}
			
		leadid.add(led.ownerId);
	}
	List<Group> pbgrpid = [select id from Group where type='Regular' AND DeveloperName='test_Team'];
	List<GroupMember> userids = [select UserOrGroupId,GroupId  from GroupMember where GroupId IN:pbgrpid];
	for(GroupMember gmid:userids){
		useridset.add(gmid.UserOrGroupId);
	}
	system.debug('useridset' +leadid);

}

Please let me know if it works for you.
~Onkar~Onkar
Did you try timebased workflow with field update action. I dont think you need to write code for this. This is possible by timebased workflow.
Abi V 4Abi V 4
Hi gaurav,

i have changed the logic and working fine now.
now i want to send email when Checkbox__c false in workflow and default value is unchecked.But again here issue is timedependent workflow will run and consider all the leads where Checkbox__c false.so any idea how to fix that i want to raise this workflow only for test_team group leads.
only thing i can do is hardcode the users names of group and put in rule criteria.

 public static void updateNamercheckbox(List<Lead> allleadlist,Map<id,Lead> oldLeadMap){
    set<id> useridset=new set<id>(); 
    List<Group> pbgrpid = [select id from Group where type='Regular' AND DeveloperName='test_Team'];
    List<GroupMember> userids = [select UserOrGroupId,GroupId  from GroupMember where GroupId IN:pbgrpid];
    
        for(GroupMember gmid:userids){
            useridset.add(gmid.UserOrGroupId);
        }
      
      for(Lead led:allleadlist){
       if((useridset.contains(oldLeadMap.get(led.id).OwnerId))&&((led.Status != oldLeadMap.get(led.Id).Status)||(led.OwnerId!= oldLeadMap.get(led.Id).OwnerId))){
        Checkbox__c=TRUE;
       }
       else{
      Checkbox__c=FALSE;
       }

    }    
}
 
Abi V 4Abi V 4
hi onkar,
i have tried but the issue is with send notificstions to gropu of users.
~Onkar~Onkar
Hi Abi,

Is your Group is dynamic or fix. I am adding snapshot from Salesforce. Salesforce support send email group of user. (Team, Group, Role) based on Object


User-added image
~Thanks
Onkar Kumar