Reputation: 3
Using an Apex Trigger, I am attempting to update the Company field, during the creation of a new lead, with the value which the user selected from a custom lookup field on the same object. So, the user clicks new on the lead tab to create a new lead. enters the first name, last name and other details. Instead of typing in the company name, the user then searches for the company name in the lookup field, compName__c, and selects it from the list. What I would like to accomplish is when the the user clicks save, the value in the compName field is copied to the Company field on the new record, then the new record is created.
Here is what I have done thus far;...
trigger UpdateLeadCompany on Lead (before insert) {
Set<Id> AN = New Set<Id>();
For (Lead LeadComp : Trigger.New)
{
IF(LeadComp.compName__c != null)
{
AN.add(LeadComp.compName__c);
}
}
Map<Id, Agency__c> y = [SELECT Name, compName__c FROM compName__c WHERE Id IN :AN];
For(Lead CompNameUpdate : Trigger.New)
{
Company a = y get(CompNameUpdate.compName__c);
IF(a != null)
{
CompNameUpdate = a.compName__c;
}
}
}
Upvotes: 0
Views: 8132
Reputation: 146
Updated
Then your code should be like -
trigger UpdateLeadCompany on Lead (before insert) {
Set<Id> AN = New Set<Id>();
for(Lead leadObj : Trigger.New) {
IF(leadObj.compName__c != null) {
AN.add(leadObj.compName__c);
}
}
Map<Id, compName__c> y = [SELECT Name, compName__c FROM compName__c WHERE Id IN :AN];
For(Lead leadObj : Trigger.New) {
compName__c a = y.get(leadObj.compName__c);
IF(a != null) {
leadObj.Company = a.Name;
}
}
}
Upvotes: 1