You need to sign in to do that
Don't have an account?

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());
}
}
}
}.
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());
}
}
}
}.
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:-
Test class:- 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.
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.
2n
the last two images showing the code which is not covered. please help me to cover this.
thanks.
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.
Otherwise you can write update instead of insert in Line no. 47 from your code.
Thanks.
We cannot insert call because it is already inserted.
Please remove this insert call or you can write update call in place of the insert call.
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.
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.