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
Kevin Chiles 930Kevin Chiles 930 

Help with raising Code Coverage

Hello!

I am trying to raise my code coverage for a trigger that picks up a converted lead, and creates a few records and eventually will send out an email to a client.  however, I cannot get my code over 57% and wanted to turn to the Community for assistance!  Any help you can provide would be awesome!  Please see the trigger below:
 
trigger createcontract on Lead (after update) {


//trigger InsertContract on Opportunity (after insert, after update) {

  List<Sertifi2_0__TestContract__c> Childs = new List<Sertifi2_0__TestContract__c>();
  
  PageReference pdf = Page.ExcelPDF;

  
  for(lead o : trigger.new)
  
    {
    
  // if(trigger.isupdate){
   
    if(o.LeadSource=='[Liberty Reseller]'&& o.isconverted==True){
    
       Sertifi2_0__TestContract__c Child = new Sertifi2_0__TestContract__c ();
       Child.Sertifi2_0__Opportunity__c = o.ConvertedOpportunityId;
       child.Sertifi2_0__Account__c=o.ConvertedAccountId;
       child.Sertifi2_0__Settings__c='Settings';
       Child.Name = 'New Contract'; 
       child.Sertifi2_0__Contact__c=o.Convertedcontactid;
       Childs.add(Child);     
          

    insert Childs;


    Sertifi2_0__EDocument__c signDoc = new Sertifi2_0__EDocument__c(
    Sertifi2_0__TestContract__c = child.id,
    Sertifi2_0__Name__c = 'FurstPerson Agreement for '+ child.Sertifi2_0__Contact__r.Name+'.pdf',
    //Sertifi2_0__Sertifi_ID__c = 'qwerty',
    Sertifi2_0__Number_Signed__c = 0,
    Sertifi2_0__Document_Type__c = 'Signing Document');
    
insert signDoc;

if (!Test.isRUnningTest()) {
            Document thedoc = [select body from document where Name = 'Sertifi Contract1'];
            //String pdfBody = EncodingUtil.urlEncode(thedoc.body.toString(), 'UTF-8');


Contact tempContact = [select id,name,email from Contact where Id =: o.Convertedcontactid][0];
Sertifi2_0__ContractContactJunction__c signer = new Sertifi2_0__ContractContactJunction__c(
    Sertifi2_0__Contact__c = tempContact.id,
    Sertifi2_0__TestContract__c = child.id,
    Sertifi2_0__Signer_Order__c = 1,
    
    Sertifi2_0__Level_Name__c = '1st Signer',
                Sertifi2_0__Email__c = tempcontact.email);
insert signer;

Attachment attach = new Attachment();
           //AttachedContentDocument attach = new AttachedContentDocument();
           //attach.ContentDocumentId = '015J0000000gLXk';
            attach.Body = thedoc.body;
            attach.name = 'Furst Agreement for ' + child.Sertifi2_0__Contact__r.Name+'.pdf';
            attach.ParentId = signDoc.Id;
            //attach.
            insert attach;
            
            Sertify_Methods.callSertifi(child.Id);

}
}
}
}

And the test class:
@isTest (SeeAllData=true)
public class Contractreatetestclass{
    static Document document;
    
    static {

    document = new Document();
    document.Body = Blob.valueOf('Some Text');
    document.ContentType = 'application/pdf';
    document.DeveloperName = 'my_document';
    document.IsPublic = true;
    document.Name = 'Sertifi Contract1';
    document.FolderId = [select id from folder where name = 'Public Documents'].id;
    insert document;

  }
  
  static testMethod void ContractreatetestclassMethod(){
  
  Test.startTest();
  
    Lead l = new Lead(FirstName='test',LastName='test',
    Company='test',
    LeadSource='[Liberty Reseller]');
    Insert L;
    
    Database.LeadConvert lc = new database.LeadConvert();
    lc.setLeadId(l.id);
    lc.setDoNotCreateOpportunity(false);
    lc.setConvertedStatus('Qualified');

    Database.LeadConvertResult lcr = Database.convertLead(lc);
    System.assert(lcr.isSuccess());

    Sertifi2_0__TestContract__c Contract = new Sertifi2_0__TestContract__c ();
       Contract.Sertifi2_0__Opportunity__c = l.ConvertedOpportunityId;
       Contract.Sertifi2_0__Account__c=l.ConvertedAccountId;
       Contract.Sertifi2_0__Settings__c='Settings';
       Contract.Name = 'New Contract'; 
       Contract.Sertifi2_0__Contact__c=l.Convertedcontactid;
       insert contract;


    Contact tempContact = new Contact(FirstName='test',LastName='Test',AccountId=l.convertedAccountId);
    
    insert tempContact;
    
    Sertifi2_0__EDocument__c signDoc = new Sertifi2_0__EDocument__c(
    Sertifi2_0__TestContract__c = contract.id,
    Sertifi2_0__Name__c = 'FurstPerson Agreement for '+ contract.Sertifi2_0__Contact__r.Name+'.pdf',
    //Sertifi2_0__Sertifi_ID__c = 'qwerty',
    Sertifi2_0__Number_Signed__c = 0,
    Sertifi2_0__Document_Type__c = 'Signing Document');
    
    insert signDoc;


    Sertifi2_0__ContractContactJunction__c signer = new Sertifi2_0__ContractContactJunction__c(
    Sertifi2_0__Contact__c = tempContact.id,
    Sertifi2_0__TestContract__c = Contract.id,
    Sertifi2_0__Signer_Order__c = 1,
    
    Sertifi2_0__Level_Name__c = '1st Signer',
                Sertifi2_0__Email__c = tempcontact.email);
    insert signer;
    
    Attachment attach = new Attachment();
           //AttachedContentDocument attach = new AttachedContentDocument();
           //attach.ContentDocumentId = '015J0000000gLXk';
            attach.Body = document.body;
            attach.name = 'Furst Agreement for ' + Contract.Sertifi2_0__Contact__r.Name+'.pdf';
            attach.ParentId = signDoc.Id;
            //attach.
            insert attach;
            

    Test.stopTest(); 


    
   }}

Where am I failing to build out the necessary components to be successful with this code?  Thank you for any support and have a great day!

Kevin Chiles
 
Best Answer chosen by Kevin Chiles 930
Terence_ChiuTerence_Chiu
In line 40 of the trigger you have an if condition that is checking if a test is not running. When you run your test class, anything within that if condition will not execute because the statement will always be false if a test is running.

All Answers

Terence_ChiuTerence_Chiu
In line 40 of the trigger you have an if condition that is checking if a test is not running. When you run your test class, anything within that if condition will not execute because the statement will always be false if a test is running.
This was selected as the best answer
Kevin Chiles 930Kevin Chiles 930
Wow, I am brilliant.  Thank you so much @Terence_Chiu !  When in doubt, make sure your syntax make sense~!  Thank you sir!

Kevin Chiles