+ Start a Discussion
sammy9099sammy9099 

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;
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

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

Vinit_KumarVinit_Kumar

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;

}
}

}

sammy9099sammy9099

Hi Vinit , 

 

I tried this ..I am getting this error.

 unexpected token: 'Map' at line 8 column 25

 

Regards

 

Vinit_KumarVinit_Kumar

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;

}
}

}

 

This was selected as the best answer
sammy9099sammy9099

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

Vinit_KumarVinit_Kumar

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.

sammy9099sammy9099

Hey Vinit , 

 

This is what I am getting :

 

 

27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
10:22:47.044 (44044000)|EXECUTION_STARTED
10:22:47.044 (44086000)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
10:22:47.044 (44114000)|CODE_UNIT_STARTED|[EXTERNAL]|01qi000000005xT|distyupdate on Lead trigger event BeforeUpdate for [00Qi0000001SJ33]
10:22:47.045 (45536000)|SYSTEM_CONSTRUCTOR_ENTRY|[2]|<init>()
10:22:47.045 (45568000)|SYSTEM_CONSTRUCTOR_EXIT|[2]|<init>()
10:22:47.045 (45800000)|SYSTEM_METHOD_ENTRY|[3]|LIST<Lead>.iterator()
10:22:47.046 (46122000)|SYSTEM_METHOD_EXIT|[3]|LIST<Lead>.iterator()
10:22:47.046 (46151000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()
10:22:47.046 (46179000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()
10:22:47.046 (46270000)|SYSTEM_METHOD_ENTRY|[5]|LIST<String>.add(Object)
10:22:47.046 (46299000)|SYSTEM_METHOD_EXIT|[5]|LIST<String>.add(Object)
10:22:47.046 (46308000)|SYSTEM_METHOD_ENTRY|[3]|system.ListIterator.hasNext()
10:22:47.046 (46318000)|SYSTEM_METHOD_EXIT|[3]|system.ListIterator.hasNext()
10:22:47.047 (47983000)|SOQL_EXECUTE_BEGIN|[8]|Aggregations:0|select Id from Account where Name IN :tmpVar1
10:22:47.052 (52528000)|SOQL_EXECUTE_END|[8]|Rows:0
10:22:47.052 (52666000)|SYSTEM_METHOD_ENTRY|[9]|LIST<Lead>.iterator()
10:22:47.052 (52693000)|SYSTEM_METHOD_EXIT|[9]|LIST<Lead>.iterator()
10:22:47.052 (52703000)|SYSTEM_METHOD_ENTRY|[9]|system.ListIterator.hasNext()
10:22:47.052 (52717000)|SYSTEM_METHOD_EXIT|[9]|system.ListIterator.hasNext()
10:22:47.052 (52761000)|SYSTEM_METHOD_ENTRY|[11]|MAP<Id,Account>.get(Object)
10:22:47.052 (52804000)|SYSTEM_METHOD_EXIT|[11]|MAP<Id,Account>.get(Object)
10:22:47.052 (52836000)|EXCEPTION_THROWN|[11]|System.NullPointerException: Attempt to de-reference a null object
10:22:47.052 (52958000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Trigger.distyupdate: line 11, column 1
10:22:47.052 (52970000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Trigger.distyupdate: line 11, column 1
10:22:47.979 (53014000)|CUMULATIVE_LIMIT_USAGE
10:22:47.979|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 4 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

10:22:47.979|CUMULATIVE_LIMIT_USAGE_END

10:22:47.053 (53080000)|CODE_UNIT_FINISHED|distyupdate on Lead trigger event BeforeUpdate for [00Qi0000001SJ33]
sammy9099sammy9099

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.

 

trigger distyupdate on Lead (before insert , before update){ 
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;
}
}
}

sammy9099sammy9099

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