You need to sign in to do that
Don't have an account?

lookupfield update with a trigger
Can anyone help with a sample code that can autopopulate a lookup field from a text field on the same object (lead) upon creation
Both the text field and the lookup field are on the lead object. While creating a new lead, we have the value of the text field written from an external source and we want to write that same value into the lookup field.
thank you
Both the text field and the lookup field are on the lead object. While creating a new lead, we have the value of the text field written from an external source and we want to write that same value into the lookup field.
thank you
set<string>toupdate = new set<string>();
for (lead l : trigger.new){
if (l.externalSource__c!=NULL)
toupdate.add(l.externalSource__c);
}
lMAccountLookup__c rvp =[ select id, Name from lMAccountLookup__c where Name=:toupdate];
for (lead l : trigger.new){
l.lMAccountLookup__c = rvp.id;
}
}
thanks guys for pointing me in the right direction. this worked for me
All Answers
Can you give more details on your data model please? What is the lookup field related to?
hope my explanation is clear enough.
thanks
however because it's a text field, it doesnt tie the lead to the LMObject. we need a lookup field for that.
hence we have a look up field called LMAccounts on the lead object that points to the LMObject. and i want to write the account name in the partner field to the lookup field to create that relationship
First you need to query for the LMAccount with the name matching the value in the external source field (lets say that field is called lead.externalSource__c)
The above query assumes that ho e names on the LMAccounts are uniqiue.
Once you have relatedLMAccount, you need check if a record was found, if it was then assign it to the lookup field (lead.lMAccountLookup__c)
You probably would want to do this on a before insert trigger.
Hope this helps.
set<string>toupdate = new set<string>();
for (lead l : trigger.new){
if (l.externalSource__c!=NULL)
toupdate.add(l.externalSource__c);
}
lMAccountLookup__c rvp =[ select id, Name from lMAccountLookup__c where Name=:toupdate];
for (lead l : trigger.new){
l.lMAccountLookup__c = rvp.id;
}
}
thanks guys for pointing me in the right direction. this worked for me