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
Galeeb SKGaleeb SK 

Parent Record Update

Hi ,
iam tried to Update Parent Record Status,My scenario is 

when  ''all'' the  child records status is 'Closed'  then its corresponding Parent record status is also 'Closed'.iam trying but fail because iam changing the  one child record status  is closed its corresponding parent record is also changed this not good,please help me.. My code is

trigger StatusChangePostion on Job_Application__c (after insert,after update) {

list<Position__c> pslist=new list<Position__c>();

list<ID> listids=new list<ID>();

for(Job_Application__c  jb:trigger.new){

listids.add(jb.Position__c);
}
pslist=[select id,Name,(select id,Status__c from Job_Applications2__r)from Position__c where id IN:listids];

for(Position__c p:pslist){

Boolean setToApproved=true;

for(Job_Application__c ja:p.Job_Applications2__r){

/*if(ja.Status__c=='Open'){

//p.Status__c='Closed';


setToApproved=false;

break;
}
if(ja.Status__c=='Approved'){

setToApproved=false;

break;
}*/

if(ja.Status__c=='Closed'){



p.Status__c='Closed';
}
}
update pslist;
}
}

Regards 
Galeeb SK
ManojjenaManojjena
HI Galeeb SK,

Try with belwo code !!
trigger StatusChangePostion on Job_Application__c (after insert,after update) {
  Set<Id> parentIdSet=new Set<Id>();
  List<Position__c> posListToUpdate=new  List<Position__c>();
  Map<Id,Integer> posIdWithJbAppSizeMap=new Map<Id,Integer>();
  Map<Id,Integer> posIdWithJbAppClosedSizeMap=new Map<Id,Integer>();
  for(Job_Application__c jobApp :Trigger.new ){
    if(jobApp.Status__c='Closed' && jobApp.Position__c != null ){
		parentIdSet.add(jobApp.Position__c);
	}
  }
  if(parentIdSet!= null ){
    for(Position__c pos:[SELECT id,(SELECT id,Status__c FROM Job_Application__c ) FROM Position__c WHERE Id IN :parentIdSet]){
      posIdWithJbAppSizeMap.put(pos.Id,pos.Job_Applications2__r.size());
    }
    for(Position__c pos:[SELECT id,(SELECT id,Status__c FROM Job_Application__c WHERE Status__c='Closed') FROM Position__c WHERE Id IN :parentIdSet]){
      posIdWithJbAppClosedSizeMap.put(pos.Id,pos.Job_Applications2__r.size());
    }
	for(Id posId : posIdWithJbAppSizeMap.keySet()){
	  if(posIdWithJbAppSizeMap.get(posId) ==posIdWithJbAppClosedSizeMap.get(posId) ){
	    Position__c pos=new Position__c(id=posId,Status__c='Closed');
		posListToUpdate.add(pos);
	  }
	}
  }
  if(!posListToUpdate.isEmpty()){
    try{
	  update posListToUpdate;
	}catch(DmlException de){
	 System.debug(de);
	}
  }
}

Let me know if it helps!!
Thanks
Manoj
Galeeb SKGaleeb SK
Hi,Manoj

Its working fine ,when we update the all child records status  the parent record status is changed to  'Closed'.But if we insert new one  child record  status is set to 'Closed' after creation of parent record ,status of parent is also changed to 'Closed'.I think we are using 'After insert' event thats why it is changed to 'Closed'.Supose we are using 'After insert ' event  the status of parent is not changed to 'Closed' .How? After Update  event its working fine.