You need to sign in to do that
Don't have an account?
mitsvik
How to Update parent case of record type A when the ASSOCIATED child record (rec type B) is closed
Requirement: if and only iff all the child cases (WITH RECORD TYPE B) are closed then automatically parent case record (rECORD TYPE A) should be closed.
I have written the following code, can some one tell me where i am wrong and also how do i query to suit the record type?
trigger Closeparentcaseauto on Case (before update) {
Integer counter = 0;
Set<Id> isparentID = new Set<Id>();----Create a set collection to get all the CHILD cases with parent ID
for(Case cs : trigger.new)
if(cs.ParentId!=null)
isparentID.add(cs.ParentId);----From the trigger het the cases where parent id is not null and add it to the set collection.
}
---loop through the Cases with the above parent ID and put it in a list collection. ie: collect all the cases with parent ID.
List<Case> cswithparentIDList = [SELECT Select Id,Status,ParentId From Case where status != 'Closed' and ParentId IN : isparentID];
---*/cswithparentIDList---Contains all the cases with the collected parents IDS. and also cases that is open.
---Create a map collection to get the Case with parent id and its child cases as a key value pair.
Map<Id,List<Case>> caseListMapWithId = new Map<Id,List<Case>>();
for (Case c: cswithparentIDList)
if(caseListMapWithId.containskey(c.parentid)){
caseListMapWithId.get(c.parentid).add(c);
}else{
caseListMapWithId.put(c.parentid,c);
}
}
Now loop though the child case parent id and status open to loop through its child cases.
for (Case c1: caseListMapWithId.values()){
if(c1.Status != 'closed')
{
counter++;
}
}
for (case parentcase: cswithparentIDList) {
parentcase.Status = 'Closed';
}
if (counter == 0){
update parentcase;}
}
I have written the following code, can some one tell me where i am wrong and also how do i query to suit the record type?
trigger Closeparentcaseauto on Case (before update) {
Integer counter = 0;
Set<Id> isparentID = new Set<Id>();----Create a set collection to get all the CHILD cases with parent ID
for(Case cs : trigger.new)
if(cs.ParentId!=null)
isparentID.add(cs.ParentId);----From the trigger het the cases where parent id is not null and add it to the set collection.
}
---loop through the Cases with the above parent ID and put it in a list collection. ie: collect all the cases with parent ID.
List<Case> cswithparentIDList = [SELECT Select Id,Status,ParentId From Case where status != 'Closed' and ParentId IN : isparentID];
---*/cswithparentIDList---Contains all the cases with the collected parents IDS. and also cases that is open.
---Create a map collection to get the Case with parent id and its child cases as a key value pair.
Map<Id,List<Case>> caseListMapWithId = new Map<Id,List<Case>>();
for (Case c: cswithparentIDList)
if(caseListMapWithId.containskey(c.parentid)){
caseListMapWithId.get(c.parentid).add(c);
}else{
caseListMapWithId.put(c.parentid,c);
}
}
Now loop though the child case parent id and status open to loop through its child cases.
for (Case c1: caseListMapWithId.values()){
if(c1.Status != 'closed')
{
counter++;
}
}
for (case parentcase: cswithparentIDList) {
parentcase.Status = 'Closed';
}
if (counter == 0){
update parentcase;}
}
That should take care of teh record Type for you.
Now you may need to think about the logic for comparing - that SELECT statement will return Open Cases for those Parent Ids.
You will now need to identify those record Ids that were in the original Set of Ids and the Ids of the parents that are returned.
SO perhaps create a Set of Parent Ids from the second list of Cases. (These are Accounts with Open Cases).
and use the Contains method for Sets and test if the Id is in the first list but not in the second list, then there are no open cases.
Example
for (Id id : isParentID) {
If ( !secondParentIDlist.contains(Id) ) {
update parentstatus;
}
regards
Andrew
Public class CaseTriggerHandler
{
public void updateCase(List<Case> caseList, map<id, List<Case>> childwithparent))
{
Set<Id> setparentID = new Set<Id>();
Integer count = 0;
Id parentRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Parent Record Type Name').getRecordTypeId();
Id childRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Child Record Type Name').getRecordTypeId();
for(Case varCase : caseList)
if(varCase.ParentId!=null && varCase.RecordTypeId= :childRecordTypeId.Id && varCase.status!=Null)
setparentID.add(varCase.ParentId)
}
if(setParentID!=NULL && !setParentID.IsEmpty())
{
List<Case> casewithparentIDList = [SELECT Select Id,Status,ParentId From Case where ParentId IN : setParentID];
for (Case childcases: casewithparentIDList)
if(childwithparent.containskey(childcases.parentid)){
List<Case> lstCases = childwithparent.get(childcases.parentid)
lstCases.add(childcases)
childwithparent.put(childcases.parentid,childcases);
}else{
caseListMapWithId.put(childcases.parentid,new List<Case> {childcases});
}
}
List<case> toUpdate = new List<case>();
for (Case childrencases: caseListMapWithId.keyset()){
for (Case casevalues: caseListMapWithId.values()){
count =0
if(casevalues.Status == 'closed')
{
count++;
system.debug('*****Countet Variable value***'+ count);
}else
continue;
}if(count == casevalues.size() ){
childrencases.Status = 'Completed';
toUpdate.add(childrencases);
}
}
At first glance, i see that you are now trying to use a Handler. With no idea of the structure of your trigger, my response is written as a trigger not Handler.
Also at first glance, you have a SELECT statment inside a FOR loop - this is a NO-NO. The trigger would fail under bulk load. Retaining the formatting would have made that easier to see.
Regards
Andrew