• Elliot32
  • NEWBIE
  • 20 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 2
    Replies
Hi All,

Disclaimer: I am a beginner developer and am still working to understand Apex; I have strong Admin experience. Please see my code below:
 
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');
        
        Boolean oldLeadIsInactive = oldLead.Trigger_Owner_Active__c == false;
        Boolean newLeadIsInactive = l.Trigger_Owner_Active__c == false;
        
        if((!oldLeadIsNo && newLeadIsNo) || (!oldLeadIsInactive && newLeadIsInactive))
        {
            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');
        
        Boolean oldLeadIsInactive = oldLead.Trigger_Owner_Active__c == false;
        Boolean newLeadIsInactive = l.Trigger_Owner_Active__c == false;
        
        if((!oldLeadIsNo && newLeadIsNo) || (!oldLeadIsInactive && newLeadIsInactive))
        {
            if(ownerMap.containsKey(l.OwnerId) && ownerMap.get(l.OwnerId).Profile.Name == 'Sales Associate')
            {
                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;
}
 
trigger EPS_OwnerActiveFalseChecker on Lead (before update) 
{
    for (Lead l : Trigger.new)
    {
        if(system.trigger.OldMap.get(l.Id).Owner_Active__c != system.trigger.NewMap.get(l.Id).Owner_Active__c)
        {
            l.Trigger_Owner_Active__c = l.Owner_Active__c;
        }
    }
}

The top trigger attempts to accomplish the following: If a User with a Profile of 'Sales Associate' has its Lead Status change to 'No' from a different value or if the field Trigger_Owner_Active__c changes to False, then create a Record associated to the Lead.

The bottom trigger attempts to copy changes in a formula field into a text field. The formula field is Owner_Active__c which looks to see if the User (Owner of the Lead) is Active, the text field is Trigger_Owner_Active__c. This serves two purposes: 1) To see if the User changes to Active = false OR if the Owner changes to a Queue (Queues cause this field to be marked false).

My issues are as follows:
1. When I change the Lead Owner to a queue, the text field changes with the formula field BUT the new Lead_Cycle__c record is not created
2. When I change the Lead Owner (User) to Active = false, the Trigger_Owner_Active__c field does NOT change with the formula field

Thanks for giving my question consideration,
Elliot
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;
}