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
Mathew AbrahamMathew Abraham 

Trigger - Need help

Hi Trigger Experts!

Could someone please help me fix this trigger?  This is part of a huge Lead trigger, and I want to insert the following script to check if a phone number is on the Lead record is referenced in another object called 'NVMStatsSF__NVM_Call_Summary__c', and if so, get the three fields, i.e. Lead Campaign, Lead DMA, and Lead Clinic, with the values that are available in the NVM Call Summary object.

I inserted this script into the master Lead trigger, and when I try to deploy it to production, I get error 
values

1   trigger LeadTrigger on Lead (before insert, before update) {
2      for (Lead thisLead : trigger.new) {
3
4   Set<string> phoneContacted = new Set<string>();
5   for(Lead thisLead : trigger.new) {
6        string formatPhone = thisLead.Phone_Contacted__c.replaceAll('[^0-9]','');
7       phoneContacted.add(formatPhone);
8       }
9   List<NVMStatsSF__NVM_Call_Summary__c> NvmSumList = new list<NVMStatsSF__NVM_Call_Summary__c>();
10
11   NvmSumList = [SELECT Id, NVMStatsSF__CLID__c, Campaign__c, DMA__c, Clinic__c FROM NVMStatsSF__NVM_Call_Summary__c                WHERE NVMStatsSF__CLID__c IN: phoneContacted];   
12  
13   List<NVMStatsSF__NVM_Call_Summary__c> thisNvmSumList = new list<NVMStatsSF__NVM_Call_Summary__c>();   
14    for (integer i = 0; i < NvmSumList.size(); i++) {
15          if (NvmSumList[i].NVMStatsSF__CLID__c == thisLead.Phone_Contacted__c) {                        
16                    thisNvmSumList.add(NvmSumList[i]);
17                    }
18                }
19                
20          if (thisNvmSumList.size() >= 1 && thisLead.Campaign__c == Null ) {
21                    thisLead.Campaign__c = thisNvmSumList[0].Campaign__c;
22                    thisLead.Clinic__c = thisNvmSumList[0].Clinic__c;
23                    thisLead.DMA__c = thisNvmSumList[0].DMA__c;
24                }
25        }
26   }

Error Message:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LeadTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.LeadTrigger: line 6, column 1: []
Stack Trace: Class.TestCaseTrigger.leadCase: line 9, column 1

By the way, the same line 6 is causing several issues on the Test Class, but I just want to fix the Trigger for now, and will worry about  Test Class later!  :)

Thank you for your helps..
Mathew
Agustin BAgustin B
Hi Mathew, you have a double for in line 2 and 5, why is that? 
Also try a system.debug(thisLead.Phone_Contacted__c); before line 6 to see which value you get.
Try to put the for filling the set at the top of the trigger like this:
1   trigger LeadTrigger on Lead (before insert, before update) {
   Set<string> phoneContacted = new Set<string>();
   for(Lead thisLead : trigger.new) {
system.debug(thisLead.Phone_Contacted__c);
        string formatPhone = thisLead.Phone_Contacted__c.replaceAll('[^0-9]','');
       phoneContacted.add(formatPhone);
       }
  for (Lead thisLead : trigger.new) {

......
Let us know
 
Mathew AbrahamMathew Abraham
Okay, line 5 was copied by mistake when I reproduced the code lines here.  Only line 2 have that script.  Good catch!  Thank you 
Agustin BAgustin B
In that case which line is the line 6 the error is showing?
In the line after the query of the NvmSumList please use a system.debug(NvmSumList.size()); to check if you are getting anything.
If you are not, use a system.debug(phoneContacted); before the query and check if you have created a NVMStatsSF__NVM_Call_Summary__c record in your test class that match that query.

If it helps please like and if it solves your issue please mark as best as it can help others.
If you still have issues please post again the correct code with the lines and the test class.
Mathew AbrahamMathew Abraham
This line:
string formatPhone = thisLead.Phone_Contacted__c.replaceAll('[^0-9]','');

Thanks,
Mathew
Agustin BAgustin B
Great, on your test class when you create the Lead, are you putting a Phone_Contacted__c value?
Please use a system.debug(thisLead.Phone_Contacted__c); before that line to check what value are you getting.
Agustin BAgustin B
Some notes on this, but is a messy code, you have some tests with seeAllData true and others with nothing.

1) You are not generating any NVMStatsSF__NVM_Call_Summary__c record in the test class.
Put a System.debug(phoneContacted); before the query and when you run the test you should see the value in the Debug logs.
With that value run that query in your query editor in the developer console.(This is because this test will have to have a NVMStatsSF__NVM_Call_Summary__c record created to be retrieved with the proper conditions or you will use a SeeAllData true condition in your test, in that case make sure that record exists using the query editor).
2)I am not seeing the test class leadCase which generated the error on your first comment. Please show it complete to me.

If you are making progress please like the comment and if it solves your issue then mark as correct please.
Mathew AbrahamMathew Abraham
Okay, so the test Class error is not on Lead Test Class !  Aha, that's another good catch...  Here is that test class.  Line 9, of course is 'insert NewLead;'

@isTest
public class TestCaseTrigger {
    
    @isTest public static void leadCase() {
        
        Lead newLead = new Lead();
        newLead.FirstName = 'John';
        newLead.LastName = 'Smith';
        insert newLead;
        
        Lead testLead = [SELECT Id FROM Lead LIMIT 1];
        
        Case newCase = new Case();
        newCase.Status = 'New';
        newCase.Origin = 'Phone';
        newCase.Subject = 'Call with ' + testLead.Id;
        insert newCase;
    }
    
    @isTest public static void accountCase() {
        
        Account newAccount = new Account();
        newAccount.FirstName = 'Jane';
        newAccount.LastName = 'Doe';
        insert newAccount;
        
        Account testAccount = [SELECT Id FROM Account LIMIT 1];
        
        Case newCase = new Case();
        newCase.Status = 'New';
        newCase.Origin = 'Phone';
        newCase.Subject = 'Call with ' + testAccount.Id;
        insert newCase;
    }
    
    @isTest public static void noRecordCase() {
        Case newCase = new Case();
        newCase.Status = 'New';
        newCase.Origin = 'Phone';
        newCase.Subject = 'Call with';
        insert newCase;
    }
}
Agustin BAgustin B
Great, so you will have to add the phone contacted there:
@isTest public static void leadCase() {
        
        Lead newLead = new Lead();
        newLead.FirstName = 'John';
        newLead.LastName = 'Smith';
newLead.Phone_Contacted__c = //The value you want;
        insert newLead;
        
        Lead testLead = [SELECT Id FROM Lead LIMIT 1];
        
        Case newCase = new Case();
        newCase.Status = 'New';
        newCase.Origin = 'Phone';
        newCase.Subject = 'Call with ' + testLead.Id;
        insert newCase;
    }
That will make it pass the famous line 6 of your error but it will not cover the rest as you have not created a related NVMStatsSF__NVM_Call_Summary__c. Test here if you you will it passes that line.

Let me know if you pass that error and I can help you cover the rest. If not please specify the error, the line and the content of that line.

Please like each comment in which I was of assistance because it helps me improve in this community.
Mathew AbrahamMathew Abraham
Okay, good point!  Lemme try that!  By the way is it possible to review the complete LeadTrigger and suggest how to fix it?  Thanks for your help.  
Mathew AbrahamMathew Abraham
Okay, not its showing error in the following lines.

          if (thisNvmSumList != Null && thisLead.Campaign__c == Null ) {
                    thisLead.Campaign__c = thisNvmSumList[0].Campaign__c;
                    thisLead.Clinic__c = thisNvmSumList[0].Clinic__c;
                    thisLead.DMA__c = thisNvmSumList[0].DMA__c;
                }

I am trying to update the Lead fields with the corresponding NVM Summary Object fields.  These three fields are Lookup fields in NVM Sumary to the respective Objects (i.e., Campaign, DMA, and Clinic custom objects), thus they are shown as their Ids in NVM Summary and not names..
Thanks
Agustin BAgustin B
Hi Mathew I assume you are getting null values, in that case you need to also create those records on your test class.
So create and insert Campaign, after that asign the inserted id to the NVMStatsSF__NVM_Call_Summary__c you are going to insert as well in the test class. Do it also for DMA and Clinic and at the end insert the NVMStatsSF__NVM_Call_Summary__c.
On your Lead those records are also lookup right?
This may be a guideline of what you should be inserting in the test class:
@isTest public static void leadCase() {
        
        Lead newLead = new Lead();
        newLead.FirstName = 'John';
        newLead.LastName = 'Smith';
newLead.Phone_Contacted__c = //The value you want;
        insert newLead;
        Campaign__c camp = new Campaign__c(//your required fields);
        insert camp;
        DMA__c dma = new DMA__c(//your required fields);
        insert dma;
        Clinic__c clinic = new Clinic__c(//your required fields);
        insert clinic;
        NVMStatsSF__NVM_Call_Summary__c nvm = new NVMStatsSF__NVM_Call_Summary__c(Campaign__c = camp.id,DMA__C=dma.id,Clinic__c =clinic.id,//and the other fields like the Phone Contacted);

        Lead testLead = [SELECT Id FROM Lead LIMIT 1];
        
        Case newCase = new Case();
        newCase.Status = 'New';
        newCase.Origin = 'Phone';
        newCase.Subject = 'Call with ' + testLead.Id;
        insert newCase;
    }


if you keep finding problems, show me the exact line,with the message and the code of your test class(leadCase) you have been changing so I can help you better.

Please as we are making good progress like my comments so it rewards my time helping as it motivates people answer and help in the community.