0

I am receiving a trigger error, can anyone help as well as explain to me what I did wrong. I have been racking my brain on it for a while.

Purpose of the trigger:

I am trying to populate the name field on a related object (child) record using the Parent Records Name. I cannot override the New button & URL hack it(Lightning). Seemed like a trigger was the only solution.

 trigger PopulateRelatedProductName on ICIX_V1__PP_Relationship__c (before update, before insert){ 

Set<Id> idCO = new Set<Id>();


    for(ICIX_V1__PP_Relationship__c Rp : trigger.new) {

        if (Rp.Name != null)
        {
            idCO.add(Rp.Id); 
        }
    } 

    Map<Id,ICIX_V1__PP_Relationship__c > mapPOtoCO = [Select Id, Name FROM ICIX_V1__ICIX_Product__c  WHERE Id IN :idCO];


     for(ICIX_V1__PP_Relationship__c co : trigger.new)
     {
         Co.ICIX_V1__ICIX_Product__c  = mapPOtoCO.get(co.id).Name;
            if( co != null)
            {
                ICIX_V1__PP_Relationship__c.Name = ICIX_V1__ICIX_Product__c.Name;
          }      

    }
  }

Thank you.

1 Answer 1

0

Your code has a number of problems, but I think you're looking for the standard aggregate-query-update pattern, which generically looks like this:

Map<Id, ParentType> parents = new Map<Id, ParentType>();
for(ChildType record: Trigger.new) {
    parents.put(record.ParentField, null);
}
parents.putAll([SELECT FieldFromParent FROM ParentType WHERE Id IN :parents.keySet()]);
for(ChildType record: Trigger.new) {
    if(record.ParentField != null) {
        record.ChildFieldToUpdate = parents.get(record.ParentField).FieldFromParent;
    }
}

I'll leave this up to you to figure out exactly what this means.


As a side note, you can build this same logic in the Process Builder, so you might not even need code at all. Consider trying to use the configuration-based processes before deploying code.

6
  • In Process builder, I do not have access to the Name field when creating a record. I have already tried this avenue.
    – Jason
    Oct 9, 2016 at 5:26
  • @Jason I don't think I exactly realized you literally meant name fields, but try starting from my code example as a baseline, and see if that gets you anywhere. Alternatively, have you tried simply copying the value in the controller before you save the record?
    – sfdcfox
    Oct 9, 2016 at 5:28
  • From your code, everything saves with no error. when I run the code I do get an "execution of BeforeInsert caused by: System.StringException: Invalid id: New: External entry point" Error The Desired effect is When you click the "New" Button on the related list of the Parent Record, the Parent Record name populates in the child record create record screen. Do I need before Insert in the code to make the desired effect? I was under that impression because the record would not have been "committed/ Inserted" to the database.
    – Jason
    Oct 9, 2016 at 5:43
  • Also would I have to declare the record types within the code, There are two record types on the child object. Item, Case ?
    – Jason
    Oct 9, 2016 at 5:47
  • @Jason A trigger won't help you, because they haven't fired yet. That said, the platform should be doing this for you pretty much automatically. I think you need to take a step back and ask about the larger issue at hand: how do you get a parent's lookup populated when creating a new child record in Lightning? We need more details to help you, like what you're trying to do, what you're building, etc. The more information we have, the better we can assist you.
    – sfdcfox
    Oct 10, 2016 at 17:58

You must log in to answer this question.

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