• Surender Giri
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hi All,
A parent case should be closed only if all its child cases are closed. Parent case shouldn’t be allowed to be closed by the user. Once the last Child case is closed, the Parent Case should automatically be closed.

I written below trigger but is is working if any child case is closed then parent case is automatically closing instead of to close all the child case.

trigger CloseParentCaseTrigger on Case (after update) {
    list<id>idList=new list<id>();
    for(case c:Trigger.New){
        idList.add(c.parentId);
    }
    list<case>csList=new list<case>();
    List<case>caseList=[select id,status,(select id,status from cases) from case where id=:idList];
    system.debug('$$$$caseList: '+ caseList);
    try{
        for(case c:caseList){
            for(case c1:c.cases){
                if(c1.status=='closed'){
                    c.status='closed';
                    csList.add(c);
                    
                }
            }
        }
    }
    catch(exception e){
    }
    update csList;
}
Hi All,

I have written a trigger but i am unable to avoid the for loop inside the for loop. Can anyone please suggest me that how can i resolve this by using map or any other collection.

trigger OfferPartnerTrigger on Offer_Partner__c (After Insert,After Update,After Delete) {
    set<id>OfferId=new set<id>();
    If(Trigger.IsAfter && Trigger.IsInsert || Trigger.IsUpdate){
        for(Offer_Partner__c offr:Trigger.New){
            OfferId.add(offr.Offer__c);
        } 
    }     
    
    If(Trigger.IsAfter && Trigger.IsDelete){
        for(Offer_Partner__c offr:Trigger.old){
            OfferId.add(offr.Offer__c);
        }
    }  
    try{
        MAP<Id,Offers__c> offerMap = new MAP<Id,Offers__c>();
        list<string>partnerIdList=new list<string>();
        list<Offers__c>ofrList=new list<Offers__c>();
        list<Offers__c>offerList=[select id,Partners__c,(select id,name,Consumer__r.PartnerId__c from Offer_Partners__r) from Offers__c where id=:OfferId and id in (select Offer__c from Offer_Partner__c)];
        system.debug('offerList***'+offerList);            
        for(Offers__c ofr:offerList){
            for(Offer_Partner__c oosf:ofr.Offer_Partners__r){
                partnerIdList.add(oosf.Consumer__r.PartnerId__c);
                
                string allstring = string.join(partnerIdList,',');
                ofr.Partners__c=allstring;
                system.debug('oosf.Consumer__r.PartnerId__c***'+oosf.Consumer__r.PartnerId__c);            
                
                ofrList.add(ofr);
            }
            system.debug('partnerIdList***'+partnerIdList);            
        }

        offerMap.putall(ofrList);
        if(offerMap.size()>0){
            update offerMap.values();
        }
    }
    catch(Exception e){
        system.debug('OfferPartnerTrigger' +' '+e+' '+e.getLineNumber());
        
    }
}

Thanks
Hi All,
A parent case should be closed only if all its child cases are closed. Parent case shouldn’t be allowed to be closed by the user. Once the last Child case is closed, the Parent Case should automatically be closed.

I written below trigger but is is working if any child case is closed then parent case is automatically closing instead of to close all the child case.

trigger CloseParentCaseTrigger on Case (after update) {
    list<id>idList=new list<id>();
    for(case c:Trigger.New){
        idList.add(c.parentId);
    }
    list<case>csList=new list<case>();
    List<case>caseList=[select id,status,(select id,status from cases) from case where id=:idList];
    system.debug('$$$$caseList: '+ caseList);
    try{
        for(case c:caseList){
            for(case c1:c.cases){
                if(c1.status=='closed'){
                    c.status='closed';
                    csList.add(c);
                    
                }
            }
        }
    }
    catch(exception e){
    }
    update csList;
}