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 

Help : Unit Test for trigger updating 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.

 

With the help of previous posts and moderators( vinit) , I am able to write trigger and it is working fine . But , I am still newbie to unit tests Can somebody help me in writing unit test for the below mentioned trigger ?

 

trigger distyupdate on Lead (before insert , before update){
List<String> Accountname = new List<String>();
for (Lead l:Trigger.new)
{
Accountname.add(l.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.systemid__c!=null){
le.pref_distributor__c= accMap.get(le.systemid__c).Id;
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Hey Sammy,

 

Try below :-

 

@IsTest(SeeAllData=true)
public class distyupdate_test{
static testmethod void MyUnitTest(){

Lead le = new Lead(name='ABC',Distributor__c='Test',Company='XYZ',Status='Open');//Populate all the mandatory fields for lead to insert a record
insert le;


}
}

All Answers

Vinit_KumarVinit_Kumar

Hey Sammy,

 

Try below :-

 

@IsTest(SeeAllData=true)
public class distyupdate_test{
static testmethod void MyUnitTest(){

Lead le = new Lead(name='ABC',Distributor__c='Test',Company='XYZ',Status='Open');//Populate all the mandatory fields for lead to insert a record
insert le;


}
}

This was selected as the best answer
sammy9099sammy9099

Vinit , 

 

You rock man....Thanks a lot

 

Regards

sammy9099sammy9099

Hey Vinit , 

 

The code in which you helped me it wasn't really working. So I made some modifications in that code . So It was working . I am posting the code below . But , The issue with this code that whenever I create new record the lookup field (pref_distributor) gets autmatically updated depending upon the value of primary_sysid__c. But , When I edit the same record and change the primary_sysid__c value the pref_distrubtor__c value doesn't update. Kindly help me in solving this issue. I will really appreciate this .

 

trigger distyupdate on Lead (before update, before insert) {

Set<String> Accountname = new Set<String>(); 
for (lead l : trigger.New) {
if (l.primary_sysid__c != null) {
Accountname.add(l.primary_sysid__c);
}
}


if (Accountname.isEmpty()) return;


Map<String, Id> accmap = new Map<String, Id> ();
for (Account acc : [SELECT Name ,Id from Account WHERE Name IN :Accountname]) { 

accmap.put (acc.Name, acc.Id); 
}

for (lead l : trigger.New) {

if (accmap.containsKey(l.primary_sysid__c)) {
l.pref_distributor__c = accmap.get(l.primary_sysid__c);}
}
}

Vinit_KumarVinit_Kumar

Hey Sammy,

 

Try below :-

 

trigger distyupdate on Lead (before update, before insert) {

Set<String> Accountname = new Set<String>();
if(Trigger.IsInsert){
for (lead l : trigger.New) {
if (l.primary_sysid__c != null) {
Accountname.add(l.primary_sysid__c);
}
}


if (Accountname.isEmpty()) return;


Map<String, Id> accmap = new Map<String, Id> ();
for (Account acc : [SELECT Name ,Id from Account WHERE Name IN :Accountname]) {

accmap.put (acc.Name, acc.Id);
}

for (lead l : trigger.New) {

if (accmap.containsKey(l.primary_sysid__c)) {
l.pref_distributor__c = accmap.get(l.primary_sysid__c);}
}
}

if(Trigger.IsUpdate){
for (lead l : trigger.New) {
if (l.primary_sysid__c != null && l.primary_sysid__c !=trigger.oldmap.get(l.id).primary_sysid__c) {
Accountname.add(l.primary_sysid__c);
}
}


if (Accountname.isEmpty()) return;


Map<String, Id> accmap = new Map<String, Id> ();
for (Account acc : [SELECT Name ,Id from Account WHERE Name IN :Accountname]) {

accmap.put (acc.Name, acc.Id);
}

for (lead l : trigger.New) {

if (accmap.containsKey(l.primary_sysid__c)) {
l.pref_distributor__c = accmap.get(l.primary_sysid__c);}
}
}
}

sammy9099sammy9099

Hey Vinit , 

 

Still not updating the pref_distributor__c value. It is not even showing any error 

 

 

what to do now?

Vinit_KumarVinit_Kumar

Sammy,

 

Check the debug logs,there only we can see as in what is going on,that is how I will approach here.Try to get the values before and after.

sammy9099sammy9099

Ok ...I will check that ... But the unit test that u gave me is also not working. Can you pls post the updated unit test for this trigger???

Vinit_KumarVinit_Kumar

I am not sure as what is not working here,you just need to test the remaining part,I mean the (Trigger.IsUpdate) as I ahve divided it in different operation.So,just update the record which we inserted and then you are good to go.