function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Elliot32Elliot32 

Restrict APEX Trigger based on Lead Owner Profile

Hi All,

Relative newbie to Apex. I am looking to create a trigger that will create a Record of a custom Object I have created when a Lead Status is changed to 'No'. I would only like this Trigger to apply to Leads owned by Users with a certain Profile. Please see below:

The Profile restriction that I used doesn't seem to work and I have been spinning my tires and Google-ing around to no avail. Does anyone have any insight? Thanks.
trigger EPS_LeadCycleCreator on Lead (before update) {
    List<Lead_Cycle__c> LeadCycles = new List<Lead_Cycle__c>();
    
    for (Lead l : trigger.new) {
        Lead oldLead = trigger.oldMap.get(l.Id);
        
        Boolean oldLeadIsNo = oldLead.Status.equals('No');
        Boolean newLeadIsNo = l.Status.equals('No');
        
        if (l.Owner.ProfileId == '00e60000000rNmn' && (!oldLeadIsNo && newLeadIsNo)) {
            Lead_Cycle__c LeadCycle = new Lead_Cycle__c ();
            LeadCycle.Lead__c = l.id;
            LeadCycle.Cycle_Start_Date__c = l.LastTransferDate;
            LeadCycle.Cycle_End_Date__c = system.today();
            LeadCycle.OwnerId = '00560000002VLHw';
        
            LeadCycles.add(LeadCycle);
        }
    
    }
    
    insert LeadCycles;
}

 
Best Answer chosen by Elliot32
Neetu_BansalNeetu_Bansal
Hi Elliot,

In trigger, you will not be able to get reference field, you need to query for that like in your case l.Owner.ProfileId will always be null. Also, you want to insert new record, on updating lead, so it is recommended to use after update trigger
Also, it is not preferable to use hard coded ids, you can change code at line 31, using the profile name comparison like
ownerMap.get( l.OwnerId ).Profile.Name == 'Your Profile Name'
Use your profile name instead of 'Your Profile Name'.

You can use below code:
trigger EPS_LeadCycleCreator on Lead( after update )
{
    List<Lead_Cycle__c> LeadCycles = new List<Lead_Cycle__c>();
    
	Set<Id> ownerIDs = new Set<Id>();
	for( Lead l : trigger.new )
	{
        Lead oldLead = trigger.oldMap.get(l.Id);
        
        Boolean oldLeadIsNo = oldLead.Status.equals('No');
        Boolean newLeadIsNo = l.Status.equals('No');
        
        if( !oldLeadIsNo && newLeadIsNo )
		{
            ownerIDs.add( l.OwnerId );
        }
    
    }
	
	Map<Id, User> ownerMap = new Map<Id, User>([ Select Id, ProfileId, Profile.Name From User where Id IN: ownerIDs ]);
	
    for( Lead l : trigger.new )
	{
        Lead oldLead = trigger.oldMap.get(l.Id);
        
        Boolean oldLeadIsNo = oldLead.Status.equals('No');
        Boolean newLeadIsNo = l.Status.equals('No');
        
        if( !oldLeadIsNo && newLeadIsNo )
		{
			if( ownerMap.containsKey( l.OwnerId ) && ownerMap.get( l.OwnerId ).ProfileId == '00e60000000rNmn' )
			{
				Lead_Cycle__c LeadCycle = new Lead_Cycle__c();
				LeadCycle.Lead__c = l.id;
				LeadCycle.Cycle_Start_Date__c = l.LastTransferDate;
				LeadCycle.Cycle_End_Date__c = system.today();
				LeadCycle.OwnerId = '00560000002VLHw';
			
				LeadCycles.add(LeadCycle);
			}
        }
    
    }   
    insert LeadCycles;
}
Let me know, if you need any other help.

Thanks,
Neetu
 

All Answers

shashi lad 4shashi lad 4
Using hardcoded ID is certainly not a best practice. I would try to change the if condition using profile name.
 if (l.Owner.Profile.Name == '<Profile Name>' && (!oldLeadIsNo && newLeadIsNo)) {

Also using System.debug statement check the log to make sure it is producing desired output for other conditions.

Hope this helps.

thanks
shashi
shashi lad 4shashi lad 4
Do you also want to think about using "after update"  instead of "before update" may be ...
Neetu_BansalNeetu_Bansal
Hi Elliot,

In trigger, you will not be able to get reference field, you need to query for that like in your case l.Owner.ProfileId will always be null. Also, you want to insert new record, on updating lead, so it is recommended to use after update trigger
Also, it is not preferable to use hard coded ids, you can change code at line 31, using the profile name comparison like
ownerMap.get( l.OwnerId ).Profile.Name == 'Your Profile Name'
Use your profile name instead of 'Your Profile Name'.

You can use below code:
trigger EPS_LeadCycleCreator on Lead( after update )
{
    List<Lead_Cycle__c> LeadCycles = new List<Lead_Cycle__c>();
    
	Set<Id> ownerIDs = new Set<Id>();
	for( Lead l : trigger.new )
	{
        Lead oldLead = trigger.oldMap.get(l.Id);
        
        Boolean oldLeadIsNo = oldLead.Status.equals('No');
        Boolean newLeadIsNo = l.Status.equals('No');
        
        if( !oldLeadIsNo && newLeadIsNo )
		{
            ownerIDs.add( l.OwnerId );
        }
    
    }
	
	Map<Id, User> ownerMap = new Map<Id, User>([ Select Id, ProfileId, Profile.Name From User where Id IN: ownerIDs ]);
	
    for( Lead l : trigger.new )
	{
        Lead oldLead = trigger.oldMap.get(l.Id);
        
        Boolean oldLeadIsNo = oldLead.Status.equals('No');
        Boolean newLeadIsNo = l.Status.equals('No');
        
        if( !oldLeadIsNo && newLeadIsNo )
		{
			if( ownerMap.containsKey( l.OwnerId ) && ownerMap.get( l.OwnerId ).ProfileId == '00e60000000rNmn' )
			{
				Lead_Cycle__c LeadCycle = new Lead_Cycle__c();
				LeadCycle.Lead__c = l.id;
				LeadCycle.Cycle_Start_Date__c = l.LastTransferDate;
				LeadCycle.Cycle_End_Date__c = system.today();
				LeadCycle.OwnerId = '00560000002VLHw';
			
				LeadCycles.add(LeadCycle);
			}
        }
    
    }   
    insert LeadCycles;
}
Let me know, if you need any other help.

Thanks,
Neetu
 
This was selected as the best answer
Elliot32Elliot32
That  did the trick! Thanks very much, Neetu.