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
GYAN ANDRUSGYAN ANDRUS 

I want to write an trigger,The condition is" if the parent case records should not be close,when its having a child case are open".It will show an error when i close the parent case,

trigger ParentChildcase on Case (before update) {
List<Id> parentCaseIds = new List<Id>();

Map<Id,Case> caseMap =new Map<Id,Case> ([select id  from Case where Id in:parentCaseIds]);
/*List<case> OpenChildCases = [select Id from case Id:Trigger.NewMap.Keyset() and status != 'Closed']
Set<String> ChildCaseSet = New Set<string>();
    for(case temp:OpenChildCases){
        ChildCaseSet.add(temp.Parent.Id);
    }
    for(case caseinst:Trigger.New){
      if(ChildCaseSet.contains(caseinst.Id) && caseinst.Status = 'Closed'){
        caseinst.addError('Unclosed Child Records are Present');
     }   
   }*/
}
dev_sfdc1dev_sfdc1
Hi Hema,

Try this..

trigger parentupdate on Case (before update) 
{

    for(Case c : Trigger.New)
    {
        if(c.Status == 'Closed' )
        {
            String childid = c.Id;
           Case cs1 = [Select id,ParentId,Status from Case where ParentId =: childid ];
             
                if(cs1.Status == 'Open')
                {  
                 c.adderror('You cannot update parent record');
                }
            
        }
       
    }
}
SaranSaran
Hi hema,

Use the below trigger.

trigger parentCase on case(before update)
{
    set<id> openCaseIDs = new set<id>();
    
    for(case caseList = [select id, status from case where ParentId in: trigger.newMap.keyset() AND status != 'Closed'])
    {
        openCaseIDs.add(caseList.ParentId);
    }
    
    for(case newCase : trigger.new)
    {
        if(newCase.status != trigger.oldMap.get(newCase.id).status && newCase.status == 'Closed' && openCaseIDs.contains(newCase.id))
        {
            newCase.addError('The associated child cases are open');
        }
    }
}

Hope this might solve your problem.

If this solved your solutions. Please mark it as solved.

Thanks,
saraz


 
SaranSaran
Hi dev_sfdc1,

FYI 

The trigger you have written is correct but in the case of bulkification the query limit will get exceed.
because of the query inside the for loop.

Make sure you dont use like this in future. 

I will be helpful for you in future.

Thanks,
saravanan
 
dev_sfdc1dev_sfdc1
Hi Saraz,

Thanks for your suggestion. Can you look my code now?If anything wrong,please correct me.

trigger parentupdate on Case (before update) 
{
    Id newid = Trigger.New[0].Id;
    Case lstc = [Select id,ParentId,Status from Case where ParentId =:newid];
    for(Case c : Trigger.New)
    {
        if(c.Status == 'Closed' && lstc.Status == 'Open')
        {
          c.adderror('You cannot update parent record');
        }
    }
   
}
SaranSaran
If i am trying to update 100 records using date loader.
Then it will work only for the first record for remaining records it will not work.

Because you have taken the case id of zeroth index. Dont take records based on the index.

Your query will return even the records with status closed because you have not mentioned where condition in the query. Try to use where condition in the query.

On that case you have to process all the 100 new records and store its id in a set or you can use trigger.newMap.keyset() and query the records based on the id set 

So tat you will get the existing open records(Use where condition in the query to stop retriving the closed records).

Process old records and save the parentID in another set.

Then process new records and check weather the set has the new records ID.

Thanks,
saraz