+ Start a Discussion
Smita HodiggeriSmita Hodiggeri 

Code Coverage stuck at 72% -- How Can I improve the coverage

Hey Devs,

For the following my test class  code coverage doesnt seem to go up above 75%, This is my first test class.
Any thoughts on how to improve the code coverage are much appreciated.

Actual Class

public class EngagementSharepointFolderCreation {
    @InvocableMethod(label='Setup Engagements Folder structure in Sharepoint' description='Creates accounts with children of Opportunities and Deliverables')
    public static void CreateEngagementsFolder (List<String> engagementId){
        CreateEngagementsFolderHierarchy(engagementId);
    }
    @future(callout=true)
    public static void CreateEngagementsFolderHierarchy(List<String> lstOfEngagementIds){
        try{
            // get the Engagement
            
            List<pse__Proj__c> lstOfEngagementRecs = [Select Id, Engagement_SharePoint_Folder_Name__c, AccountEngagementSharePointFolderIdOnEng__c,pse__Account__c, pse__Account__r.Name, pse__Account__r.Account_Engagement_SharePoint_Folder_Id__c, pse__Opportunity__r.Name,pse__Account__r.Opportunity_Sharepoint_Folder_Id__c, pse__Proj__c.Engagement_Sharepoint_Folder_Id__c from pse__Proj__c where Id = :lstOfEngagementIds];
            
            List<Account> lstOfAccountsToPSEFolderIds = new List<Account>();
            
            List<Opportunity> lstOfOppPSEFolderIds = new List<Opportunity>();
            
            
            //get auth info from the Settings MDT
            fileforcem1__Sharepoint_Settings__mdt orgSettings = [SELECT fileforcem1__Named_Credential__c,fileforcem1__Document_Library_Id__c,fileforcem1__Site_Id__c FROM fileforcem1__Sharepoint_Settings__mdt WHERE Developername = 'Default' limit 1];
            
            //Initialize the client object
            fileforcem1.ClientApi apiClient = new fileforcem1.ClientApi();
            apiClient.namedCredentialName = orgSettings.fileforcem1__Named_Credential__c;
            
            
            //get the root folder id for the Engagement -- Need to check whats this?
            fileforcem1__SharePoint_Object_Settings__mdt objectSettings = [SELECT fileforcem1__Root_Folder_Id__c,fileforcem1__Site_Id__c,fileforcem1__Document_Library_Id__c,fileforcem1__Initial_Folder_Structure__c FROM fileforcem1__Sharepoint_Object_Settings__mdt WHERE MasterLabel ='pse__Proj__c' LIMIT 1 ];
            
            apiClient.sharepointSiteId = objectSettings.fileforcem1__Site_Id__c;
            apiClient.documentLibraryId = objectSettings.fileforcem1__Document_Library_Id__c;
            for(pse__Proj__c  engagement : lstOfEngagementRecs){
                String accFolderId;
                fileforcem1.SharepointFileInfo accFolder;
                if (String.isBlank(engagement.pse__Account__r.Account_Engagement_SharePoint_Folder_Id__c))
                {
                    //Create the Customer Account folder and record the ID
                    
                    if (!Test.isRunningTest()) {
                        accFolder = apiClient.createFolder(Label.PSA_CCX_Account, engagement.pse__Account__r.Name);
                        accFolderId = accFolder.Id;
                        lstOfAccountsToPSEFolderIds.add(new Account(Id = engagement.pse__Account__c, Account_Engagement_SharePoint_Folder_Id__c = accFolder.Id));
                    } else {accFolder=new fileforcem1.SharepointFileInfo();}
                    
                }else{
                    accFolderId = engagement.pse__Account__r.Account_Engagement_SharePoint_Folder_Id__c;
                }
                
                //Create the opportunity folder and record the ID
                fileforcem1.SharepointFileInfo oppFolder;
                if (!Test.isRunningTest()) {
                    oppFolder = apiClient.createFolder(accFolderId, engagement.pse__Opportunity__r.Name);
                    lstOfOppPSEFolderIds.add(new Opportunity(Id = engagement.pse__Opportunity__c, Opportunity_PSA_Sharepoint_Folder_Id__c = oppFolder.Id));
                } else{ oppFolder = new fileforcem1.SharepointFileInfo();}
                System.debug('Opp Folder created successfully'+oppFolder.Id);
                
                //Create PSE folder
                
                fileforcem1.SharepointFileInfo pseFolder;
                if (!Test.isRunningTest()){
                pseFolder = apiClient.createFolder(oppFolder.Id, engagement.Engagement_SharePoint_Folder_Name__c);
                engagement.sharepoint_folder_id__c = pseFolder.Id;
                }
                //Create Deliverables Folder under opportunity
                fileforcem1.SharepointFileInfo Deliverable1Folder;
                if (!Test.isRunningTest()) {
                    Deliverable1Folder  = apiClient.createFolder(pseFolder.Id, 'Deliverables');
               
                }
                else {Deliverable1Folder = new fileforcem1.SharepointFileInfo(); }
                engagement.Deliverable_Folder_Id__c= Deliverable1Folder.Id;
                System.debug('Deliverable Folder created successfully'+Deliverable1Folder.Id);
            }
            
            // update Account Engagement Folder Id 
            if(!lstOfAccountsToPSEFolderIds.isEmpty()){
                update lstOfAccountsToPSEFolderIds;
            }
            // update Opportunity Engagement Folder Id
            if(!lstOfOppPSEFolderIds.isEmpty()){
                update lstOfOppPSEFolderIds;
            }
            
            //update the opportunity to save the IDs
            update lstOfEngagementRecs;
        }
        
        catch(DmlException e) {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
    }
}

The Test Class for the same

 // Engagement(Deliverable) folder test class creation
    
  static testMethod void folderCreationTestEngagement()
  {
      
  Test.StartTest();         
  
  //Create Accounts To be related to Engagement
 
  Account customerAccTobeRelatedToEngagement = new Account();
  customerAccTobeRelatedToEngagement.Name ='TestCustomerAccountTobeRelatedToEngagement';
  customerAccTobeRelatedToEngagement.Type ='Customer';
  customerAccTobeRelatedToEngagement.D_Invoice_Delivery_Method__c= 'Email';
  customerAccTobeRelatedToEngagement.D_Historic_Customer_Folder_Locations__c= 'HistoricCustomerFolderLocation';  
  insert customerAccTobeRelatedToEngagement;
  System.debug('The new inserted account' + customerAccTobeRelatedToEngagement);    
      
  Account prospectAccTobeRelatedToEngagement = new Account();    
  prospectAccTobeRelatedToEngagement.Name='TestProspectAccountTobeRelatedToEngagement';
  prospectAccTobeRelatedToEngagement.Type='Prospect';
  prospectAccTobeRelatedToEngagement.D_Invoice_Delivery_Method__c='Email';
  prospectAccTobeRelatedToEngagement.D_Historic_Customer_Folder_Locations__c='HistoricCustomerFolderLocation';    
  prospectAccTobeRelatedToEngagement.D_External_ID_From_Legacy_Systems__c='ExternalId';    
  insert prospectAccTobeRelatedToEngagement;
  System.debug('The new inserted prospect account' + prospectAccTobeRelatedToEngagement); 

   //add account ids to be related to the Engagement to the list
  List<String> accIDsTobeRelatedToEngagement = new List<String>();
  accIDsTobeRelatedToEngagement.add(customerAccTobeRelatedToEngagement.Id); 
  accIDsTobeRelatedToEngagement.add(prospectAccTobeRelatedToEngagement.Id);     
       
  //Create Account Folders in SharePoint    
  AccountSharepointFolderCreation.CreateAccountFolder(accIDsTobeRelatedToEngagement);
    
    If (customerAccTobeRelatedToEngagement.SharePoint_Folder_Id__c<>'')  {
      System.debug('Account SharePoint FolderID: ' + customerAccTobeRelatedToEngagement.SharePoint_Folder_Id__c);}
    else if (customerAccTobeRelatedToEngagement.Opportunity_Sharepoint_Folder_Id__c<>'')  {
        System.debug('Account Opp SharePoint FolderID: ' + customerAccTobeRelatedToEngagement.Opportunity_Sharepoint_Folder_Id__c);}
    else if (customerAccTobeRelatedToEngagement.Contracts_Sharepoint_Folder_Id__c<>'')  {
        System.debug('Account Contract SharePoint FolderID: ' + customerAccTobeRelatedToEngagement.Contracts_Sharepoint_Folder_Id__c);}
    else if (customerAccTobeRelatedToEngagement.Contracts_Sharepoint_Folder_Id__c<>'')  {
        System.debug('Account Contract SharePoint FolderID: ' + customerAccTobeRelatedToEngagement.Contracts_Sharepoint_Folder_Id__c); }
    else if (customerAccTobeRelatedToEngagement.Finance_Sharepoint_Folder_Id__c<>'')  {
        System.debug('Account Finance SharePoint FolderID: ' + customerAccTobeRelatedToEngagement.Finance_Sharepoint_Folder_Id__c); }
    else if (customerAccTobeRelatedToEngagement.Archive_Sharepoint_Folder_Id__c<>'') {
        System.debug('Account Archive SharePoint FolderID: ' + customerAccTobeRelatedToEngagement.Archive_Sharepoint_Folder_Id__c);}
  
  //Create Opportunity to be related to Engagement
  Opportunity newCustomerOppTestCustomerOppTobeRelatedtoEngagement = new Opportunity();  
  newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Name='TestCustomerOppTobeRelatedOpportunity';
  newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Type='New Sale';    
  newCustomerOppTestCustomerOppTobeRelatedtoEngagement.CloseDate=System.today();
  newCustomerOppTestCustomerOppTobeRelatedtoEngagement.StageName='Discovery & Qualification';
  newCustomerOppTestCustomerOppTobeRelatedtoEngagement.AccountId=customerAccTobeRelatedToEngagement.Id;
  insert newCustomerOppTestCustomerOppTobeRelatedtoEngagement;

    
  Opportunity newProspectOppTobeRelatedtoEngagement = new Opportunity();     
  newProspectOppTobeRelatedtoEngagement.Name='TestCustomerOpp';
  newProspectOppTobeRelatedtoEngagement.Type='New Sale';
  newProspectOppTobeRelatedtoEngagement.CloseDate=System.today();
  newProspectOppTobeRelatedtoEngagement.StageName='Discovery & Qualification';
  newProspectOppTobeRelatedtoEngagement.AccountId=prospectAccTobeRelatedToEngagement.Id;
  insert newProspectOppTobeRelatedtoEngagement;
     
    
  List<String> oppIDsToBeRelatedToEngagement = new List<String>();
  oppIDsToBeRelatedToEngagement.add(newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Id); 
  oppIDsToBeRelatedToEngagement.add(newProspectOppTobeRelatedtoEngagement.Id);     
 

  //Create Opportunity Folders in SharePoint
  OpportunitySharepointFolderCreation.CreateOpportunityFolder(oppIDsToBeRelatedToEngagement);

    If (newCustomerOppTestCustomerOppTobeRelatedtoEngagement.fileforcem1__Sharepoint_Folder_Id__c <>'')  {
      System.debug('Opp SharePoint FolderID: ' + newCustomerOppTestCustomerOppTobeRelatedtoEngagement.fileforcem1__Sharepoint_Folder_Id__c);}
    else if (newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Quotes_Sharepoint_Folder_Id__c<>'')  {
        System.debug('Opp Quotes SharePoint FolderID: ' + newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Quotes_Sharepoint_Folder_Id__c);}
    else if (newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Proposal_Sharepoint_Folder_Id__c<>'')  {
        System.debug('Opp Proposals SharePoint FolderID: ' + newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Proposal_Sharepoint_Folder_Id__c);}
    else if (newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Signed_Contracts_Sharepoint_Folder_ID__c<>'')  {
        System.debug('Opp Signed Contracts SharePoint FolderID: ' + newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Signed_Contracts_Sharepoint_Folder_ID__c); }
    else if (newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Customer_Invoices_Folder_Id__c<>'')  {
        System.debug('Opp Customer Invoices SharePoint FolderID: ' + newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Customer_Invoices_Folder_Id__c); }
    else if (newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Vendor_Sharepoint_Folder_Id__c<>'') {
        System.debug('Opp Vendor SharePoint FolderID: ' + newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Vendor_Sharepoint_Folder_Id__c);}
  
            
  //Create Engagement 
  pse__Proj__c NewEngagement1 = new pse__Proj__c();
  NewEngagement1.RecordTypeId = '0124Y000000502fQAA'; //Client
  NewEngagement1.Name='TestEngagementCreatedForMilestone';
  NewEngagement1.Account__c = customerAccTobeRelatedToEngagement.Id;
  NewEngagement1.Opportunity__c = newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Id;
 // Query the regions to be used and assigned
   List<pse__Region__c> regionresultsforvic  = [SELECT Name FROM pse__Region__c WHERE Name = 'Victoria' Limit 1]; 
  if (regionresultsforvic.size()>0)
{
   NewEngagement1.pse__Region__c = regionresultsforvic[11].Name;

}
  NewEngagement1.pse__Start_Date__c = System.today();
  NewEngagement1.pse__End_Date__c = System.today();  
  NewEngagement1.pse__Practice__c = 'aDt4Y000000000aSAA';
    
  System.debug('practice name inserted' + NewEngagement1.pse__Practice__c);
  insert NewEngagement1;
 
  //Create Engagement2 
  pse__Proj__c NewEngagement2 = new pse__Proj__c();
  NewEngagement2.RecordTypeId = '0124Y000000502BQAQ'; // Global // Internal 0124Y000000502aQAA // 0124Y000000502kQAA
  NewEngagement2.Name='TestEngagementCreatedForMilestone2';
  NewEngagement2.Account__c = customerAccTobeRelatedToEngagement.Id;
  NewEngagement2.Opportunity__c = newCustomerOppTestCustomerOppTobeRelatedtoEngagement.Id;
 // Query the regions to be used and assigned
   List<pse__Region__c> regionresultsforqld  = [SELECT Name FROM pse__Region__c WHERE Name = 'Queensland' Limit 1]; 
  if (regionresultsforqld.size()>0)
{
   NewEngagement1.pse__Region__c = regionresultsforqld[6].Name;

}
  NewEngagement1.pse__Start_Date__c = System.today();
  NewEngagement1.pse__End_Date__c = System.today();  
  NewEngagement1.pse__Practice__c = 'aDt4Y000000000aSAA';
    
  System.debug('practice name inserted' + NewEngagement2.pse__Practice__c);
  insert NewEngagement2;
 
      
  List<String> milestoneIDs = new List<String>();
  milestoneIDs.add(NewEngagement1.Id); 
  milestoneIDs.add(NewEngagement2.Id); 
 /* milestoneIDs.add(NewEngagement3.Id); 
  milestoneIDs.add(NewEngagement4.Id); */
     
  //Create Milestone Folders in SharePoint
  AutomaticmilestonesharepointFolder.CreateMilestoneFolder(milestoneIDs);    

 If (NewEngagement1.sharepoint_folder_id__c <>'')  {
      System.debug('Milestone SharePoint FolderID: ' + NewEngagement1.sharepoint_folder_id__c);}
 If (NewEngagement2.sharepoint_folder_id__c <>'')  {
      System.debug('Milestone SharePoint FolderID: ' + NewEngagement2.sharepoint_folder_id__c);}
 /* If (NewEngagement3.sharepoint_folder_id__c <>'')  {
      System.debug('Milestone SharePoint FolderID: ' + NewEngagement3.sharepoint_folder_id__c);}
  If (NewEngagement4.sharepoint_folder_id__c <>'')  {
      System.debug('Milestone SharePoint FolderID: ' + NewEngagement4.sharepoint_folder_id__c);} */
 
  Test.stopTest();
  }
 
Chaudhary AdityaChaudhary Aditya
Some pointers: 

In the editor of choice - Check for lines of code not covered(Code Coverage).  They are highlighted in a different color in Developer Console and VS Code. 
Run test cases for these lines of code.  ( may have to write new test cases or run existing test cases with different parameters)