2

How do reference a related object within a Salesforce Trigger? I am creating a record for a related object and need to assign a value to one of the fields in the new related object (relatedOject1) from a second related object (relatedOject2). I understand that a parent or related object is not returned in the list. I wrote o.relatedObject2__r.Name below to illustrate the intended data to be returned at this point.

trigger objectTrigger on object__c (after insert) {

    List<relatedObject1__c> ListToInsert = new List<relatedObject1__c>();

    for (Order__c o : Trigger.new) {
        relatedObject1__c ro = new relatedObject1__c();
        ro.Type__c = 'New Customer';
        ro.RecordTypeId = '0121a000000e8sX';
        ro.relatedOject2__c = o.relatedObject2__c;
        ro.Description__c = 'Open Order:'+o.relatedObject2__r.Name+'  Date: '+o.Date__c;
        ro.object__c = o.Id;
        ListToInsert.add(ro);
    }

    if(ListToInsert.size() > 0) {
        insert ListToInsert; 
    }
}

2 Answers 2

5

You need to query the records in the trigger context to lookup the related object fields:

trigger objectTrigger on object__c (after insert) {
    List<relatedObject1__c> ListToInsert = new List<relatedObject1__c>();

    // Query the current records so you can get the related data
    Map<Id, object__c> objMap = new Map<Id, object__c>([
        SELECT Id, relatedObject2__r.Name 
        FROM object__c
        WHERE Id IN : Trigger.new
    ]);

    for (object__c o : Trigger.new) {
        // Get current record from map and get related data
        String relatedObject2Name = objMap.get(o.Id).relatedObject2__r.Name;

        relatedObject1__c ro = new relatedObject1__c();
        ro.Type__c = 'New Customer';
        ro.RecordTypeId = '0121a000000e8sX';
        ro.relatedOject2__c = o.relatedObject2__c;
        ro.Description__c = 'Open Order:' + relatedObject2Name + '  Date: '+o.Date__c;
        ro.object__c = o.Id;
        ListToInsert.add(ro);
    }

    if(ListToInsert.size() > 0) {
        insert ListToInsert; 
    }
}

You might even want to add a conditional to check if there is even a relatedObject2__c for the records within the trigger context and decide what to do when there isn't.

As a side note, best practice is to avoid hardcoding Ids in your code. You should query the RecordTypeId that you want to assign for the relatedObject1__c records.

3

You've got two options really:

  1. Query for the related object fields that you need; OR
  2. Create a formula field on the Order__c object that returns relatedObject2__r.Name, which will be available in the after insert trigger.

For the 1st option, you would execute a SOQL query to return the Order fields you need, including relationship fields for the related object fields.

For the 2nd option, your code would almost be identical but you would use the formula field on Order__c, rather than relatedObject2__r.Name.

Eg.

ro.Description__c = 'Open Order:'+o.relatedObject2_Name__c+'  Date: '+o.Date__c;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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