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
Geof131313Geof131313 

Issue with test class not validating properly

Hello,

 

I am having an issue with a test class that I can't for the life of me figure out why it won't validate my code properly.

 

The Trigger does the following:

 

  • Runs on Campaign Member create/update

 

 

  • If campaign member contains LeadId, push Campaign.CampaignName into Lead.Campaign_Name__c, a custom text field on the Lead object
  • If campaign member contains LeadId and associated Lead.Campaign_Name__c is not null (before insert), set Email_Inside_Sales_Rep__c to 1
  • If campaign member contains ContactId, set Account.Email_AM__c to 1
  • If campaign member contains ContactId and Account.AccountManager__c <> Account.Customer_Relationship_Manager__c, set Email_CRM__c to 1
 
The problem that I am having is that while the trigger works perfectly fine, the test class keeps running into an error. Code is here:

public class CampaignPushTest {
static testMethod void runCampPushTest() {
Lead lead1 = new Lead();
lead1.firstname = 'test';
lead1.lastname = 'insert';
lead1.company = 'ReturnPathTest';
insert lead1;

Lead lead2 = new Lead();
lead2.firstname = 'test2';
lead2.lastname = 'insert2';
lead2.company = 'ReturnPathTest2';
lead2.campaign_name__c = 'SSORG: Test';
lead2.Email_Inside_Sales_Rep__c = 0;
insert lead2;

Campaign cmpn1 = new Campaign();
cmpn1.name = 'TestInsert';
cmpn1.IsActive = true;
insert cmpn1;

Campaign cmpn2 = new Campaign();
cmpn2.name = 'TestInsert2';
cmpn2.IsActive = true;
insert cmpn2;

Campaign cmpn3 = new Campaign();
cmpn3.name = 'TestInsert3';
cmpn3.IsActive = true;
insert cmpn3;

Account acct = new Account();
acct.name = 'RPTestAcct';
acct.AccountManager__c = 'Unassigned';
acct.Email_AM__c = 0;
acct.Email_CRM__c = 0;
acct.Customer_Relationship_Manager_Primary__c = 'Unassigned';
insert acct;

Contact contct = new Contact();
contct.lastname = 'test2';
contct.accountid = acct.id;
insert contct;

//Run as Sales Ops
User SalesOps = new User(id = '00500000006olmF');
System.RunAs(SalesOps){
CampaignMember CmpnM1 = new CampaignMember();
CmpnM1.leadid = lead1.id;
CmpnM1.campaignid = cmpn1.id;
insert CmpnM1;
}

integer theNewLead = [select count() from Lead where Campaign_Name__c = 'TestInsert' limit 1];
if(theNewLead > 0){
System.Assert(true);
}
else{
System.Assert(false);
}

System.RunAs(SalesOps){
CampaignMember CmpnM2 = new CampaignMember();
CmpnM2.leadid = lead2.id;
CmpnM2.campaignid = cmpn1.id;
insert CmpnM2;
}

integer theNewLead2 = [select count() from Lead where Email_Inside_Sales_Rep__c > 0 limit 1];
if(theNewLead2 > 0){
System.Assert(true);
}
else{
System.Assert(false);
}

System.RunAs(SalesOps){
CampaignMember CmpnM3 = new CampaignMember();
CmpnM3.contactid = contct.id;
CmpnM3.campaignid = cmpn1.id;
insert CmpnM3;
}

integer theNewAcct = [select count() from Account where Email_CRM__c > 0 limit 1];
if(theNewAcct == 0){
System.Assert(true);
}
else{
System.Assert(false);
}

acct.AccountManager__c = 'testAM';
acct.Email_CRM__c = 0;
update acct;

System.RunAs(SalesOps){
CampaignMember CmpnM4 = new CampaignMember();
CmpnM4.contactid = contct.id;
CmpnM4.campaignid = cmpn2.id;
insert CmpnM4;
}

integer theNewAcct2 = [select count() from Account where Email_AM__c > 0 limit 1];
if(theNewAcct2 > 0){
System.Assert(true);
}
else{
System.Assert(false);
}
acct.Customer_Relationship_Manager_Primary__c = 'testCRM';
acct.Email_AM__c = 0;
update acct;

System.RunAs(SalesOps){
CampaignMember CmpnM5 = new CampaignMember();
CmpnM5.contactid = contct.id;
CmpnM5.campaignid = cmpn3.id;
insert CmpnM5;
}

integer theNewAcct3 = [select count() from Account where Email_CRM__c > 0 limit 1];
if(theNewAcct3 > 0){
System.Assert(true);
}
else{
System.Assert(false);
}
}
}

 

The error that I am getting has to do with the fact that my queries are returning a count of 0, and as a result evaluating false. I can't figure out why this is, everything is working properly when I run actual sandbox tests on the objects. Can someone please help me with this?

 
Thanks,
Geoffrey
 

P.S. Here's the code for the trigger and the error messages:

 

trigger campaign_name_push on CampaignMember (after insert, after update) {

list<CampaignMember> theCampMemb = new list<CampaignMember>();
list<CampaignMember> theCampMemb2 = new list<CampaignMember>();
list<Id> theCampaigns = new list<Id>();
list<Id> theLeads = new list<Id>();
list<Id> theContacts = new list<Id>();
list<Id> theAccounts = new list<Id>();
for(CampaignMember cpmb : trigger.new) {
if(cpmb.leadid <> null) {
theCampMemb.add(cpmb);
theCampaigns.add(cpmb.campaignid);
theLeads.add(cpmb.leadid);
}
else if(cpmb.contactid <> null){
theCampMemb2.add(cpmb);
theContacts.add(cpmb.contactid);
}
}

map<Id,Campaign> campNames = new Map<Id,Campaign>([Select Name from Campaign where Id in :theCampaigns]);
map<Id,Lead> leadNames = new Map<Id,Lead>([Select Campaign_Name__c from Lead where Id in :theLeads]);
map<Id,Contact> AcctIds = new Map<Id,Contact>([Select AccountId from Contact where Id in :theContacts]);


for(Id getaccts : theContacts){
theAccounts.add(AcctIds.get(getaccts).AccountId);
}

map<Id,Account> AcctManagers = new Map<Id,Account>([Select AccountManager__c, Customer_Relationship_Manager_Primary__c from Account where Id in :theAccounts]);


list<Lead> UpdateLeads = new list<Lead>();
list<Account> UpdateAccounts = new list<Account>();

system.debug('before cm loop');
for(CampaignMember cm : theCampMemb){
system.debug('in cm loop');
Lead l = new Lead(Id = cm.LeadId);
l.Campaign_Name__c = campNames.get(cm.CampaignId).name;
system.debug('after getting campaign_name' + l.Campaign_Name__c);
if(leadNames.get(cm.LeadId).Campaign_Name__c <> null){
l.Email_Inside_Sales_Rep__c = 1;
system.debug('after setting inside sales rep');
}
UpdateLeads.add(l);
}
for(CampaignMember cm2 : theCampMemb2){
String AM = AcctManagers.get(AcctIds.get(cm2.contactid).AccountId).AccountManager__c;
String CRM = AcctManagers.get(AcctIds.get(cm2.contactid).AccountId).Customer_Relationship_Manager_Primary__c;
Account a = new Account(Id = (AcctIds.get(cm2.contactid).AccountId));
a.Email_AM__c = 1;
if(AM <> CRM) a.Email_CRM__c = 1;
a.ContactId__c = cm2.contactid;
UpdateAccounts.add(a);
}
Update UpdateLeads;
Update UpdateAccounts;
}

 


 

Error Messages:

 

 

 

P.P.S. Whenever I put the code in it seems to reformat like this, so if anyone has any advice on how to fix this or would like me to send them the files, please email me.

 

Message Edited by Geof131313 on 11-10-2009 01:17 PM
Message Edited by Geof131313 on 11-10-2009 01:19 PM
Message Edited by Geof131313 on 11-10-2009 01:23 PM
Message Edited by Geof131313 on 11-10-2009 01:34 PM
Message Edited by Geof131313 on 11-10-2009 01:36 PM
Message Edited by Geof131313 on 11-10-2009 01:36 PM
jkucerajkucera
Please paste the exact error.  Best way to paste code is to copy & then paste using the Insert Code button next to the ABC icon.
Geof131313Geof131313

Ah the formatting problem was a browser issue. I was using Chrome, and when I switched to Firefox it worked. Here are the errors:

 

*** Beginning Test 1: CampaignPushTest.static testMethod void runCampPushTest() 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 7, column 9: Insert: SOBJECT:Lead 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 7, column 9: DML Operation executed in 138 ms 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 15, column 9: Insert: SOBJECT:Lead 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 15, column 9: DML Operation executed in 139 ms 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 20, column 9: Insert: SOBJECT:Campaign 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 20, column 9: DML Operation executed in 307 ms 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 25, column 9: Insert: SOBJECT:Campaign 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 25, column 9: DML Operation executed in 21 ms 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 30, column 9: Insert: SOBJECT:Campaign 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 30, column 9: DML Operation executed in 19 ms 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 38, column 9: Insert: SOBJECT:Account *** Beginning Account Validation Rule Evaluation for null Start Time: 20091110213113.138 Rule Name: Currency_Must_be_USD Error Condition Formula: OR(ISPICKVAL(CurrencyIsoCode, 'GBP'),ISPICKVAL(CurrencyIsoCode, 'AUS'),ISPICKVAL(CurrencyIsoCode, 'EUR')) Value(s) Found: CurrencyIsoCode=USD Result: PASS - Continue End Time: 20091110213113.138 *** Ending Account Validation Rule Evaluation for null 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 38, column 9: DML Operation executed in 191 ms 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 43, column 9: Insert: SOBJECT:Contact 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 43, column 9: DML Operation executed in 56 ms 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 51, column 13: Insert: SOBJECT:CampaignMember *** Beginning campaign_name_push on CampaignMember trigger event AfterInsert for 00vR0000001BajO 20091110213113.416:Trigger.campaign_name_push: line 9, column 5: SelectLoop:LIST:SOBJECT:CampaignMember 20091110213113.416:Trigger.campaign_name_push: line 9, column 5: Number of iterations: 1 20091110213113.416:Trigger.campaign_name_push: line 21, column 55: SOQL query with 1 row finished in 6 ms 20091110213113.416:Trigger.campaign_name_push: line 22, column 47: SOQL query with 1 row finished in 11 ms 20091110213113.416:Trigger.campaign_name_push: line 23, column 51: SOQL query with 0 rows finished in 3 ms 20091110213113.416:Trigger.campaign_name_push: line 26, column 5: SelectLoop:LIST:Id 20091110213113.416:Trigger.campaign_name_push: line 26, column 5: Number of iterations: 0 20091110213113.416:Trigger.campaign_name_push: line 30, column 56: SOQL query with 0 rows finished in 4 ms 20091110213113.416:Trigger.campaign_name_push: line 36, column 3: before cm loop 20091110213113.416:Trigger.campaign_name_push: line 37, column 5: SelectLoop:LIST:SOBJECT:CampaignMember 20091110213113.416:Trigger.campaign_name_push: line 38, column 3: in cm loop 20091110213113.416:Trigger.campaign_name_push: line 41, column 3: after getting campaign_nameTestInsert 20091110213113.416:Trigger.campaign_name_push: line 37, column 5: Number of iterations: 1 20091110213113.416:Trigger.campaign_name_push: line 48, column 5: SelectLoop:LIST:SOBJECT:CampaignMember 20091110213113.416:Trigger.campaign_name_push: line 48, column 5: Number of iterations: 0 20091110213113.416:Trigger.campaign_name_push: line 57, column 5: Update: LIST:SOBJECT:Lead 20091110213113.416:Trigger.campaign_name_push: line 57, column 5: DML Operation executed in 42 ms 20091110213113.416:Trigger.campaign_name_push: line 58, column 5: Update: LIST:SOBJECT:Account Cumulative resource usage: Resource usage for namespace: (default) Number of SOQL queries: 4 out of 100 Number of query rows: 2 out of 500 Number of SOSL queries: 0 out of 20 Number of DML statements: 9 out of 100 Number of DML rows: 9 out of 500 Number of script statements: 66 out of 200000 Maximum heap size: 0 out of 1000000 Number of callouts: 0 out of 10 Number of Email Invocations: 0 out of 10 Number of fields describes: 0 out of 10 Number of record type describes: 0 out of 10 Number of child relationships describes: 0 out of 10 Number of picklist describes: 0 out of 10 Number of future calls: 0 out of 10 Number of find similar calls: 0 out of 10 Number of System.runAs() invocations: 1 out of 20 Total email recipients queued to be sent : 0 Static variables and sizes: campaign_name_push:AcctIds:4 campaign_name_push:AcctManagers:4 campaign_name_push:UpdateAccounts:4 campaign_name_push:UpdateLeads:62 campaign_name_push:campNames:44 campaign_name_push:leadNames:34 campaign_name_push:theAccounts:4 campaign_name_push:theCampMemb:8 campaign_name_push:theCampMemb2:4 campaign_name_push:theCampaigns:26 campaign_name_push:theContacts:4 campaign_name_push:theLeads:8 *** Ending campaign_name_push on CampaignMember trigger event AfterInsert for 00vR0000001BajO 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 51, column 13: DML Operation executed in 119 ms 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 54, column 30: SOQL count() query with 1 row finished in 8 ms 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 66, column 13: Insert: SOBJECT:CampaignMember *** Beginning campaign_name_push on CampaignMember trigger event AfterInsert for 00vR0000001BajP 20091110213113.528:Trigger.campaign_name_push: line 9, column 5: SelectLoop:LIST:SOBJECT:CampaignMember 20091110213113.528:Trigger.campaign_name_push: line 9, column 5: Number of iterations: 1 20091110213113.528:Trigger.campaign_name_push: line 21, column 55: SOQL query with 1 row finished in 3 ms 20091110213113.528:Trigger.campaign_name_push: line 22, column 47: SOQL query with 1 row finished in 3 ms 20091110213113.528:Trigger.campaign_name_push: line 23, column 51: SOQL query with 0 rows finished in 2 ms 20091110213113.528:Trigger.campaign_name_push: line 26, column 5: SelectLoop:LIST:Id 20091110213113.528:Trigger.campaign_name_push: line 26, column 5: Number of iterations: 0 20091110213113.528:Trigger.campaign_name_push: line 30, column 56: SOQL query with 0 rows finished in 4 ms 20091110213113.528:Trigger.campaign_name_push: line 36, column 3: before cm loop 20091110213113.528:Trigger.campaign_name_push: line 37, column 5: SelectLoop:LIST:SOBJECT:CampaignMember 20091110213113.528:Trigger.campaign_name_push: line 38, column 3: in cm loop 20091110213113.528:Trigger.campaign_name_push: line 41, column 3: after getting campaign_nameTestInsert 20091110213113.528:Trigger.campaign_name_push: line 44, column 3: after setting inside sales rep 20091110213113.528:Trigger.campaign_name_push: line 37, column 5: Number of iterations: 1 20091110213113.528:Trigger.campaign_name_push: line 48, column 5: SelectLoop:LIST:SOBJECT:CampaignMember 20091110213113.528:Trigger.campaign_name_push: line 48, column 5: Number of iterations: 0 20091110213113.528:Trigger.campaign_name_push: line 57, column 5: Update: LIST:SOBJECT:Lead 20091110213113.528:Trigger.campaign_name_push: line 57, column 5: DML Operation executed in 156 ms 20091110213113.528:Trigger.campaign_name_push: line 58, column 5: Update: LIST:SOBJECT:Account Cumulative resource usage: Resource usage for namespace: (default) Number of SOQL queries: 9 out of 100 Number of query rows: 5 out of 500 Number of SOSL queries: 0 out of 20 Number of DML statements: 11 out of 100 Number of DML rows: 11 out of 500 Number of script statements: 99 out of 200000 Maximum heap size: 0 out of 1000000 Number of callouts: 0 out of 10 Number of Email Invocations: 0 out of 10 Number of fields describes: 0 out of 10 Number of record type describes: 0 out of 10 Number of child relationships describes: 0 out of 10 Number of picklist describes: 0 out of 10 Number of future calls: 0 out of 10 Number of find similar calls: 0 out of 10 Number of System.runAs() invocations: 2 out of 20 Total email recipients queued to be sent : 0 Static variables and sizes: campaign_name_push:AcctIds:4 campaign_name_push:AcctManagers:4 campaign_name_push:UpdateAccounts:4 campaign_name_push:UpdateLeads:95 campaign_name_push:campNames:44 campaign_name_push:leadNames:65 campaign_name_push:theAccounts:4 campaign_name_push:theCampMemb:8 campaign_name_push:theCampMemb2:4 campaign_name_push:theCampaigns:26 campaign_name_push:theContacts:4 campaign_name_push:theLeads:8 *** Ending campaign_name_push on CampaignMember trigger event AfterInsert for 00vR0000001BajP 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 66, column 13: DML Operation executed in 195 ms 20091110213112.481:Class.CampaignPushTest.runCampPushTest: line 69, column 31: SOQL count() query with 0 rows finished in 9 ms System.Exception: Assertion Failed Class.CampaignPushTest.runCampPushTest: line 74, column 13 External entry point Cumulative resource usage: Resource usage for namespace: (default) Number of SOQL queries: 10 out of 100 Number of query rows: 5 out of 500 Number of SOSL queries: 0 out of 20 Number of DML statements: 11 out of 100 Number of DML rows: 11 out of 500 Number of script statements: 101 out of 200000 Maximum heap size: 0 out of 1000000 Number of callouts: 0 out of 10 Number of Email Invocations: 0 out of 10 Number of fields describes: 0 out of 10 Number of record type describes: 0 out of 10 Number of child relationships describes: 0 out of 10 Number of picklist describes: 0 out of 10 Number of future calls: 0 out of 10 Number of find similar calls: 0 out of 10 Number of System.runAs() invocations: 2 out of 20 Total email recipients queued to be sent : 0 Stack frame variables and sizes: Frame0 *** Ending Test CampaignPushTest.static testMethod void runCampPushTest()

 

 

 

jkucerajkucera

I believe you have to query for the Campaign before the ID is available after insert.  Are you sure the campiagn member is being created?

 

CmpnM1.campaignid = cmpn1.id;
A quick way to check is to put this right below the above line:

 

system.debug('CM 1 CampaignID: ' CmpnM1.CampaignID);

Geof131313Geof131313
Sounds like that might work, I'll try it out.
Geof131313Geof131313

So I added in a query of the campaign ids, and now I'm having another error:

 

public class CampaignPushTest { static testMethod void runCampPushTest() { Lead lead1 = new Lead(); lead1.firstname = 'test'; lead1.lastname = 'insert'; lead1.company = 'ReturnPathTest'; insert lead1; Lead lead2 = new Lead(); lead2.firstname = 'test2'; lead2.lastname = 'insert2'; lead2.company = 'ReturnPathTest2'; lead2.campaign_name__c = 'SSORG: Test'; lead2.Email_Inside_Sales_Rep__c = 0; insert lead2; list<String> CampaignNames = new list<String>(); Campaign cmpn1 = new Campaign(); cmpn1.name = 'TestInsert'; cmpn1.IsActive = true; CampaignNames.add(cmpn1.name); insert cmpn1; Campaign cmpn2 = new Campaign(); cmpn2.name = 'TestInsert2'; cmpn2.IsActive = true; CampaignNames.add(cmpn2.name); insert cmpn2; Campaign cmpn3 = new Campaign(); cmpn3.name = 'TestInsert3'; cmpn3.IsActive = true; CampaignNames.add(cmpn3.name); insert cmpn3; map<String,Campaign> campIds = new Map<String,Campaign>([Select Id from Campaign where Name in :CampaignNames]); Campaign cmpn1Id = new Campaign(Id = campIds.get(cmpn1.Name).Id); Campaign cmpn2Id = new Campaign(Id = campIds.get(cmpn2.Name).Id); Campaign cmpn3Id = new Campaign(Id = campIds.get(cmpn3.Name).Id); Account acct = new Account(); acct.name = 'RPTestAcct'; acct.AccountManager__c = 'Unassigned'; acct.Email_AM__c = 0; acct.Email_CRM__c = 0; acct.Customer_Relationship_Manager_Primary__c = 'Unassigned'; insert acct; Contact contct = new Contact(); contct.lastname = 'test2'; contct.accountid = acct.id; insert contct; //Run as Sales Ops User SalesOps = new User(id = '00500000006olmF'); System.RunAs(SalesOps){ CampaignMember CmpnM1 = new CampaignMember(); CmpnM1.leadid = lead1.id; CmpnM1.campaignid = cmpn1.Id; insert CmpnM1; } integer theNewLead = [select count() from Lead where Campaign_Name__c = 'TestInsert' limit 1]; if(theNewLead > 0){ System.Assert(true); } else{ System.Assert(false); } System.RunAs(SalesOps){ CampaignMember CmpnM2 = new CampaignMember(); CmpnM2.leadid = lead2.id; CmpnM2.campaignid = cmpn1.Id; insert CmpnM2; } integer theNewLead2 = [select count() from Lead where Email_Inside_Sales_Rep__c > 0 limit 1]; if(theNewLead2 > 0){ System.Assert(true); } else{ System.Assert(false); } System.RunAs(SalesOps){ CampaignMember CmpnM3 = new CampaignMember(); CmpnM3.contactid = contct.id; CmpnM3.campaignid = cmpn1.Id; insert CmpnM3; } integer theNewAcct = [select count() from Account where Email_CRM__c > 0 limit 1]; if(theNewAcct == 0){ System.Assert(true); } else{ System.Assert(false); } acct.AccountManager__c = 'testAM'; acct.Email_CRM__c = 0; update acct; System.RunAs(SalesOps){ CampaignMember CmpnM4 = new CampaignMember(); CmpnM4.contactid = contct.id; CmpnM4.campaignid = cmpn2Id.Id; insert CmpnM4; } integer theNewAcct2 = [select count() from Account where Email_AM__c > 0 limit 1]; if(theNewAcct2 > 0){ System.Assert(true); } else{ System.Assert(false); } acct.Customer_Relationship_Manager_Primary__c = 'testCRM'; acct.Email_AM__c = 0; update acct; System.RunAs(SalesOps){ CampaignMember CmpnM5 = new CampaignMember(); CmpnM5.contactid = contct.id; CmpnM5.campaignid = cmpn3Id.Id; insert CmpnM5; } integer theNewAcct3 = [select count() from Account where Email_CRM__c > 0 limit 1]; if(theNewAcct3 > 0){ System.Assert(true); } else{ System.Assert(false); } } }

 

 Error is this:

 

*** Beginning Test 1: CampaignPushTest.static testMethod void runCampPushTest() 20091110223651.764:Class.CampaignPushTest.runCampPushTest: line 7, column 9: Insert: SOBJECT:Lead 20091110223651.764:Class.CampaignPushTest.runCampPushTest: line 7, column 9: DML Operation executed in 80 ms 20091110223651.764:Class.CampaignPushTest.runCampPushTest: line 15, column 9: Insert: SOBJECT:Lead 20091110223651.764:Class.CampaignPushTest.runCampPushTest: line 15, column 9: DML Operation executed in 74 ms 20091110223651.764:Class.CampaignPushTest.runCampPushTest: line 23, column 9: Insert: SOBJECT:Campaign 20091110223651.764:Class.CampaignPushTest.runCampPushTest: line 23, column 9: DML Operation executed in 51 ms 20091110223651.764:Class.CampaignPushTest.runCampPushTest: line 29, column 9: Insert: SOBJECT:Campaign 20091110223651.764:Class.CampaignPushTest.runCampPushTest: line 29, column 9: DML Operation executed in 18 ms 20091110223651.764:Class.CampaignPushTest.runCampPushTest: line 35, column 9: Insert: SOBJECT:Campaign 20091110223651.764:Class.CampaignPushTest.runCampPushTest: line 35, column 9: DML Operation executed in 19 ms 20091110223651.764:Class.CampaignPushTest.runCampPushTest: line 37, column 65: SOQL query with 3 rows finished in 4 ms System.NullPointerException: Attempt to de-reference a null object Class.CampaignPushTest.runCampPushTest: line 39, column 70 External entry point Cumulative resource usage: Resource usage for namespace: (default) Number of SOQL queries: 1 out of 100 Number of query rows: 3 out of 500 Number of SOSL queries: 0 out of 20 Number of DML statements: 5 out of 100 Number of DML rows: 5 out of 500 Number of script statements: 30 out of 200000 Maximum heap size: 0 out of 1000000 Number of callouts: 0 out of 10 Number of Email Invocations: 0 out of 10 Number of fields describes: 0 out of 10 Number of record type describes: 0 out of 10 Number of child relationships describes: 0 out of 10 Number of picklist describes: 0 out of 10 Number of future calls: 0 out of 10 Number of find similar calls: 0 out of 10 Number of System.runAs() invocations: 0 out of 20 Total email recipients queued to be sent : 0 Stack frame variables and sizes: Frame0 *** Ending Test CampaignPushTest.static testMethod void runCampPushTest()

 Any idea what it means to de-reference a null object?

 

Thanks,

Geoffrey

 

 

 

jkucerajkucera
Dereferencing a null means you're pointing to a null record (one that doesn't exist). Might mean your query isn't returning anything.
Geof131313Geof131313
According to the error message, the query is returning three rows.
Geof131313Geof131313

It looks to me like there is an issue with the query returning three blank rows or it might be an issue with the way that I am trying to pull the value out of the query. Here is the most updated version:

 

public class CampaignPushTest2 { static testMethod void runCampPushTest() { Lead lead1 = new Lead(); lead1.firstname = 'test'; lead1.lastname = 'insert'; lead1.company = 'ReturnPathTest'; insert lead1; Lead lead2 = new Lead(); lead2.firstname = 'test2'; lead2.lastname = 'insert2'; lead2.company = 'ReturnPathTest2'; lead2.campaign_name__c = 'SSORG: Test'; lead2.Email_Inside_Sales_Rep__c = 0; insert lead2; list<String> CampaignNames = new list<String>(); Campaign cmpn1 = new Campaign(); cmpn1.Name = '1TestInsert'; cmpn1.IsActive = true; CampaignNames.add(cmpn1.name); insert cmpn1; Campaign cmpn2 = new Campaign(); cmpn2.Name = '2TestInsert'; cmpn2.IsActive = true; CampaignNames.add(cmpn2.name); insert cmpn2; Campaign cmpn3 = new Campaign(); cmpn3.Name = '3TestInsert'; cmpn3.IsActive = true; CampaignNames.add(cmpn3.name); insert cmpn3; map<String,Campaign> campIds = new Map<String,Campaign>([Select Name,Id from Campaign where Name in :CampaignNames]); system.debug('cmpn1 Id is: ' + campIds.get(cmpn1.Name).Id); Campaign cmpn1Id = campIds.get(cmpn1.Name); Campaign cmpn2Id = campIds.get(cmpn2.Name); system.debug('cmpn2 Id is: ' + cmpn2Id.Name); //cmpn2Id.isactive = true; Campaign cmpn3Id = campIds.get(cmpn3.Name); //cmpn3Id.isactive = true; Account acct = new Account(); acct.name = 'RPTestAcct'; acct.AccountManager__c = 'Unassigned'; acct.Email_AM__c = 0; acct.Email_CRM__c = 0; acct.Customer_Relationship_Manager_Primary__c = 'Unassigned'; insert acct; Contact contct = new Contact(); contct.lastname = 'test2'; contct.accountid = acct.id; insert contct; //Run as Sales Ops User SalesOps = new User(id = '00500000006olmF'); System.RunAs(SalesOps){ CampaignMember CmpnM1 = new CampaignMember(); CmpnM1.leadid = lead1.id; CmpnM1.campaignid = cmpn1Id.Id; insert CmpnM1; } integer theNewLead = [select count() from Lead where Campaign_Name__c = 'TestInsert' limit 1]; if(theNewLead > 0){ System.Assert(true); } else{ System.Assert(false); } System.RunAs(SalesOps){ CampaignMember CmpnM2 = new CampaignMember(); CmpnM2.leadid = lead2.id; CmpnM2.campaignid = cmpn2Id.Id; insert CmpnM2; } string x = 'select Email_Inside_Sales_Rep__c from Lead where Id = \'' + lead2.id + '\' limit 1'; List<sObject> s = Database.query(x); system.debug('query results: ' + s[0]); system.debug('lead2 id: ' + lead2.id); integer theNewLead2 = [select count() from Lead where Email_Inside_Sales_Rep__c = 1 limit 1]; if(theNewLead2 > 0){ System.Assert(true); } else{ System.Assert(false); } System.RunAs(SalesOps){ CampaignMember CmpnM3 = new CampaignMember(); CmpnM3.contactid = contct.id; CmpnM3.campaignid = cmpn1Id.Id; insert CmpnM3; } integer theNewAcct = [select count() from Account where Email_CRM__c > 0 limit 1]; if(theNewAcct == 0){ System.Assert(true); } else{ System.Assert(false); } acct.AccountManager__c = 'testAM'; acct.Email_CRM__c = 0; update acct; System.RunAs(SalesOps){ CampaignMember CmpnM4 = new CampaignMember(); CmpnM4.contactid = contct.id; CmpnM4.campaignid = cmpn2Id.Id; insert CmpnM4; } integer theNewAcct2 = [select count() from Account where Email_AM__c > 0 limit 1]; if(theNewAcct2 > 0){ System.Assert(true); } else{ System.Assert(false); } acct.Customer_Relationship_Manager_Primary__c = 'testCRM'; acct.Email_AM__c = 0; update acct; System.RunAs(SalesOps){ CampaignMember CmpnM5 = new CampaignMember(); CmpnM5.contactid = contct.id; CmpnM5.campaignid = cmpn3Id.Id; insert CmpnM5; } integer theNewAcct3 = [select count() from Account where Email_CRM__c > 0 limit 1]; if(theNewAcct3 > 0){ System.Assert(true); } else{ System.Assert(false); } } }

 

 

 

 Here are the errors I'm getting when I try to call out the values in the system.debug command:

 

*** Beginning Test 1: CampaignPushTest2.static testMethod void runCampPushTest() 20091111174910.523:Class.CampaignPushTest2.runCampPushTest: line 7, column 9: Insert: SOBJECT:Lead 20091111174910.523:Class.CampaignPushTest2.runCampPushTest: line 7, column 9: DML Operation executed in 66 ms 20091111174910.523:Class.CampaignPushTest2.runCampPushTest: line 15, column 9: Insert: SOBJECT:Lead 20091111174910.523:Class.CampaignPushTest2.runCampPushTest: line 15, column 9: DML Operation executed in 75 ms 20091111174910.523:Class.CampaignPushTest2.runCampPushTest: line 23, column 9: Insert: SOBJECT:Campaign 20091111174910.523:Class.CampaignPushTest2.runCampPushTest: line 23, column 9: DML Operation executed in 30 ms 20091111174910.523:Class.CampaignPushTest2.runCampPushTest: line 29, column 9: Insert: SOBJECT:Campaign 20091111174910.523:Class.CampaignPushTest2.runCampPushTest: line 29, column 9: DML Operation executed in 16 ms 20091111174910.523:Class.CampaignPushTest2.runCampPushTest: line 35, column 9: Insert: SOBJECT:Campaign 20091111174910.523:Class.CampaignPushTest2.runCampPushTest: line 35, column 9: DML Operation executed in 17 ms 20091111174910.523:Class.CampaignPushTest2.runCampPushTest: line 37, column 65: SOQL query with 3 rows finished in 3 ms System.NullPointerException: Attempt to de-reference a null object Class.CampaignPushTest2.runCampPushTest: line 39, column 64 External entry point Cumulative resource usage: Resource usage for namespace: (default) Number of SOQL queries: 1 out of 100 Number of query rows: 3 out of 500 Number of SOSL queries: 0 out of 20 Number of DML statements: 5 out of 100 Number of DML rows: 5 out of 500 Number of script statements: 30 out of 200000 Maximum heap size: 0 out of 1000000 Number of callouts: 0 out of 10 Number of Email Invocations: 0 out of 10 Number of fields describes: 0 out of 10 Number of record type describes: 0 out of 10 Number of child relationships describes: 0 out of 10 Number of picklist describes: 0 out of 10 Number of future calls: 0 out of 10 Number of find similar calls: 0 out of 10 Number of System.runAs() invocations: 0 out of 20 Total email recipients queued to be sent : 0 Stack frame variables and sizes: Frame0 *** Ending Test CampaignPushTest2.static testMethod void runCampPushTest()