1. Award
  2. Users
  3. Nominee

nominee is the juction object

and one more object AllResult

I want that when a record is inserted in nominee object, the same record gets inserted in AllResult Object as well.

For this I have written this trigger :

trigger MakeEntryInAllResult on Nominee__c (after insert)

    All_Result__C newEntry;

    for(Nominee__c newNominee:Trigger.new)
        newEntry = new All_Result__c();

        newEntry.Award_Name__c = newNominee.Award__r.Name;
        newEntry.Nominee_Name__c = newNominee.Users__r.Name;

        insert newEntry;

but I am getting the values of newEntry.Award_Name__c and newEntry.Nominee_Name__c as null.

Please help

2 Answers 2


In the Trigger context collections, only the immediate fields of the object are populated meaning that the related fields (the __r ones) can't be used. Instead you have to query for the data:

trigger MakeEntryInAllResult on Nominee__c (after insert) {

    Set<Id> awardIds = new Set<Id>();
    Set<Id> userIds = new Set<Id>();
    for (Nominee__c n : Trigger.new) {

    Map<Id, Award__c> awards = new Map<Id, Award__c>([
            select Name from Award__c where Id in :awardIds
    Map<Id, User__c> users = new Map<Id, User__c>([
            select Name from User__c where Id in :userIds

    All_Result__c[] inserts = new All_Result__c[] {};
    for (Nominee__c n : Trigger.new) {
        inserts.add(new All_Result__c(
                Award_Name__c = awards.get(n.Award__c).Name,
                Nominee_Name__c = users.get(n.User__c).Name
    insert inserts;

I downvoted Keiths answer. Yes, he is correct in that you need to requery to access relationship fields. However, you can get by with a simple, single query instead of two queries with maps.

You'll also want to move your insert or DML outside of the for loop, as Keiths example correctly demonstrates.

List<All_Result__c> allResults = new List<All_Result__c>();
for(Nominee__c newNominee : 
        Id in :Trigger.newMap.keySet()]){
        new All_Result__c(

insert allResults;
  • James yep yours is a better solution.
    – Keith C
    May 28, 2014 at 19:12

You must log in to answer this question.

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