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
Itayb34Itayb34 

Trigger to Populate Lookup Fields

Hi 

 

I'm quite new to the triggers world, so forgive me for any mistakes...

I have a field populated with contact ID, and I want that two lookup fields will be populated accordingly ("Assigned to Partner" and "Assigned to Partner Rep")

I managed to populate the "Assigned to Partner Rep" with the "Partner Rep" contect (Id in this case). "Assigned to partner" is populated only after another edit, not right away 

(He is populated by the "Partner SF ID", which is the partner account ID)

 

trigger AssignedRep on Lead(before insert, before update) {
  for (Lead a : Trigger.new) {
   if(a.LeadSource =='Partner' && a.Partner_Rep__c != null){
    a.Assigned_to_Partner_Rep__c = a.Partner_Rep__c;  
    if(a.Assigned_to_Partner_Rep__c != null)
    a.Assigned_to_Partner__c = a.Partner_SF_ID__c;
        }
       }
}

 

Also, how do I write a test coverage to this code? I understood it's mandatory (although the code is quite simple...)

Any help is appriciated!

 

 

Thanks!

 

Itay

florianhoehnflorianhoehn

Hi Itay,

 

It looks to me like this could solve your issue: You were checking the field 'Partner_Rep__c' twice to be not null.

 

trigger AssignedRep on Lead(before insert, before update) {
    for(Lead thisLead : Trigger.new) {
        if(thisLead.LeadSource =='Partner' 
                && thisLead.Partner_Rep__c != null) {
            thisLead.Assigned_to_Partner_Rep__c = thisLead.Partner_Rep__c;  
            if(thisLead.Partner_SF_ID__c != null) {
                thisLead.Assigned_to_Partner__c = thisLead.Partner_SF_ID__c;
            }
        }
    }
}

 

For test coverage you need to create a test class which inserts and updates a lead (and several for bulk operations). Have a look here for Testing Best Practices.

 

Hope this helps.

 

Florian