+ 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;
        }
    }
}