+ Start a Discussion
Sangeetha TSangeetha T 

Triggers to copy values from one field to another on different objects

Hi this is the requiremnt
i have to copy the standard field(Closedate) on opportunities object to custom filed(start_date__c) on Opportunities products object
and opportunities products have another field called enddate where enddate = start_date_c + 1 year + 14 days and also check according for leap year (i.e) leap year may have 1 day extra ​
Please help me with the apex trigger
Best Answer chosen by Sangeetha T
Sangeetha TSangeetha T
Hi Krishna this worked perfectly

if (Date.isLeapYear(opp.Start_Date__c.year()))
   { opp.End_Date__c = opp.Start_Date__c.addDays(380);}
else
    {opp.End_Date__c = opp.Start_Date__c.addDays(378);}

All Answers

sharathchandra thukkanisharathchandra thukkani
Yous hould be able to use Date Class and DateTime class methods to do this have a look on those

In the Date Class we have isLeapYear(year) method which will help you in finding the leap year.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm
Sangeetha TSangeetha T
Hi Sharath thanks

trigger Start_date_update on OpportunityLineItem (before insert) {
for (OpportunityLineItem opp: Trigger.new)
{
opp.Start_Date__c=opp.Opportunity.CloseDate;
}
}

this is my trigger
so here the value of closedate on opp is not getting populated to the start date of opp line item object Can you pls tell if iam missing anything ​​
Surendra nuneSurendra nune
Hi Sangeeta,

You  are trying to access opportunity field from opp line item. 

You have to query the close date from opportunity using opportunity id and then assign the date, because you cant access related records in trigger.
Sangeetha TSangeetha T
Hi Surendra Thank you

Can you please modify the above code or tell me where to insert it
since im new to trigger im stuck how to complete it

Thanks in advance ​​​
Surendra nuneSurendra nune
trigger Start_date_update on OpportunityLineItem (before insert) {

Set<id> oppids = new set<id>();

for (OpportunityLineItem opp: Trigger.new)
{
 oppids.add(opp.opportunityid);
}

Map<id, opportunity> oppMap = new Map<id, opportunity>();
for(opportunity o : [select closedate from opportunity where id in: oppids]){
 
oppMap.put(o.id, o); 
}

for (OpportunityLineItem opp: Trigger.new){
 opp.Start_Date__c=oppMap.get(opp.opportunityId).closedate;
}

}
Sangeetha TSangeetha T
Wow thanks it perfectly worked for me ..you are supercool

Now I  have another field which is called end_date and end date = 365 days +14 days ​+startdate
I can create a formula filed here but this should be a editable one ..how can i again achieve that in trigger
Can i give the condition below this trigger itself
Please help me out ​​
Surendra nuneSurendra nune
Yes you can give it in the last for loop
Sangeetha TSangeetha T
trigger Start_date_update on OpportunityLineItem (before insert) {

Set<id> oppids = new set<id>();

for (OpportunityLineItem opp: Trigger.new)
{
 oppids.add(opp.opportunityid);
}

Map<id, opportunity> oppMap = new Map<id, opportunity>();
for(opportunity o : [select closedate from opportunity where id in: oppids]){
 
oppMap.put(o.id, o); 
}

for (OpportunityLineItem opp: Trigger.new){
 opp.Start_Date__c=oppMap.get(opp.opportunityId).closedate;

_______________________________________________________________________________
***if year(startdate) /4 ==0 ​
 {   opp.End_Date__c=opp.Start_Date__c+365+14;   }                                                                                    How to implement here
***else
{​opp.End_Date__c=opp.Start_Date__c+365+15;}
_______________________________________________________________________________​

}

}
Surendra nuneSurendra nune
yeah its correct
Sangeetha TSangeetha T

trigger Start_date_update on OpportunityLineItem (before insert) {
Set<id> oppids = new set<id>();
for (OpportunityLineItem opp: Trigger.new)
{
 oppids.add(opp.opportunityid);
}
Map<id, opportunity> oppMap = new Map<id, opportunity>();
for(opportunity o : [select closedate from opportunity where id in: oppids]){
 
oppMap.put(o.id, o);
}
for (OpportunityLineItem opp: Trigger.new){
 opp.Start_Date__c=oppMap.get(opp.opportunityId).closedate;
 if(year(Start_Date__c)/4==0) //leap year//
 {opp.End_Date__c=opp.Start_Date__c+365+14;}
 else
 {opp.End_Date__c=opp.Start_Date__c+366+14;}
 
}
}

I'm getting this error Error: Compile Error: Variable does not exist: Start_Date__c at line 18 column 10​
Surendra nuneSurendra nune
Please try to read the error message.

You cant expect direct solution for your entire implementation. This forum just guides you or shows you path to solution for our problems.

Change line 18 to 
if(year(opp.Start_Date__c)/4==0)

 
Krishna SambarajuKrishna Sambaraju
if (Date.isLeapYear(oli.Start_Date__c.year()))
    oli.End_Date__c = oli.Start_Date__c.addDays(379)
else
    oli.End_Date__c = oli.Start_Date__c.addDays(380)

Hope this works.
Sangeetha TSangeetha T
Thanks for your advice.and it is not working (incorrect signature )
Thanks all for your support ​
Sangeetha TSangeetha T
Hi Krishna

Thanks for the instant response.gave the condition like
if (Date.isLeapYear(opp.Start_Date__c.year()))
   { opp.End_Date__c = opp.Start_Date__c.addDays(380);}
else
    {opp.End_Date__c = opp.Start_Date__c.addDays(379);}

since else was throwing error
now for both the leap year and non leap year it is taking the first condition only that is 380 days is getting added for both the years
I feel that it is not taking the else statement
SO is der any option like Date.Is not leap year ​​​​
​​​
Sangeetha TSangeetha T
Hi Krishna this worked perfectly

if (Date.isLeapYear(opp.Start_Date__c.year()))
   { opp.End_Date__c = opp.Start_Date__c.addDays(380);}
else
    {opp.End_Date__c = opp.Start_Date__c.addDays(378);}
This was selected as the best answer