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
Priya MishraPriya Mishra 

Test class coverage for the case creation logic

Hello all,

Need help to complete the test  class for the below class it has completed 52% now covering the create case but it is not covering.
please help me to complete this. 

Thanks in advance

Apex class:



public class CallDetailsController_LWCApex {
    
    @AuraEnabled(cacheable = true)
    public static Contact findContacts(String contactId) {
        return [SELECT FirstName,LastName,UserName__c,BusinessName__c,EIN__c,MobilePhone,Phone,Country__c,State__c,
               Email,Caller_Name__c,Caller_Phone__c FROM contact WHERE id =: contactId LIMIT 1];
    }
    
    @AuraEnabled
    public static void saveRequest(Call_Log__c Call,String contactId) {
        if(Call != NULL) {
            Contact newCon;
            if(Call.Select_User_Type__c == 'New User') {   
                 newCon = new Contact();
                newCon.FirstName = Call.First_Name__c;
                newCon.LastName = Call.Last_Name__c;
                newCon.UserName__c = Call.UserNameNew__c;
                newCon.BusinessName__c = Call.BusinessName__c;
                newCon.EIN__c = Call.EIN__c;
                newCon.MobilePhone = Call.CellPhone__c;
                newCon.Phone = Call.Phone_number__c;
                newCon.Email=Call.Email__c;
                newCon.Caller_Name__c=Call.Caller_Name_new__c;
                newCon.Caller_Phone__c=Call.Caller_Phone_new__c;
                newCon.Country__c = Call.Country__c;
                newCon.State__c = Call.State__c;
                try {
                     insert newCon;
                    
                }Catch(Exception e){
                     throw new AuraHandledException(e.getMessage());
                }      
            }
            
            //Create Call log;
            String callRecordTypeId  = Schema.getGlobalDescribe().get('Call_Log__c').getDescribe().getRecordTypeInfosByName().get(Call.Call_Type__c).getRecordTypeId();
            Call.RecordTypeId = callRecordTypeId;
            Call.Status__c ='New'; 
            
           /* if(String.isNotBlank(contactId) && Call.Select_User_Type__c != 'New User'){
                Call.Id = [SELECT Id FROM Call_Log__c WHERE Caller_Phone__c =:contactId LIMIT 1].Id;
            }
            else*/ if(newCon != NULL && newCon.Id != NULL) {
                 Call.Caller_Phone__c = newCon.Id;                
            }      
            try {
                insert Call;
            }Catch(Exception e){
                throw new AuraHandledException(e.getMessage());
            } 
            //Create Case
            Case newCase = new Case();        
            if(Call.Is_Follow_up_Required__c  == 'Yes') 
            {
                newCase.Status    = 'New';
                newCase.Subject = 'A new Inbound Case has been created';
                
                if(Call.Call_Type__c == 'Inbound')
                {
                    newCase.Subject = 'A new Inbound Case has been created';
                }
                else if (Call.Call_Type__c == 'Outbound')
                {
                    newCase.Subject = 'A new Outbound Case has been created';
                }
                
                else if (Call.Call_Type__c == 'Bulk Followup Upload')
                {
                    newCase.Subject = 'A new Followup Case has been created';
                }           
                else
                {
                    newCase.Subject = 'A new Follow Up Case has been created';
                }
            }        
            else{            
                newCase.Status = 'closed';
                newCase.Subject = 'A Case has been created';
            }        
            newCase.Assign_too__c= Call.Assign_too__c;
            newCase.Form_Type__c = Call.Form_Type__c;
            newCase.Purpose_of_the_Call__c = Call.Purpose_of_the_Call__c;
            newCase.Call_Deposition__c = Call.Call_Deposition__c;
            newCase.Remark_Comment__c = Call.Remark_Comment__c;
            newCase.Tax_Products__c = Call.Id;
            newCase.Created_By__c = Call.CreatedById;
            newCase.UserId__c = Call.UserNameNew__c;
            newCase.Phone__c =Call.Phone_number__c;
            if(newCon != NULL && newCon.Id != NULL)
                newCase.ContactId = newCon.Id;
            //   newCase.Ticket_Number__c = Call.Ticket_Number__c;
            try {
                insert newCase;
            }Catch(Exception e){
                throw new AuraHandledException(e.getMessage());
            } 
        }
    }
}.
Suraj Tripathi 47Suraj Tripathi 47
Hi priya,

User-added image

You don't need to insert the Call_Log__c (call) because you have already inserted.

You can take reference from this below code.
apex class:-
public class CallDetailsController_LWCApex {
    
    @AuraEnabled(cacheable = true)
    public static Contact findContacts(String contactId) {
        return [SELECT FirstName,LastName,UserName__c,BusinessName__c,EIN__c,MobilePhone,Phone,Country__c,State__c,
                Email,Caller_Name__c,Caller_Phone__c FROM contact WHERE id =: contactId LIMIT 1];
    }
    
    @AuraEnabled
    public static void saveRequest(Call_Log__c Call,String contactId) {
        if(Call != NULL) {
            Contact newCon;
            if(Call.Select_User_Type__c == 'New User') {   
                newCon = new Contact();
                newCon.FirstName = Call.First_Name__c;
                newCon.LastName = Call.Last_Name__c;
                newCon.UserName__c = Call.UserNameNew__c;
                newCon.BusinessName__c = Call.BusinessName__c;
                newCon.EIN__c = Call.EIN__c;
                newCon.MobilePhone = Call.CellPhone__c;
                newCon.Phone = Call.Phone_number__c;
                newCon.Email=Call.Email__c;
                newCon.Caller_Name__c=Call.Caller_Name_new__c;
                newCon.Caller_Phone__c=Call.Caller_Phone_new__c;
                newCon.Country__c = Call.Country__c;
                newCon.State__c = Call.State__c;
                try {
                    insert newCon;
                    
                }Catch(Exception e){
                    throw new AuraHandledException(e.getMessage());
                }      
            }
            
            String callRecordTypeId  = Schema.getGlobalDescribe().get('Call_Log__c').getDescribe().getRecordTypeInfosByName().get(Call.Call_Type__c).getRecordTypeId();
            Call.RecordTypeId = callRecordTypeId;
            Call.Status__c ='New'; 
            if(newCon != NULL && newCon.Id != NULL) {
                Call.Caller_Phone__c = newCon.Id;                
            }      
          
            Case newCase = new Case();        
            if(Call.Is_Follow_up_Required__c  == 'Yes') 
            {
                newCase.Status    = 'New';
                newCase.Subject = 'A new Inbound Case has been created';
                
                if(Call.Call_Type__c == 'Inbound')
                {
                    newCase.Subject = 'A new Inbound Case has been created';
                }
                else if (Call.Call_Type__c == 'Outbound')
                {
                    newCase.Subject = 'A new Outbound Case has been created';
                }
                
                else if (Call.Call_Type__c == 'Bulk Followup Upload')
                {
                    newCase.Subject = 'A new Followup Case has been created';
                }           
                else
                {
                    newCase.Subject = 'A new Follow Up Case has been created';
                }
            }        
            else{            
                newCase.Status = 'closed';
                newCase.Subject = 'A Case has been created';
            }        
            newCase.Assign_too__c= Call.Assign_too__c;
            newCase.Form_Type__c = Call.Form_Type__c;
            newCase.Purpose_of_the_Call__c = Call.Purpose_of_the_Call__c;
            newCase.Call_Deposition__c = Call.Call_Deposition__c;
            newCase.Remark_Comment__c = Call.Remark_Comment__c;
            newCase.Tax_Products__c = Call.Id;
            newCase.Created_By__c = Call.CreatedById;
            newCase.UserId__c = Call.UserNameNew__c;
            newCase.Phone__c =Call.Phone_number__c;
            if(newCon != NULL && newCon.Id != NULL)
                newCase.ContactId = newCon.Id;
            //   newCase.Ticket_Number__c = Call.Ticket_Number__c;
            try {
                insert newCase;
            }Catch(Exception e){
                throw new AuraHandledException(e.getMessage());
            } 
        }
    }
}

Test class:-
@isTest
public class CallDetailsController_LWCApex_Test {
    @istest static void test(){
        contact con=new contact();
        con.FirstName='Suraj';
        con.LastName='Tripathi';
        con.UserName__c='username';
        con.BusinessName__c='BusinessName';
        con.MobilePhone='9876543234';
        con.Phone='9876544455';
        con.Country__c='India';
        con.State__c='Uttar Pradesh';
        con.Email='xyz@gmail.com';
        con.Caller_Name__c='caller name';
        con.Caller_Phone__c='876545468';
        insert con;
        
        Call_Log__c call= new Call_Log__c();
        call.Select_User_Type__c='New User';
        call.First_Name__c='first name';
        call.Last_Name__c='lastname';
        call.UserNameNew__c='UserNameNew';
        call.BusinessName__c='BusinessName';
        call.EIN__c='EIN';
        call.CellPhone__c='876543';
        call.Phone_number__c='987675432';
        call.Email__c='abc@gmail.com';
        call.Caller_Name_new__c='Caller Name';
        call.Caller_Phone_new__c='8978675432';
        call.Country__c='India';
        call.State__c='Haryana';
        Call.Is_Follow_up_Required__c='Yes';
        Call.Call_Type__c= 'Outbound';
        call.Assign_too__c='Assign too';
        call.Form_Type__c='form type';
        call.Purpose_of_the_Call__c='purpose';
        call.Call_Deposition__c='description';
        call.Remark_Comment__c='remark comment';
        insert call;
            
        test.startTest();
        CallDetailsController_LWCApex.findContacts(con.Id);
        CallDetailsController_LWCApex.saveRequest(call,con.Id);
        test.stopTest();
    }
}
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
 
Priya MishraPriya Mishra
Thanks for the reply.

Actully this code is not covering any method.


i am attaching the image you can see the uppar part of the code is covring but the case creation is not covering.

2nUser-added imageUser-added imageUser-added image



the last two images showing the code which is not covered. please help me to cover this.

thanks.
Suraj Tripathi 47Suraj Tripathi 47
Hi Priya,

You don't need to insert the Call_Log__c (call) [line No. 47] because you have already inserted. Please remove the line 47 from your code.
In case you find any other issue please mention. 
Suraj Tripathi 47Suraj Tripathi 47
Hi Priya,

Otherwise you can write update instead of insert in Line no. 47 from your code.

Thanks.
Priya MishraPriya Mishra
you mean to say in the test class i should remove insert call or how actully i have tried to your code also but it is not covering method is not getting passed.
Suraj Tripathi 47Suraj Tripathi 47
Don't remove the insert call from the test class. Remove this insert call from your main class or you can write the update call in place of the insert call.
We cannot insert call because it is already inserted.

User-added image
Please remove this insert call or you can write update call in place of the insert call.
Priya MishraPriya Mishra
Thanks for your reply. But when I am removing the insert call log or else giving like update call it is throwing  error while creating the record that means functionality  is not working as expected. 
Priya MishraPriya Mishra
I have seen after giving update call code is getting covered 78 percent  but that is not giving us the expected  functionality if we change to update call or remove call.
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Priya,

When a record is already inserted, then we cannot perform an insert operation on that record but yes you can write update or delete DML operation.

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
Priya MishraPriya Mishra
No it is not working as expected.
Suraj Tripathi 47Suraj Tripathi 47
Hi priya

Please write the update call in place of the insert call afterthat you check it is working as expected or not. if it still does not work then tell me what is not working.