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
mallikammallikam 

problem with trigger

I wrote a trigger after update for child cases. If all the child cases are closed, the Inactive field of the ContactId reference of the parent case will be set to true...I am getting an exception with the following code, when I try to close the last child case. Its then trying to evaluate the innermost if condition and hitting the exception..

trigger childsClosed on Case (after update) { Case b = new Case(); if (Trigger.New[0].Status == 'Closed') { if (Trigger.New[0].ParentId != null) { list<Case> a = [Select Id from Case where Id = :Trigger.New[0].ParentId and Subject = 'Delete Network ID or Reset password' limit 1]; if(a.size() != 0) { b = a[0];} if (b.Id != null) { integer childCases = [Select count() from Case where ParentId = :b.Id]; integer closed = [Select count() from Case where ParentId = :b.id and Status = 'Closed']; if (closed == childCases) { b.Contact.Inactive__c = true; } } } } }

And I am getting the exception that says:

 

Apex script unhandled trigger exception by user/organization: 005700000018KNh/00D70000000JLCa

 

childsClosed: execution of AfterUpdate

 

caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Case.Contact

 

Trigger.childsClosed: line 12, column 6

 

Best Answer chosen by Admin (Salesforce Developers) 
prabhutumprabhutum

Mallikam,

 

1. Replace b.Contact.Inactive__c = true; with the following,

 

Contact c = [SELECT Inactive__c FROM contact WHERE id = :Trigger.Old[0].ContactId];

c.Inactive__c = TRUE;

UPDATE c;

 

 

The above should take care of the error.

 

2. Instead of running two SOQL to check if all the child calls are closed, you may want to just check if the count of any Open Child calls == 0. Just a thought.... :o)

 

Message Edited by prabhutum on 07-23-2009 09:34 AM

All Answers

prabhutumprabhutum

Mallikam,

 

1. Replace b.Contact.Inactive__c = true; with the following,

 

Contact c = [SELECT Inactive__c FROM contact WHERE id = :Trigger.Old[0].ContactId];

c.Inactive__c = TRUE;

UPDATE c;

 

 

The above should take care of the error.

 

2. Instead of running two SOQL to check if all the child calls are closed, you may want to just check if the count of any Open Child calls == 0. Just a thought.... :o)

 

Message Edited by prabhutum on 07-23-2009 09:34 AM
This was selected as the best answer
mallikammallikam

Thanks a lot!! It works! You know what, I tried almost the same thing but because I was looking for Trigger.new[0].ContactId and since I am not updating, it did not work. Also, I changed the code to below(I am a database novice, so I am unable to think any better if I am doing any database stuff!)

 

integer open = [Select count() from Case where ParentId = :b.id and Status != 'Closed'];

if (open == 0) {

Contact c = [SELECT Inactive__c FROM contact WHERE id = :Trigger.Old[0].ContactId];

c.Inactive__c = true;

update c;

}