0

I am trying to populate the Name field on a New Child Record, using the Parent Record Name. There will be a Record type that a user will choose from. The desired effect is that I want the Parent record name to populate in the Child records Name field once the New button is clicked on the Related list for the related Object (Parent Record related list, New button for the Child Record). My code is below, so far I am getting no errors with saving the code in the dev console. I am getting a "PopulateRelatedProductName2: execution of BeforeInsert caused by: System.StringException: Invalid id: ParentNameShouldBeHere: External entry point" as I try to save the New Child Record. When I click the New Button, The New child record field is blank.

CODE:

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

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

    for(ICIX_V1__PP_Relationship__c Rp : trigger.new) {
    {
        idCO.add(Rp.Id); 
    }
} 
  Map<Id, ICIX_V1__ICIX_Product__c> parents = new Map<Id, ICIX_V1__ICIX_Product__c>();
    for(ICIX_V1__PP_Relationship__c record: Trigger.new) {
        parents.put(record.Name, null);
}
  parents.putAll([SELECT Name FROM ICIX_V1__ICIX_Product__c WHERE Id IN :parents.keySet()]);
     for(ICIX_V1__PP_Relationship__c record: Trigger.new) {
       if(record.Name != null) {
          record.Name = parents.get(record.Id).Name;
    }
}  

}

0

Your parents map key is and id type. Either Change the map key to a string or put record.id instead of record.name as the key when populating the map

parents.put(record.Id, null);

The most likely reason you are not getting a compile error is a string can be an id, just not if the string at runtime is not actually an id

1
  • Ah, I see now. Excellent Thank You +20 – Jason Oct 9 '16 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.