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
JaredPHJaredPH 

Help? Simple trigger

I'm trying to create a trigger that will update a picklist (picklist__c) based on the number of days a date/time field (field1__c) is later than another date field (field2__c) on the opportunity object. Here are the parameters:

 

if field1-field2 + 45

   <0 do nothing

   45 to 74 update picklist to "late1"

   75 to 104 update picklist to "late2"

   105 or greater update picklist to "late3"

 

Thanks for any help. I look at this and think it should be simple, but I'm not coming up with it.

 

 

Navatar_DbSupNavatar_DbSup

Hi,


Try the below code as reference(Made changes Accordingly):


Trigger TestTrigger on opportunity(after insert)
{
date dt1;
date dt2;
list<opportunity> op=new lsit<opportunity>();
for(opportunity o : trigger.new)
{
dt1=trigger.new[0].field1__c;
dt2=trigger.new[0].field2__c;
if(dt1.daysBetween(dt2)+45 >= 45 || dt1.daysBetween(dt2)+45 < 75)
{
o.PickList__c ='late1';
}
if(dt1.daysBetween(dt2)+45 >= 75 || dt1.daysBetween(dt2)+45 < 105)
{
o.PickList__c ='late2';
}
if(dt1.daysBetween(dt2)+45 >= 105)
{
o.PickList__c ='late3';
}
opp.add(o);
}
update opp;
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

JaredPHJaredPH

Thanks so much for the help.

 

I made the changes and I am getting an error related to the datetime field. The first is a date/time rather than a date, so I changed the variable, but then it didn't like the daysbetween method on a datetime.

 

Error: Compile Error: Method does not exist or incorrect signature: [Datetime].daysBetween(Date) at line 10 column 4

  
 

 

 

Any ideas? Thanks again for the help

 

Here is my changed version of what you gave:

 

Trigger LateApp on opportunity(after update)
{
datetime dt1;
date dt2;
list<opportunity> opp=new list<opportunity>();
for(opportunity o : trigger.new)
{
dt1=trigger.new[0].Accepted__c;
dt2=trigger.new[0].Payment_ref__c;
if(dt1.daysBetween(dt2)+45 >= 45 || dt1.daysBetween(dt2)+45 < 75)
{
o.Payment_Plan__c ='Late1';
}
if(dt1.daysBetween(dt2)+45 >= 75 || dt1.daysBetween(dt2)+45 < 105)
{
o.Payment_Plan__c ='Late2';
}
if(dt1.daysBetween(dt2)+45 >= 105)
{
o.Payment_Plan__c ='Late3';
}
opp.add(o);
}
update opp;
}