You need to sign in to do that
Don't have an account?

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
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
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: Let us know
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.
string formatPhone = thisLead.Phone_Contacted__c.replaceAll('[^0-9]','');
Thanks,
Mathew
Please use a system.debug(thisLead.Phone_Contacted__c); before that line to check what value are you getting.
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.
@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;
}
}
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.
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
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:
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.