function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Patrick/ JamesPatrick/ James 

Apex Trigger - Trying to populate lookup field

Hi there,

I'm quite new to Apex Triggers, so just trying to find my feet here.

On the Campaign object we have a lookup to a custom object called 'Course Templates'. We also have a lookup on the Campaign object to another custom object called 'Course Archives'.

All of the 'Course Template' records have duplicates 'Course Archive' records. So - for example - the Course Template record 'Acting - Beginners' has a Course Archive equivalent (also called 'Acting - Beginners').

The trigger below is intended to populate the Course Archive lookup on the Campaign, using the name of the Course Template. The error I'm receiving seems to suggest that I actually need the ID of the record in order to input into this lookup field, but I only have the name of the record.
 
trigger CourseArchiveInclusion on Campaign (before insert, before update) { 
    for (Campaign c : Trigger.new) {
        if(c.status != null && (c.Status.equals('Completed') ||  c.Status.equals('Cancelled'))){
            c.course_archive__c = c.Template_Name_for_Archive__c;
        }
    }
}

Is there a workaround for this?

Many thanks.
 
Marek Kosar_Marek Kosar_
Hello,

yes you need to know Id, so your options are:
1. to populate course archive lookup, you have to query for it (so you get Id of that record)  by Name (I suppose this name is unique for both custom objects) - don't forget to bulkify it -> query outside of for loop
2. you create new field (or use existing one where you store name), set it as External ID + unique -> then you can use this external ID to populate lookup without knowing ID (you populate this field during creation of both - Course Templates and Archives - with the same value)

Marek
 
Terence_ChiuTerence_Chiu
Patrck, do you actually have a record with name stored in Template_Name_for_Archive__c ? If so you can query for that record using the name so that you can pull the Id. For example:
 
trigger CourseArchiveInclusion on Campaign (before insert, before update) { 
    
      Set<String> courseArchiveNames = new Set<String>

       for(Campaign c : trigger.new){
                 courseArchiveNames.add(c.Template_Name_for_Archive__c;);
       }

       List<Insert Course Archive API Name> archives = [SELECT Id, Name FROM <Insert Course Archive API Name> WHERE Name IN : courseArchiveNames ]


for (Campaign c : Trigger.new) {
       for(<Insert Course Archive API Name> ca : archives ){
                 if(ca.Name == c.Template_Name_for_Archive__c && c.status != null && (c.Status.equals('Completed') ||  c.Status.equals('Cancelled'))){
                             c.course_archive__c = ca.Id;
                  }
        }
 
    }
}

 
Patrick/ JamesPatrick/ James
Hi Terence,

Yes, we do have a record with name stored in 'Template_Name_for_Archive__c'

I adjusted the code yout sent over, but it came back with 'Error: Compile Error: unexpected token: 'for' at line 5 column 7'
 
trigger CourseArchiveInclusion on Campaign (before insert, before update) { 
    
      Set<String> courseArchiveNames = new Set<String>

       for(Campaign c : trigger.new){
                 courseArchiveNames.add(c.Template_Name_for_Archive__c;);
       }

       List Course_Archive__c archives = [SELECT Id, Name FROM  Course_Archive__c WHERE Name IN : courseArchiveNames ]


for (Campaign c : Trigger.new) {
       for( Course_Archive__c ca : archives ){
                 if(ca.Name == c.Template_Name_for_Archive__c && c.status != null && (c.Status.equals('Completed') ||  c.Status.equals('Cancelled'))){
                             c.course_archive__c = ca.Id;
                  }
        }
 
    }
}

Is that something easily solved?

Many thanks.
Marek Kosar_Marek Kosar_
you missing () and semicolon :)
Set<String> courseArchiveNames = new Set<String>();

 
Patrick/ JamesPatrick/ James
Thanks very much Marek - your help is appreciated.

I'm now getting the error - "Error: Compile Error: unexpected token: 'List' at line 10 column 7"

Any thoughts?
 
trigger CourseArchiveInclusion on Campaign (before insert, before update) { 
    
      Set<String> courseArchiveNames = new Set<String>();


       for(Campaign c : trigger.new){
                 courseArchiveNames.add(c.Template_Name_for_Archive__c);
       

       List Course_Archive__c archives = [SELECT Id, Name FROM  Course_Archive__c WHERE Name IN : courseArchiveNames ]


for (Campaign c : Trigger.new) {
       for( Course_Archive__c ca : archives ){
                 if(ca.Name == c.Template_Name_for_Archive__c && c.status != null && (c.Status.equals('Completed') ||  c.Status.equals('Cancelled'))){
                             c.course_archive__c = ca.Id;
                  }
        }
 
    }
}


 
Dan1126Dan1126
Whenever you receive that error, it's because you're missing something (typically either a semi-colon or curly brace) that syntactically completes the statement on the line before. 

In this case, you're missing a closing curly brace }  at the end of your for-loop on line 8. 
Marek Kosar_Marek Kosar_
trigger CourseArchiveInclusion on Campaign (before insert, before update) { 
    
      Set<String> courseArchiveNames = new Set<String>();


       for(Campaign c : trigger.new){
                 courseArchiveNames.add(c.Template_Name_for_Archive__c);
       

       List<Course_Archive__c> archives = [SELECT Id, Name FROM  Course_Archive__c WHERE Name IN : courseArchiveNames ];


for (Campaign c : Trigger.new) {
       for( Course_Archive__c ca : archives ){
                 if(ca.Name == c.Template_Name_for_Archive__c && c.status != null && (c.Status.equals('Completed') ||  c.Status.equals('Cancelled'))){
                             c.course_archive__c = ca.Id;
                  }
        }
 
    }
}
^^^ this shold be ok ^^^
But you are missing some very basic knowledge about apex/programming, you should invest your time and start here:
https://developer.salesforce.com/trailhead/trail/force_com_dev_beginner
...and then continue with other trails.
If you don't know Trailhead, it's a great resource of salesforce trainings, interactive, for free and it's FUN ;)
Dan1126Dan1126
Also, I just realized once you fix that you will get another compile error on line 10, since you didn't put your Course_Archive__c in angle brackets. It should be: List<Course_Archive__c> archives