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
Jyosi jyosiJyosi jyosi 

Trigger Help on case object

Hello Everyone,
I have a custom object which is related to case via lookup relation.
when case is created the status will be "New" .if the custom object has alteast one records,when the user should be able to change the status to "Ready ".if he tries to chnage with out any record it should prompt the error .

Thanks

Regards,
Jyo

 
Donald BlayDonald Blay
First you need to create a trigger for Before Updat on the custom object
In the trigger you will need construct a list of all the IDs for the items being updated from the trigger.new field.
Do a SOQL select using a Count() and get the number of children for each record and put them into a map (map(Id, Integer) with the key being the ID of the record, and the Integer being the count of children).  
Then loop through all the records being updated (trigger.new).  Using the Id of the current record, look it up in the map. If the count value is 0 then set set the record's AddError method with your error mesage.  By the error field having a value, that inhibits the update and the error message will buble up to the UI
currentRecord.addError('This record has no children');