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
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12 

System.TypeException: Invalid id value for this SObject type Test classe error

Hi all,

I written one trigger and test class getting this error. 
System.TypeException: Invalid id value for this SObject type

Trigger:-
----------

trigger EscalateCase on Escalation__c (after update) {
IF(Trigger.size > 1){return;}
IF(Trigger.new[0].Reason_For_Escalation__c == null || Trigger.new[0].Escalation_Comments__c == null || Trigger.new[0].Escalate_To_Management__c == false || Trigger.new[0].Case__c == null){return;}
Case c = new case(ID = trigger.new[0].Case__c, Escalate_To_Management__c = trigger.new[0].Escalate_To_Management__c, Reason_For_Escalation__c = trigger.new[0].Reason_For_Escalation__c, Escalation_Comments__c = trigger.new[0].Escalation_Comments__c);
update c;
}

Test class:-
--------------

@isTest
private class EscalateCaseTestClass{
@isTest static void Esc(){

Account a = new Account(Name='ContractBookingPeriodProcessingTest',Geography__c='TestGeography',
        Territory_Id__c='TestTerritory',
            Territory_Overlay__c='TestTerritoryOverlay',Renewals_Team__c='TestRenewalsTeam',
            Renewals_Account_Manager__c='TestRenewalsAccountManager');
        insert a;
        Contract c = new Contract(AccountId=a.id,Number_of_Booking_Periods_Records__c = 0, Number_of_Booking_Periods__c = 1,
                                    Status='Create', Finance_Charges__c ='0.5%',
                                    Contract_Term_Start_Date__c=date.valueOf('2013-01-01'),
                                    Contract_Term_End_Date__c=date.valueOf('2099-12-31')                    
                                    );
        insert c;

Case case1 = new case(Id=a.id,Escalate_To_Management__c =true,Reason_For_Escalation__c = 'TestReason' ,Escalation_Comments__c = 'TestEscalation' );
insert case1;

case1.Reason_For_Escalation__c = 'TestReasonForEscalation';
case1.Escalation_Comments__c = 'TestEscalationComments';
update case1;
}
}

Thanks
kumar
Best Answer chosen by kumar.fdc81.3902978579608325E12
Subhash GarhwalSubhash Garhwal
Hi Kumar,

Check filter criteria on case's account Field
To Check filter criteria follow these steps :
Setpup -> Case -> Fields -> Account Name (Field) -> Lookup Filter
According to this condition put data in account record than assign it to case.

All Answers

Hargobind_SinghHargobind_Singh
In your test class, where you are creating a new case, seems like you are trying to assign Account ID (since "a" is Account object) to Case ID. You might want to change it to: 

Case case1 = new case(AccountId=a.id,Escalate_To_Management__c =true,Reason_For_Escalation__c = 'TestReason' ,Escalation_Comments__c = 'TestEscalation' );






Subhash GarhwalSubhash Garhwal
Hi

You are putting account id in case Id that is invalid.Also to cover your trigger you need to create Escalation__c record and than update it.
Like :
@isTest
private class EscalateCaseTestClass{
@isTest static void Esc(){

Account a = new Account(Name='ContractBookingPeriodProcessingTest',Geography__c='TestGeography',
        Territory_Id__c='TestTerritory',
            Territory_Overlay__c='TestTerritoryOverlay',Renewals_Team__c='TestRenewalsTeam',
            Renewals_Account_Manager__c='TestRenewalsAccountManager');
        insert a;
        Contract c = new Contract(AccountId=a.id,Number_of_Booking_Periods_Records__c = 0, Number_of_Booking_Periods__c = 1,
                                    Status='Create', Finance_Charges__c ='0.5%',
                                    Contract_Term_Start_Date__c=date.valueOf('2013-01-01'),
                                    Contract_Term_End_Date__c=date.valueOf('2099-12-31')                    
                                    );
        insert c;

Case case1 = new case();
insert case1;


//Inset Escalation__c
Escalation__c esc = new Escalation__c(Case__c = case1.Id, Reason_For_Escalation__c = 'Test', Escalation_Comments__c = 'Test', Escalate_To_Management__c = true);

insert esc;

//update 
update esc;
}
}

Regards
Subhash
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Hi subash,

i written as per an=bove code i am getting this error. 
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, The Account you are searching for does not exist or is not a current customer.: [AccountId]

Test Class:-
@isTest
private class EscalateCaseTestClass{
@isTest static void Esc(){

Account a = new Account(Name='PipelineChangeTest',Geography__c='TestGeography',Territory_Id__c='TestTerritory',
            Territory_Overlay__c='TestTerritoryOverlay',Renewals_Team__c='TestRenewalsTeam',Renewals_Account_Manager__c='TestRenewalsAccountManager');
        insert a;
       
Contact c = new Contact(AccountId=a.id,LastName='Test',Job_Function__c='TestJobFunction',Department__c='TestDepartment');
        insert c;    
  
Case case1 = new case(AccountId=a.id,ContactId=c.id);
insert case1;

//Inset Escalation__c
Escalation__c esc = new Escalation__c(Case__c = case1.Id, Reason_For_Escalation__c = 'Test', Escalation_Comments__c = 'Test', Escalate_To_Management__c = true);

insert esc;

//update
update esc;
}
}

Thanks 
kumar
Subhash GarhwalSubhash Garhwal
On which line you are getting error.
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Hi Subash,

Class.EscalateCaseTestClass.Esc: line 13, column 1

Thanks
kumar
Subhash GarhwalSubhash Garhwal
Can you check case validation rules if their is any on accountId field or filter criteria on accountId field on case and according to validation condtion insert account record.Than assign it to accountId field on case.

Thanks
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Hi Subash,
I Cecked Validation rules, Not available any Validation based on ID.

Thanks
kumar
Subhash GarhwalSubhash Garhwal
Hi Kumar,

Check filter criteria on case's account Field
To Check filter criteria follow these steps :
Setpup -> Case -> Fields -> Account Name (Field) -> Lookup Filter
According to this condition put data in account record than assign it to case.

This was selected as the best answer
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Thanks Subash, Lookup filter is the problem.  Thanks for your help.