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
sam_Adminsam_Admin 

Help needed in simple test class

Hi folks,

      I have a small trigger and my test class is not giving coverage for me to deploy to prod.

 

Trigger:

 

trigger UserLead on Lead (after insert)
{
Campaign C = [Select Id,Name from Campaign where Name = 'Campaign' limit 1];
List<CampaignMember> CM = new List<CampaignMember>();
for(Lead L : Trigger.New)
{
    if(L.ConnectionReceivedId != null)
    {
        CampaignMember Cmp = new CampaignMember();
        Cmp.LeadId = L.Id;
        Cmp.CampaignId = C.Id;
        Cmp.Status = 'Sent';
        CM.add(Cmp);
    }
}
    
    insert CM;
}

 

 

Test Class:

 

@isTest
private class UserRegion {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
         List<CampaignMember> CM = new List<CampaignMember>();

 

Campaign c = new Campaign(Name = 'Universal', Campaign_Objective__c = 'Marketing', Type = 'Blog', StartDate =  Date.newInstance(2010,02,01),EndDate = Date.newInstance(2011,02,01), Status = 'New');
 insert c;
 Lead l1 = new Lead (LeadType__c = 'New', Company = 'xyz', Status = 'New', Region__c = 'CAN', LastName = 'mytest');
 insert l1;
 CampaignMember cmp = new CampaignMember( CampaignId = c.Id, LeadId = l1.Id, Status = 'Sent');
  insert cmp;
    
    }
}

 

when i run this test class, the below part is not covered and it only gives me 50%

 

        CampaignMember Cmp = new CampaignMember();
        Cmp.LeadId = L.Id;
        Cmp.CampaignId = C.Id;
        Cmp.Status = 'Sent';
        CM.add(Cmp);

 

Any help is most Appreciated.

 

TIA!

 

Best Answer chosen by Admin (Salesforce Developers) 
Kevin SwiggumKevin Swiggum

Ah, got it. yeah, you can't write to that field through code since it's realted to SF2SF. 

 

If you need your trigger logic to depend on that field being populated, the other way around would be to add code to your trigger to recognize when it's running a test.

 

Easiest way...try this.

 

if (L.connectionReceivedId != null || Test.isRunningTest() ) {

...

}

 

The test.isRunningTest will resolve to true when running a unit test...so that the unit test can enter that if block and cover those lines. 

All Answers

Kevin SwiggumKevin Swiggum

Can you populate the ConnectionReceivedId on your test lead before you insert it? Or is that a formula? It looks like that block of code is getting skipped if the lead's connectionReceivedId is null.

sam_Adminsam_Admin

Kevin,

    Thx for the reply , Actually it's not formula we are using sf2sf so iam taking that field ConnectionReceivedId , but in the test class if i include ConnectionReceivedId in the lead it gives me error

 

Field is not writeable: Lead.ConnectionReceivedId

 

iam not sure wtelse iam missing

Kevin SwiggumKevin Swiggum

Ah, got it. yeah, you can't write to that field through code since it's realted to SF2SF. 

 

If you need your trigger logic to depend on that field being populated, the other way around would be to add code to your trigger to recognize when it's running a test.

 

Easiest way...try this.

 

if (L.connectionReceivedId != null || Test.isRunningTest() ) {

...

}

 

The test.isRunningTest will resolve to true when running a unit test...so that the unit test can enter that if block and cover those lines. 

This was selected as the best answer
sam_Adminsam_Admin

Dude,

   You are awesome :) it worked and it's 100% coverage but i have a small question in trigger usually the trigger says if the lead is coming from other org then assign the lead to campaign called "Campaign" but what if i have couple of connections and i don't want to assign all the leads coming from other orgs to the same campaign, so i tried to figure out this problem but couldn't get any issue because i cannot give

if(L.ConnectionReceivedId = '000085749hfyfh') or even we don't have the field called ConnectionReceivedName in salesforce so how to override this issue?

 

TIA!

Kevin SwiggumKevin Swiggum

That sounds trickier. I'm sure you don't want to hard code that id into your code. You could use something like custom settings to store the id in there...even a custom label would be fine if you just need to store 1 connection id. Hope that helps a little bit.