You need to sign in to do that
Don't have an account?
Sangeetha 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
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
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
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
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
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.
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
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
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;}
_______________________________________________________________________________
}
}
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
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
oli.End_Date__c = oli.Start_Date__c.addDays(379)
else
oli.End_Date__c = oli.Start_Date__c.addDays(380)
Hope this works.
Thanks all for your support
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
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);}