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
DeexithDeexith 

Date Trigger

Some of the fields of the Object are CommencementDate, Expiry Date and Termination Date

I want to write a trigger 

1)If the commencement date has not happened yet – it should trigger to pending

Can I try After Insert After Update Trigger

 



for(Office off: Trigger.new)
{
system.debug('Trigger.New size' +Trigger.New.size());
try
{
if(off.CommencementDate__c == ' ??')
off.Description = 'It is pending';
else if(off.CommencementDate__c==' ??')
off.Description = ' Active ';
}
catch (Exception e)
{
system.debug('Error');
}
}



How to write date trigger ?

Best Answer chosen by Admin (Salesforce Developers) 
jbroquistjbroquist

An additional DML operation isn't needed in this case, so you can have your trigger run before insert and update. Your code would look something like the following:

trigger MyObjectTrigger on SObject (before insert, before update)
{
    for(SObject object : Trigger.new)
    {
        //check if the commencement date is after today
        if(object.CommencementDate__c > System.today())
        {
            object.Description = 'It is pending';
        }
        else
        {
            object.Description = 'Active';
        }
    }
}

 I should also point out this could very easily be accomplished with Workflow Rules as well.

All Answers

jbroquistjbroquist

An additional DML operation isn't needed in this case, so you can have your trigger run before insert and update. Your code would look something like the following:

trigger MyObjectTrigger on SObject (before insert, before update)
{
    for(SObject object : Trigger.new)
    {
        //check if the commencement date is after today
        if(object.CommencementDate__c > System.today())
        {
            object.Description = 'It is pending';
        }
        else
        {
            object.Description = 'Active';
        }
    }
}

 I should also point out this could very easily be accomplished with Workflow Rules as well.

This was selected as the best answer
DeexithDeexith

Thanks Buddy ! , 
Even I thought of writing workflows but I was told to write Trigger .