You need to sign in to do that
Don't have an account?
Help with Apex Trigger.: no Apex coding experience at all!
Hi,
First of all I have no Apex experience at all, and up until now havent needed any, so be kind! I have my sandbox built and have done some reading up on Apex language (last time I programmed was PASCAL at college!)
I need to populate a custom lookup field on our Lead object based on it referencing a field on a custom object. As Leads can't be a child of a master-Child relationship, I've now realised the only way to do this will be an Apex trigger.
I've created a relationship between the Lead and custom object and now need the lookup field (CCLookup) on the Lead to use the value from Campaign Code (also on Lead) look up the value of the Field 'Campaign Code_CC' on my custom object and return the value of the custom object Name:
Campaign code on Lead: 1234
Campaign Code on custom Object: 1234
Name ID of Custom object: Campaign 1234
Lookup up field returns value of: Campaign1234
If no campaign code that matches exists on the custom object, the lookup field can remain blank.
Anyone willing to help!?
Antony
Assuming the name of your custom object is "Campaign2":
All Answers
Assuming the name of your custom object is "Campaign2":
Thanks for the answer! I've implemented and tried testing. My code (with actual object and fieldnames) is below.
When updating or cretaing a new record I get the error:
Error:Apex trigger updateCCLookupField caused an unexpected exception, contact your administrator: updateCCLookupField: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.updateCCLookupField: line 7, column 1
trigger updateCCLookupField on Lead (before insert, before update) {
List<Lead> leads = new List<Lead>();
for (Lead l : Trigger.new)
{
Campaign_Codes__c AssociatedCC = [SELECT Id FROM Campaign_Codes__c WHERE CodeOnCC__c= :l.Campaign_Code__c];
l.CC_Lookup__c = AssociatedCC.Id;
}
}
With a bit of help from a coder friend of mine, we have teh code working in sandbox thanks.