I have a sample request (header) table and product information (details). What I'm trying to do is after a product is shipped, an integration writes to the child record(s) and updates a field called: ship date actual with a date for when the product has been shipped. After all the child records have been marked with a ship date- the system should then update the parent status to Shipped. Otherwise, if there are child records not yet marked with a ship date- then keep the header status as it is: approved. I'm new to this- (I know, terribly obvious).
Parent Table: Sample_Request_Account_Information__c
Child Table: Sample_Request_Product_Information__c
Here is my apex code: I tried to look at an example which was posted on this forum to try my best to get this to work:
Apex Class: /** Updates the status field on Sample Request Account Information*/
public with sharing class childTriggerHandler {
public void UpdateFields(List< Sample_Request_Product_Information__c > newList)
{
List< Sample_Request_Account_Information__c > updateList = new List< Sample_Request_Account_Information__c >();
Map<id,List< Sample_Request_Product_Information__c >> mapParentIDAndChild = new Map<id,List< Sample_Request_Product_Information__c >>();
boolean flag;
set<Id> parentTrue = new set<Id>();
for(Sample_Request_Product_Information__c objChild : newList) {
parentTrue.add(objChild.(Sample_Request_Account_Information__c));
}
for(Sample_Request_Product_Information__c objChild : [select id , Ship_Date_Actual__c, Sample_Request_Account_Information__c from Sample_Request_Product_Information__c where Sample_Request_Account_Information__c IN: parentTrue] ){
system.debug('<<<<<<objChild '+objChild );
if(mapParentIDAndChild.containsKey(objChild.Sample_Request_Account_Information__c)) {
system.debug('<<<<<<in if 1st ');
mapParentIDAndChild.get(objChild.(Sample_Request_Account_Information__c).add(objChild));
system.debug('<<<<<<in if 1st mapParentIDAndChild '+mapParentIDAndChild);
} else {
system.debug('<<<<<<in else 1st ');
mapParentIDAndChild.put(objChild.Sample_Request_Account_Information__c,new List< Sample_Request_Product_Information__c >{objChild});
system.debug('<<<<<<in else 1st mapParentIDAndChild '+mapParentIDAndChild);
}
}
for(Id objId : mapParentIDAndChild.keySet()) {
flag = true;
for(Sample_Request_Product_Information__c objChild : mapParentIDAndChild.get(objId)) {
if(objChild.Ship_Date_Actual__c !==''){
flag = true;
break;
}
}
if(flag == true) {
updateList.add(new Sample_Request_Account_Information__c (Id = objId , Status__c = 'Shipped'));
} else {
updateList.add(new Sample_Request_Account_Information__c (Id = objId , Status__c = 'Approved'));
}
}
if(!updateList.isEmpty()) {
update updateList;
}
}// end of UpdateFields
}// end of childTriggerHandler