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
sammy9099sammy9099 

Urgent : Trigger to populate Lookup field

Hey , 

 

I am new to salesforce triggers. I have a look up field on LEAD object called Elite_Partner_Id_gen__c . This is a lookup field to custom object called Elite_Partner_Id__c. I want the value of custom field ( Elite_Id__c) on elite_partner_Id__c object to to get copied to this look up field. Below is the code prepared from various resources. It is not showing any error but it is also not updating the look up field. Any little help will be highly appreciated.

 

trigger updateelitepartnerid on Elite_Partner_Id__c (before insert)
{
List<ID> LeadIds = New List<ID>();

for(Elite_Partner_Id__c partid : Trigger.new)
{
if(partid.Elite_Id__c != null)
{
LeadIds.add(partid.Elite_Id__c);
}
}

 

Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Id, Elite_Partner_Id_Gen__c FROM Lead WHERE Id IN :LeadIds]);

 

for(Elite_Partner_Id__c partid : Trigger.new)
{
if(partid.Name != null)
{
Lead Ld = leadMap.get(partId.Elite_Id__c);

Ld.Elite_Partner_Id_Gen__c= partid.Elite_Id__c;

leadMap.put(partId.Elite_Id__c, Ld);
}
}

update leadMap.values();
}

raseshtcsraseshtcs

Try making the trigger after insert. I did not check the code so it is possible that it still does not work. The point here is that the id of the record is not present until you insert the record. Before insert does not have the id. You need to have after insert trigger to access the id of the record.

sammy9099sammy9099

using after insert gives error "system.null.point.exception" . what to do now ??

bouscalbouscal

If you want this to fire when the Lead is insert then you need to adjust the trigger.

 

trigger updateelitepartnerid on Lead (before insert)