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
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12 

Auto - Update Lookup field on Lead Object !!!

Hi All,

I have a Custom object called Branch__c, which has Region__c and Country__c picklist fields.

Branch__c is a Lookup on Lead object.

Lead also have Region__c and Country__c picklist fields,

So, I want to update the Branch__c Lookup field on Lead, If the Branch Region__c and Country__c fields matches with Lead Region__c and Country__c.
If Branch Region__c is null. update the Lead Region__c with Bangalore.

For this I wrote a Trigger below.

It is saving without errors, but it will not update the Lookup field on Lead.
 
trigger UpdateBranch on Lead (before insert, before update) {
    Set<Id> ids = trigger.newmap.keySet();
    Lead ld;
    
    for (Lead lT:Trigger.new){
        ld = lT;
    }
    
    try{
        List<Branch__c> brList = [Select Id, Name, Country__c, Region__c, Email_ID__c from Branch__c where Id IN : ids];        
        Branch__c b = new Branch__c();
        for (Branch__c br : brList){
            if (brList .size() > 0 && ld.Country == br.Country__c && b.Region__c != Null){
                    ld.Branch_NAME__c = br.Id;
                    ld.Region__c = br.Region__c;
            }
            else{
                ld.Branch_NAME__c = null;
                ld.Region__c = 'Bangalore';
            }
        }
        }catch(Exception e){
         ld.addError('unable to find the Branch Name  ' + e.getMessage());
        }

}

Thanks in advance................
Best Answer chosen by rmranjith8881.3927046400771116E12
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Hi Gopi and Dushyant,

Thanks for your reply,

The trigger is working fine now, after couple of changes.

Here is my Trigger:
 
trigger UpdateBranch on Lead (before insert, before update) {
    Lead ld;    
    for (Lead lT:Trigger.new){
        ld = lT;
    }
    
    try{
        Branch__c br = [Select Id, Branch_manager__c  from Branch__c where Country__c = : ld.Country__c and Region__c = : ld.Region__c];
        ld.Branch_NAME__c = br.Id;
        }catch(Exception e){
            if(Trigger.isUpdate){
                 ld.addError('unable to find the Branch Name  ' + e.getMessage());
            } 
        }   

}
Thanks
Ranjith
 

All Answers

gopi biswalgopi biswal
if there will be multiple branches exists with same country & same region , then which one it should pick ??
Dushyant SonwarDushyant Sonwar
I think your trigger should be at after insert ,after update .
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Hi Gopi and Dushyant,

Thanks for your reply,

The trigger is working fine now, after couple of changes.

Here is my Trigger:
 
trigger UpdateBranch on Lead (before insert, before update) {
    Lead ld;    
    for (Lead lT:Trigger.new){
        ld = lT;
    }
    
    try{
        Branch__c br = [Select Id, Branch_manager__c  from Branch__c where Country__c = : ld.Country__c and Region__c = : ld.Region__c];
        ld.Branch_NAME__c = br.Id;
        }catch(Exception e){
            if(Trigger.isUpdate){
                 ld.addError('unable to find the Branch Name  ' + e.getMessage());
            } 
        }   

}
Thanks
Ranjith
 
This was selected as the best answer