+ 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

 

 

Ashley@WCELAshley@WCEL

First off, I am not clear when this trigger will run. When inserting / updating a new lead or when inserting / updating a new custom object? Should it run on both insert and update or just one?

 

Second, can you try to explain a bit more clearly using pseudo-code? By that I mean, try writing something code-like that describes the logic that this trigger needs to execute.

 

e.g.

 

oninsert new leadobject
  get customfield from customobject
  if not blank
    copyto customfield.leadobject
  else
    do nothing
  end if

onupdate leadobject
do something

 

 

 

Team WorksTeam Works

ould try on these line ::

 

trigger fillLeadLookup on Lead (before insert, after insert) {
  CampaignCC__c camp= new CampaignCC__c();
 //List<CampaignCC__c> camp= new List<CampaignCC__c>();
 List<Lead> leeds = new List<Lead>();
 
  for(Lead leed : trigger.new){
   camp = [Select Id, Name, campcodecc__c from CampaignCC__c where campcodecc__c = :leed.Campcode__c];
         system.debug(camp);    
       if(camp != null){

        //Just noticed it s throwing error Read only record
         //Do'nt know if we can assign a ID this way to a lookup field OR need to separate the code into before and after insert
            leed.LookupCC__c = camp.Id;
            leeds.add(leed);  
         
       }else{
      
        
         //system.debug('The Value in Looup is' + leed.LookupCC__c);  
         leed.LookupCC__c = '';
         leeds.add(leed);
        }     
   }
 
     insert leeds;
}