You need to sign in to do that
Don't have an account?
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
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
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.
A slightly different approach using a Map to hold the related values:
Thanks Srinivas and David for all your help. It worked like charm!