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
lizmijohnlizmijohn 

Update/add/delete child records on parent field's start and end date change

Hi,

 

Please help me with my requirement as follows:

 

Parent object: project

Child object :Project analysis

 

Trigger on :Project analysis

 

Scenario 1: Parent object (start date=1/6/2011 and end date=1/1/2012)

 

On Insert Project analysis will have records with date values as

PA 1 : Date-1/6/2011,PA 2:  Date-2/7/2011, PA 3: Date-2/8/2011,...PA 19: 10/12/2012.

 

On Update if I change Parent object's (start date=1/5/2011 and end date=1/1/2012) then the value are as follows:

PA 1 : Date-1/5/2011,PA 2:  Date-1/6/2011,...PA 18: 9/10/2012, PA-19: 10/12/2012

 

As per my understanding on change of parent object's start and end date, the child records are not incrementing or decrementing accordingly to take the correct date values.

 

My code is as below:

 

if(trigger.isupdate){
List<Project_Analysis__c> listPA= new List <Project_Analysis__c> ();

List<Project_Analysis__c> tbuPA = New List<Project_Analysis__c>();

listPA = [Select Project1__c,Amount_Per_Month__c,Date__c,Start_Date__c FROM Project_Analysis__c WHERE Project1__c IN :trigger.new];

for (Project1__c np : trigger.new) {Integer j = 0;

date NewDate=np.Start_Date__c;

date upDate1=NewDate;date Test;

for (Project1__c oProject : trigger.old){       

for(Project_Analysis__c lPA : listPA){         

if (lPA.Project1__c == np.id && ((np.Project_Value_per_month__c != lPA.Amount_per_month__c)&&(np.Start_Date__c!=lPA.Start_Date__c))){           

lPA.Amount_per_month__c = np.Project_Value_per_month__c;            if(np.No_of_months__c!=oProject.No_of_months__c){           

if(j <= (np.No_of_months__c)){               

if(upDate1< np.EndDate__c.addDays(25)){                 

  lPA.Date__c = upDate1;                                       

upDate1= upDate1.addDays(31);                                     

}                              

}             

j=j+1;                         

tbuPA.add(lPA);           

}       

}   

}   

}

if(tbuPA.size() > 0) 

update tbuPA;

}

 

 

Thanks in advance

pankaj.raijadepankaj.raijade

the logic you have writter will update the child records only if "No_of_months__c" field is changed.

 

there are many other conditions. But I think this is the problem area or was this part of your requirement.

 

Regards,

Pankaj Raijade.

lizmijohnlizmijohn

Based on 'No of months' field change, the code has to be modified. I am not a developer. So would like to get some help on this area.

pankaj.raijadepankaj.raijade

The code you have written is unnecessery complicated.

 

You can post your detailed requirement I will get you the code with simple logic.

 

Regards,

Pankaj Raijade.

pankaj.raijadepankaj.raijade

if possible post with example.

 

Regards,

Pankaj Raijade.

lizmijohnlizmijohn

I have a parent object: Project

Child object: ProjectAnalysis

 

On insert Project fields will be entered

 

Project value=60000

Start date=1/6/2011

End date=1/6/2012

 

No: of months=ROUND(((( EndDate__c - Start_Date__c ) / 30)+0.5),0)

Project value per month=ROUND(( ProjectValue__c / No_of_months__c ),0)

 

On save a trigger is initiated that creates child object (project analysis records)

PA-id, Amount per month=Project value per month,Date=1/6/2011

 

This way it has to repeat till( End date+25) is reached. Can be seen in my code. 

 

On Update of start date to 1/7/2011 then child records should take dates from 1/7/2011 and ends in 1/6/2012. It should reduce child records count to 12 now. This way on change of start date /end date the child record should change by taking correct date values.

 

Please let me know if any questions.

 

 

lizmijohnlizmijohn

Any help on this soon will be appreciated.

 

Thanks

pankaj.raijadepankaj.raijade

need to clear one more thing.

 

if the project period is extended you may need to add new records for Project analsys and if it is reduced you may need to delete additional.

 

or you expect the project dureation to remain constant?

lizmijohnlizmijohn

Yes, if the project duration is more then records need to be added else reduced. Project duration may not be constant.

pankaj.raijadepankaj.raijade

Hi,

I am looking in this. will update you soon.

 

Regards,

Pankaj Raijade.

lizmijohnlizmijohn

Any update on this?