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
kcnmaheshkcnmahesh 

Problem with triggers.....update other records based on one record value


We have two objects called as "Sales Order" and "Production Activity". 
For every "Sales Order" object's record there are 5 records in "Production Activity" which are dependent( or lookup) to "Sales Order" object's record.
That is for a Sales Order record, say SO1, there are 5 records in "Production Activity", say Punching, Bending, Fabrication, Powder-Coating and Packing; these 5 records are dependent to SO1.
For every record in production activity there is a start time and an end time. Also, the condition is that the end time of first record will be the start time of next record. And so on for all the remaining records. 
For ex: st1= start time of record 1 and et1 = end time of record 1
           st2= end time of record 1 and et2 = end time of record 2 
           st3= end time of record 2 and et3 = end time of record 3
           st4= end time of record 3 and et4 = end time of record 4
           st5= end time of record 4 and et5 = end time of record 5
Now, st2=et1, st3=et2, st4=et3, st5=et4.
st = Start Time; et = End Time

          In SO1. if I change the end time of first record, i.e. "Punching", then the start time of second record, i.e. "Bending", should also be changed, automatically and also the remaining records means 3 records.

If we edit second record time then we need to change remaining 3 records time w.r.t. second record.

. I am unable to build a logic to make this change happen automatically.

Anup JadhavAnup Jadhav

I'd also add field called "order" or "sequence number" to the "Production Activity" object.

 

The trigger should be on the Production Activity object obviously:

 

The logic  will be along the lines of:

1. Check if the start date or end date has changed.

2. Retrieve the corresponding SO object, and all of it's PA objects

3. Add the PA to a list. Add them to the list only if the sequence number of the PA is greater than the current PA in context (i.e. whose date has changed)

4. Make the changes to the object in the list by iterating through the items in the list

5. update the list in database.

 

Obviously this will involve again fire the trigger on the PA object. To avoid that you might have to use global or public static flags in another class which will be used by the trigger to perform any action.

 

 

 

 

kcnmaheshkcnmahesh

Hi, 

     thanks for the reply. We have done the first 3 points as said by you. We are held up in the 4th point. 

We are unable to do the iteration. 

 

I.e. If ET1 is changed then ST2 should also be changed w.r.t ET1.

 Similarly, ST3 should change w.r.t ET2

                   ST4 should change w.r.t ET3

                   ST5 should change w.r.t ET4

 

here, ST1 = Start Time of 1st stage. ET1 = End Time of 1st stage.

          ST2 = Start Time of 2nd stage. ET2 = End Time of 2nd stage.

          ST3 = Start Time of 3rd stage. ET3 = End Time of 3rd stage.

          ST4 = Start Time of 4th stage. ET4 = End Time of 4th stage.

          ST5 = Start Time of 5th stage. ET5 = End Time of 5th stage.

 

We have written code to change ST2 w.r.t ET1.

But we are unable to do this recursively for remaining stages. 

I.e.             unable to change  ST3 w.r.t ET2

                  unable to change  ST4  w.r.t ET3

                  unable to change ST5  w.r.t ET4

 

 

Below is our code:

 

 

trigger ChangeTimeOnProduction on Production_Activity__c (after insert, after update) {


Production_Activity__c p = new Production_Activity__c();
p = [ select id , SO_Activity_Name__c , Planned_Finish_Date_Time__c ,Planned_Start_Date_Time__c , Preceding_Activity__c , Dependency__c ,stage__c , Sales_Order__c from Production_Activity__c where id =: trigger.new[0].id] ;
System.debug('Activity Name '+p.SO_Activity_Name__c);
List<Production_Activity__c> pro = new List<Production_Activity__c>();
pro = [ select id,SO_Activity_Name__c , Planned_Finish_Date_Time__c , Planned_Start_Date_Time__c, Stage__c , Sales_Order__c from Production_Activity__c where Sales_Order__c =: p.Sales_Order__c and Stage__c >: p.Stage__c Order by Stage__c];
System.debug('Size of pro' + pro.size());
for(Production_Activity__c prod : pro){


}
}