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
sales4cesales4ce 

Trigger On Lead, which looks at other custom object for mapping values!

Hi,

 

I have a custom Object called " Country Mapping__c", which has following custom fields, "Country__c", "My_Organization__c".

Now i need to look at the Lead country and then look into the Country Mapping object and get a corresponding "My Organization" Value.

 

for ex: Country Mapping Object:

 

Country                                       My_Organization

Uruguay                                      Chile

Japan                                           Asia

United States                              USA

United States of America          USA

US                                                  USA

UK                                                   UK

United Kingdom                           UK

 

If a lead has country value=Uruguay, i need to populate "My_Organization" on Lead with "Chile".

How can i accomplish this?

Any Help/Idea on this is highly appreciated.

 

here is my trigger Uptil now:

 

trigger Map on Lead (after Insert) 
{
    List<Id> l_Ids=new List<Id>();
    for(Lead l : Trigger.New)
    {
        l_Ids.add(l.Id);
    }
    
    List<Lead> new_Leads=[Select Id, My_Organizat ion__c FROM Lead where Id in : l_Ids];
    List<Lead> update_Leads=new List<Lead>();
    
    for( Lead l : new_Leads)
    {
        l.My_Organization__c='USA';
        update_Leads.add(l);
    }
    
    Update update_Leads;       
}

 

Thanks,

Sales4ce

Best Answer chosen by Admin (Salesforce Developers) 
sforce2009sforce2009

trigger myTrigger on Lead (before Insert) //it should be before insert ideally
{
       set<string> setCountries = new set<string>();
        for(Lead l : Trigger.New)
        {
            setcountry.add(l.Country__c);
        }
        list<CountryMapping__c> lstCM = [select Country__c,Myorgnaiztion__c from CountryMapping__c where Country__c In: setCountry];
    for(Lead l: Trigger.New)
    {
        for(Integer i = 0; i < lstCM.size(); i++)
        {
            if(lstCM[i].Country__c == l.Country__c)
                l.Myorganization__c = lstCM[i].MyOrganization__c;
            break;
        }
    }
         
}

//I have not tested this. But it should work. you may have to avoid duplicates in set. Set avoids duplicate Ids when it holds Ids, but not sure about string duplacte avoiding.

All Answers

sforce2009sforce2009

trigger myTrigger on Lead (before Insert) //it should be before insert ideally
{
       set<string> setCountries = new set<string>();
        for(Lead l : Trigger.New)
        {
            setcountry.add(l.Country__c);
        }
        list<CountryMapping__c> lstCM = [select Country__c,Myorgnaiztion__c from CountryMapping__c where Country__c In: setCountry];
    for(Lead l: Trigger.New)
    {
        for(Integer i = 0; i < lstCM.size(); i++)
        {
            if(lstCM[i].Country__c == l.Country__c)
                l.Myorganization__c = lstCM[i].MyOrganization__c;
            break;
        }
    }
         
}

//I have not tested this. But it should work. you may have to avoid duplicates in set. Set avoids duplicate Ids when it holds Ids, but not sure about string duplacte avoiding.

This was selected as the best answer
David81David81

A slightly different approach using a Map to hold the related values:

 

 

trigger myTrigger on Lead (before Insert) {

       Map<String,String> = countryOrg_map = new Map<String,String>
        for(Lead l : Trigger.New){
            if(!countryOrg_map.contains(l.Country__c))
                 setcountry.add(l.Country__c);
        }

for(CountryMapping__c cm : [select Country__c,Myorgnaiztion__c from CountryMapping__c where Country__c IN :countryOrg_map.keyset()]){
     countryOrg_map.put(cm.Country__c,cm.Myorgnaiztion__c);
}

    for(Lead l: Trigger.New){
        l.Myorganization__c = countryOrg_map.get(l.Country);

    }
         
}

 

 

sales4cesales4ce

Thanks Srinivas and David for all your help. It worked like charm!