1

In my org Lead is Child and a custom object (Intake__c) is parent. This custom object have 2 picklist fields called Intake_Month and Intake_Year. Now in trigger i am trying to access these 2 picklist fields and put them in 2 same picklist fields on Lead.

Part of My Trigger:

if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
     if(ld.Intake__c != Null){
             ld.Intake_Month__c  = ld.Intake__r.Intake_Month__c;
             ld.Intake_Year__c   = ld.Intake__r.Intake_Year__c;
           }else{
             ld.Intake_Month__c  = Null;
             ld.Intake_Year__c   = Null;
           }  
  }

But both picklists on Lead are Null. Any suggestions?

3
  • The problem is that in your before trigger, you don't have access already to the fields of your look up field. You have to request the Intake__c related like this: salesforce.stackexchange.com/questions/53548/… Commented Jun 6, 2017 at 15:41
  • @MartinLezer Perhaps we should close as duplicate?
    – sfdcfox
    Commented Jun 6, 2017 at 16:16
  • @sfdcfox I don't know the rules for questions like this. Other questions are really close but without the specific code provided in the question. What do you think? Commented Jun 6, 2017 at 16:29

2 Answers 2

2

If the picklist value has to be get from Parent object, then you could use formula field as well (in case if it works for your business).

Else, in trigger code, you are not getting the parent field data, because Parent related fields are not available by default in trigger.new context variable. You need to query the parent records and then use them to update the child.

//Take a set to get the parent Ids.
   Set<Id> parentIds = new Set<id>();

  //Iterate on the Trigger.new to collect the parent Ids.
  for(Lead ld : Trigger.new){
    if(ld.Intake__c != null){
        parentIds.add(ld.Intake__c);
    }
  }

  Map<Id, Intake__c> intakeMap = new Map<Id, Intake__c>([select id, Intake_Month__c, Intake_Year__c from Intake__c where Id In: parentIds]);

  //Iterate again and use above map to get the data of intake month and year.
  for(Lead ld : Trigger.new){
      if(ld.Intake__c != Null){
            //use the map here to get the data of parent record.
             ld.Intake_Month__c  = intakeMap.get(ld.Intake__c).Intake_Month__c;
             ld.Intake_Year__c   = intakeMap.get(ld.Intake__c).Intake_Year__c;
        }
        else{
             ld.Intake_Month__c  = Null;
             ld.Intake_Year__c   = Null;
        } 
    }
4

You are going to have to query for the Intake__c object and store the info in a map accessible by the lead.Intake__c Id. For example:

Set<Id> intakeIds = new Set<Id>();
for (Lead lead : Trigger.new) {
    leadIds.add(lead.Intake__c);
}

Map<Id, Intake__c> intakes = new Map<Id, Intake__c>([SELECT Id, Name, Intake_Month__c, Intake_Year__c FROM Intake__c WHERE Id IN :intakeIds]);

if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
    if(ld.Intake__c != Null){
        ld.Intake_Month__c  = intakes.get(ld.Intake__c).Intake_Month__c;
        ld.Intake_Year__c   = intakes.get(ld.Intake__c).Intake_Year__c;
    }else{
        ld.Intake_Month__c  = Null;
        ld.Intake_Year__c   = Null;
    }  
}

You don't have related objects automatically in Triggers. This is what makes Trigger Frameworks so important, because you can imagine that in a large org with many relationships, the 101 SOQL Query Limit could be hit very fast. Just make sure to keep the query out of your Trigger's for loops.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .