You need to sign in to do that
Don't have an account?
Urgent: Trigger to update look-up field
Hi All ,
I have two objects , leads and Accounts . Lead has three fields , distributor__c , systemid__c and pref_distributor__c .
pref_distributor__c is a look up field on Accounts . Distributor__c and systemid__c are both pick lists. Systemid__c is a dependent picklist and it's value depends on the Distributor__c . whenever I select value in distributor__c , I get a 18 digit ID in systemid__c. Now I want to write trigger which can populate the pref_distributor__c (look up field) . We are doing this since pref_distributor__c is a look up field to accounts . It will be easy to identify the lead in account and vice versa.
I have taken help from some previous posts and wrote trigger . But when I insert data into record or update it , I am getting this error.
Apex trigger distyupdate caused an unexpected exception, contact your administrator: distyupdate: execution of BeforeUpdate caused by: System.ListException: List index out of bounds: 0: Trigger.distyupdate: line 12, column 1
I have posted my code below . Kindly help me in fixing the bugs . Any little help will be highly appreciated.
Regards
trigger distyupdate on Lead (before insert , before update){
List<String> Accountname = new List<String>();
for (Lead l:Trigger.new)
{
Accountname.add(l.systemid__c);
}
List <Account> IDList = [Select Id from Account where Name in :Accountname];
for (Integer i = 0; i < Trigger.new.size(); i++)
{
if (Trigger.new[i].systemid__c!= null)
{
Trigger.new[i].pref_distributor__c = IDList[i].ID;
}
else
{
Trigger.new[i].pref_distributor__c= null;
}
}
}
My Bad,I did not initialize the map.Try below :-
trigger distyupdate on Lead (before insert , before update){
List<String> Accountname = new List<String>();
for (Lead l:Trigger.new)
{
Accountname.add(l.systemid__c);
}
//List <Account> IDList = [Select Id from Account where Name in :Accountname];
Map<Id,Account> accMap =new Map<Id,Account> ([Select Id from Account where Name in :Accountname]);
for(Lead le:Trigger.new){
if(le.systemid__c!=null){
le.pref_distributor__c= accMap.get(le.systemid__c).id;
}
}
}
All Answers
Hi Sammy,
Try below code :-
trigger distyupdate on Lead (before insert , before update){
List<String> Accountname = new List<String>();
for (Lead l:Trigger.new)
{
Accountname.add(l.systemid__c);
}
List <Account> IDList = [Select Id from Account where Name in :Accountname];
Map<Id,Account> accMap = Map<Id,Account> ([Select Id from Account where Name in :Accountname];);
for(Lead le:Trigger.new){
if(le.systemid__c!=null){
le.pref_distributor__c= accMap.get(le.systemid__c).id;
}
}
}
Hi Vinit ,
I tried this ..I am getting this error.
unexpected token: 'Map' at line 8 column 25
Regards
My Bad,I did not initialize the map.Try below :-
trigger distyupdate on Lead (before insert , before update){
List<String> Accountname = new List<String>();
for (Lead l:Trigger.new)
{
Accountname.add(l.systemid__c);
}
//List <Account> IDList = [Select Id from Account where Name in :Accountname];
Map<Id,Account> accMap =new Map<Id,Account> ([Select Id from Account where Name in :Accountname]);
for(Lead le:Trigger.new){
if(le.systemid__c!=null){
le.pref_distributor__c= accMap.get(le.systemid__c).id;
}
}
}
Hey Vinit ,
Again the same error is coming which was coming earlier.
Apex trigger distyupdate caused an unexpected exception, contact your administrator: distyupdate: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.distyupdate: line 11, column 1
Regards
Hey Sammy,
I would suggest you to check debug logs and can you check the size of map.I mean
system.debug('##########' + accMap.size());
What you get here.
Hey Vinit ,
This is what I am getting :
Hi vinit ,
The code you wrote is just working fine with just one 0r two change. I posting the code below . I was wondering , can you help me in writing the unit tests too because I am new to this stuff ? Kindly help me in writing unit tests too.
List<String> Accountname = new List<String>();
for (Lead l:Trigger.new)
{
Accountname.add(l.primary_distributor__c);
}
//List <Account> IDList = [Select Id from Account where Name in :Accountname];
Map<Id,Account> accMap =new Map<Id,Account> ([Select Id from Account where Name in :Accountname]);
for(Lead le:Trigger.new){
if(le.primary_sysid__c!=null){
le.preferred_distributor__c= accMap.get(le.primary_sysid__c).Id;
}
}
}
Hey Vinit ,
Please help me with this . The changes which you made in the code were fine with 15 digit system id . But I the systmid I have which is a dependent picklist on primary distributor__ c are 18 digit. its again throwing error
Apex trigger distyupdate caused an unexpected exception, contact your administrator: distyupdate: execution of BeforeInsert caused by: System.StringException: Invalid id: BlueStar Inc (NA): External entry point
Kindly help me with this as its very urgent
Thanks in advance
Regards