You need to sign in to do that
Don't have an account?
Elliot32
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.
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; }
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 Use your profile name instead of 'Your Profile Name'.
You can use below code: Let me know, if you need any other help.
Thanks,
Neetu
All Answers
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
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 Use your profile name instead of 'Your Profile Name'.
You can use below code: Let me know, if you need any other help.
Thanks,
Neetu