1

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
5
  • What is the issue? Any errors you are getting? Commented May 10, 2016 at 20:16
  • I should have included that, my apologies. Yes, I am receiving a compile error which says: Variable does not exist: Sample_Request_Account_Information at line 13 ......which is: parentTrue.add(objChild.(Sample_Request_Account_Information__c)); However, when I try to declare it- it doesn't like it.
    – Michele
    Commented May 10, 2016 at 20:22
  • 1
    It should be parentTrue.add(objChild.Sample_Request_Account_Information__c); remove ( after. and ) at the end Commented May 10, 2016 at 20:23
  • That was it! But I am still receiving an error saying : Comparison arguments must be compatible types: Date, String at line 30 which is: if(objChild.Ship_Date_Actual__c !==''){
    – Michele
    Commented May 10, 2016 at 20:27
  • I want to evaluate if there is some date stored in the field: Ship_Date_Actual__c
    – Michele
    Commented May 10, 2016 at 20:28

2 Answers 2

1

Alternative solution (no code)

  1. Add two RSF fields on Parent: CountChildren__c, CountShippedChildren
  2. Use a workflow/process builder that compares when both are equal and then updates Parent with header field

Usual caveats about RSF apply (no cross object formula references, no reference to formula fields with TODAY() or other relative dates

5
  • @Michele -- don't you need to count all Children unconditionally in one RSF and count shipped children in separate RSF? That is, count of children where shipdate is blank is a count of unshipped children. I thought your business logic was if all children were shipped, then do update
    – cropredy
    Commented May 11, 2016 at 17:33
  • That's correct- if all children are shipped update. I had to add the 0s because it was marking it shipped when no product information was even entered- hence both counts would have ='d 0 and then the process would update the status. I am ignoring when those counts are 0 now. I had an unconditional count - but the same results happened when comparing with the count of ship to <> blank. It still updated the status too early. That's why I put in the other condition (just trying different things.) No luck- either way.
    – Michele
    Commented May 11, 2016 at 17:49
  • @Michele -- my suggestion is to turn debug log on for the running usr (i.e. you) and log WORKFLOW at INFO -- you should be able to trace the inputs to your process builder criteria. see SFDC Help
    – cropredy
    Commented May 11, 2016 at 17:56
  • I just changed it back to no conditions on the all child count-but I made one more change- it checked the option in process builder to "execute the actions only when specified changes are made to the record" and now it's working! Any drawback to that option? That is the first time I'm using it- seems to work though- I'll conduct more testing of course.
    – Michele
    Commented May 11, 2016 at 18:02
  • @Michele - your change made perfect sense - it means that the process only executes if any of the inputs to the criteria changed. Once you're confident this approach works, as a service to the community, mark the answer as resolved.
    – cropredy
    Commented May 11, 2016 at 19:25
1

I could see few issues with the code

  1. There shouldn't be ( after the dot notation. So change

    parentTrue.add(objChild.(Sample_Request_Account_Information__c));
    

to

parentTrue.add(objChild.Sample_Request_Account_Information__c);
  1. You comparison operator is wrong. It should be != than !== and also you can't compare Date with String. So compare the date with NULL instead of '' which is string.

  2. flag = true will always be true and update the parent status to 'Shipped' even all of child's ship date is null. So modify it as

    flag = true;
    for(Sample_Request_Product_Information__c objChild : mapParentIDAndChild.get(objId)) {
        if(objChild.Ship_Date_Actual__c == NULL){
            flag = false;
            break;
        }
    }
    

Hope it helps.

3
  • The problem is - it's marking the parent status to shipped when there is no values in the Ship_Date_Actual field. We only want the status to get updated to Shipped when there is a true date value in each of the child records' ship date actual field.
    – Michele
    Commented May 10, 2016 at 20:53
  • Yes you need to mark the flag as false when any of the Ship_Date_Actual__c is blank and using your if condition following, you can set the status of parent only if flag is true. Recheck my answer. Commented May 10, 2016 at 20:58
  • It marks the parent shipped when only 1 child has a true ship date. for(Id objId : mapParentIDAndChild.keySet()) { flag = true; for(Sample_Request_Product_Information__c objChild : mapParentIDAndChild.get(objId)) { if(objChild.Ship_Date_Actual__c == NULL){ flag = false; 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'));
    – Michele
    Commented May 11, 2016 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .