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 with record name (not ID)

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.

Is there a workaround for this?

Many thanks.
 
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;
        }
    }
}