You need to sign in to do that
Don't have an account?
Patrick/ 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.
Is there a workaround for this?
Many thanks.
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.
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
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'
Is that something easily solved?
Many thanks.
I'm now getting the error - "Error: Compile Error: unexpected token: 'List' at line 10 column 7"
Any thoughts?
In this case, you're missing a closing curly brace } at the end of your for-loop on line 8.
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 ;)