0

I have a parent-child relationship in which i need to change the Child's Parent record.

Basically everytime there is a change in SAP salesforce generates a brand NEW record, BUT the new record and previous old record share the same SAP document number. IE they are for the same thing except one record gets a status change to inactive and the other to active. The reason we simply don't update the current record is so we can track a history of changes that are made.

Think of it this way, theres an automated trigger that generates opportunity records EVERY TIME new data comes in from SAP, but they are the SAME 'opportunity' only an updated version. The reason we simply don't update the current record is so we can track a history of changes that are made. IE any change that happens in SAP will generate a new record in salesforce.

Now that i explained that twice i'm going into part two.

So each time a new record is generated the changes from the previous record to the new record are recorded in a child object record. But lets say there's 50 (for the same SAP document) records generated because over time there have been 50 changes in SAP. The problem becomes is each time the parent object record is generated only the changes from the previous record to the new record are listed in the child object record.

Now what i want to do is take all of the child objects and parent it to the latest parent object record. So if i have 50 parent records and 49 child records there will be 49 parent records with a status of 'inactive' and one record listed as 'active' and they share the same SAP doc number. So how do i get the 49 child records reparented to the 1 parent record that is listed as active.

obviously it'll need to be a trigger/class but i can't even begin to think of how the reparenting process works

1
  • Have you considered field history tracking? – Adrian Larson Sep 14 '18 at 0:37
1

I will start with this:

The reason we simply don't update the current record is so we can track a history of changes that are made.

Well, you could do that without the need to really create a new child record for this purpose. You can have the field audit enabled or use Big Objects and the rest is taken care of. You should likely go through these routes as in the current implementation you also have to deal with storage. If you at all want to reconsider your current approach, a good place to start with is from below links:


Now, coming to your original question:

So how do i get the 49 child records reparented to the 1 parent record that is listed as active.

You can do this based on the trigger on the Parent record when its getting created. Your approach will be something to as below:

  1. During creation of New Parent record, fetch the Existing Child records which have same SAP Document Number
  2. In after insert of the New Parent record, fetch all the child records with the same SAP Document Number
  3. Iterate through every Existing Child record, and set their status to Inactive and re-parent them to the newly created record
  4. The New Child record is created as it is today

Your code based on the approach above will be something as below.

trigger on Parent__c (after insert) {
    ...
    ...
    List<ChildRecords__c> updatedList = new List<ChildRecords__c>();

    for(ChildRecords__c crec : [select id, parent_id__c, status__c, sap_document_number from ChildRecords__c where sap_document_number = <the number>]) {
        crec.status__c = 'Inactive';
        crec.parent_id__c = Trigger.new[0].Id;
        ...
        updatedList.add(crec);
    }
    update updatedList;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.