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
KyoKyo 

Error TestClass

System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

 

rigger trgLookupSLAValues on Case (after insert) {

    for(Case caseArgs : Trigger.new) {
    
        List<Case_Master__c> caseMasters = [select Text_Note__c,Case_Escalation_To__c,Queue_Name__c, 
        Type__c,Priority__c, Function__c, SLA_Time_Hours__c, Status__c from Case_Master__c 
        where Case_Category__c =: caseArgs.Case_Category__c and Sub_Category__c =:caseArgs.Sub_Category__c 
        and Description__c =: caseArgs.Description__c and Customer_Class__c =: caseArgs.Customer_Class__c ];

        if(caseMasters.IsEmpty()) {
            RelationMgrException ex = new RelationMgrException('Cannot find Case Master record');
            throw ex;
        }
        else {
        
        
        Case_Master__c caseMasterTemp = caseMasters.get(0);
        
        Case caseNew = new Case(Id=caseArgs.Id);
        caseNew.OwnerId = caseMasterTemp.Case_Escalation_To__c;
        caseNew.Function__c = caseMasterTemp.Function__c;
        caseNew.Priority = caseMasterTemp.Priority__c;
        caseNew.SLATimeHours__c = caseMasterTemp.SLA_Time_Hours__c;
        caseNew.Text_Note__c = caseMasterTemp.Text_Note__c;
        caseNew.Status = caseMasterTemp.Status__c;
        caseNew.Type = caseMasterTemp.Type__c;
        
        update caseNew ;
        }
        
      
    }
  
}

 



@isTest 
private class TesttrgLookupSLAValues{
    static testMethod void TestMethodtrgLookupSLAValues() {
      
     Account Acc = new Account();
        Acc.Name = 'test1'; 
        Acc.AccountNumber = '000001';
        Acc.CustomerPriority__c = 'Platinum';       
        insert Acc;
        

    Case_Master__c CM = new Case_Master__c();
         CM.Text_Note__c = ' test ';
         CM.Case_Category__c ='Z - Customer Solution';
         CM.Sub_Category__c ='CSD - Price and Policy';
         CM.Description__c ='Complaint about price and policy';
         CM.Customer_Class__c ='Complaint about price and policy';       
    insert CM;
    
     Case ca = new Case();
        ca.AccountID = Acc.Id;
        ca.Origin = 'Web';
        ca.Status = 'Open 1st Level';
        ca.Type = 'Inquiry';
        ca.Multi_Account__c = true;
        ca.Case_Category__c = 'Z - Customer Solution';
        ca.Sub_Category__c = 'CSD - Open New Ship to';
        ca.Description__c = 'Request add new ship to';
    update ca;
    
    }

    
}

 

Thank you so much.

Shashikant SharmaShashikant Sharma

You need to insert case record as you are testing a after insert trigger on Case object

 

Your test method should be

@isTest 
private class TesttrgLookupSLAValues{
    static testMethod void TestMethodtrgLookupSLAValues() {
      
     Account Acc = new Account();
        Acc.Name = 'test1'; 
        Acc.AccountNumber = '000001';
        Acc.CustomerPriority__c = 'Platinum';       
        insert Acc;
        

    Case_Master__c CM = new Case_Master__c();
         CM.Text_Note__c = ' test ';
         CM.Case_Category__c ='Z - Customer Solution';
         CM.Sub_Category__c ='CSD - Price and Policy';
         CM.Description__c ='Complaint about price and policy';
         CM.Customer_Class__c ='Complaint about price and policy';       
    insert CM;
    
     Case ca = new Case();
        ca.AccountID = Acc.Id;
        ca.Origin = 'Web';
        ca.Status = 'Open 1st Level';
        ca.Type = 'Inquiry';
        ca.Multi_Account__c = true;
        ca.Case_Category__c = 'Z - Customer Solution';
        ca.Sub_Category__c = 'CSD - Open New Ship to';
        ca.Description__c = 'Request add new ship to';
    insert ca;
    
    }

    
}

 i Just update last statement of your test emthod "update ca" to "insert ca" ,

 

The reason you get error was you were updating ca, but for update we need id of record which we wnat to update but in your case you initialized Case record as a new one

Case ca = new Case();

KyoKyo

Thank you i'm change but new error

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, trgLookupSLAValues: execution of AfterInsert caused by: RelationMgrException: Cannot find Case Master record Trigger.trgLookupSLAValues: line 11, column 39: []

 

public class RelationMgrException extends Exception {

}

 


Shashikant SharmaShashikant Sharma

Nothing to worry this error is negative testing case and shows your triggerr is working correctly , you just need to wite a negative test in try-catch blocks, In your test Sub_Category__c field for case was not equal to case master's Sub_Category__c thats why it showed exception.

 

@isTest 
private class TesttrgLookupSLAValues{
//positive case
static testMethod void TestMethodtrgLookupSLAValuesPos() {
      
     Account Acc = new Account();
        Acc.Name = 'test1'; 
        Acc.AccountNumber = '000001';
        Acc.CustomerPriority__c = 'Platinum';       
        insert Acc;
        

    Case_Master__c CM = new Case_Master__c();
         CM.Text_Note__c = ' test ';
         CM.Case_Category__c ='Z - Customer Solution';
         CM.Sub_Category__c ='CSD - Price and Policy';
         CM.Description__c ='Complaint about price and policy';
         CM.Customer_Class__c ='Complaint about price and policy';       
    insert CM;
    
     Case ca = new Case();
        ca.AccountID = Acc.Id;
        ca.Origin = 'Web';
        ca.Status = 'Open 1st Level';
        ca.Type = 'Inquiry';
        ca.Multi_Account__c = true;
        ca.Case_Category__c = CM.Case_Category__c;
        ca.Sub_Category__c = CM.Sub_Category__c;
        ca.Description__c = 'Request add new ship to';
    
insert ca;
}



//Negative Test
static testMethod void TestMethodtrgLookupSLAValuesNeg() {
      
     Account Acc = new Account();
        Acc.Name = 'test1'; 
        Acc.AccountNumber = '000001';
        Acc.CustomerPriority__c = 'Platinum';       
        insert Acc;
        

    Case_Master__c CM = new Case_Master__c();
         CM.Text_Note__c = ' test ';
         CM.Case_Category__c ='Z - Customer Solution';
         CM.Sub_Category__c ='CSD - Price and Policy';
         CM.Description__c ='Complaint about price and policy';
         CM.Customer_Class__c ='Complaint about price and policy';       
    insert CM;
    
     Case ca = new Case();
        ca.AccountID = Acc.Id;
        ca.Origin = 'Web';
        ca.Status = 'Open 1st Level';
        ca.Type = 'Inquiry';
        ca.Multi_Account__c = true;
        ca.Case_Category__c = 'Z - Customer Solution';
        ca.Sub_Category__c = 'CSD - Open New Ship to';
        ca.Description__c = 'Request add new ship to';
try
{    
insert ca;
 }
Catch(Exception e)
{
system.assert(e.getMessage().Contains('Cannot find Case Master record');
}   
    }

    
}