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
AntonyWarcAntonyWarc 

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

Best Answer chosen by Admin (Salesforce Developers) 
wt35wt35

Assuming the name of your custom object is "Campaign2":

 

trigger updateCCLookupField on Lead (before insert, before update) {

List<Lead> leads = new List<Lead>();

  for (Lead l : Trigger.new)
  {
    Campaign2__c AssociatedCC = [SELECT Id FROM Campaign2__c WHERE CampaignCode__c= :l.CampaignCode__c];
    l.CCLookup__c = AssociatedCC.Id;
  }

}

 

All Answers

wt35wt35

Assuming the name of your custom object is "Campaign2":

 

trigger updateCCLookupField on Lead (before insert, before update) {

List<Lead> leads = new List<Lead>();

  for (Lead l : Trigger.new)
  {
    Campaign2__c AssociatedCC = [SELECT Id FROM Campaign2__c WHERE CampaignCode__c= :l.CampaignCode__c];
    l.CCLookup__c = AssociatedCC.Id;
  }

}

 

This was selected as the best answer
AntonyWarcAntonyWarc

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

}

 

AntonyWarcAntonyWarc

With a bit of help from a coder friend of mine, we have teh code working in sandbox thanks.