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
SF7SF7 

Help with trigger to fire without updating the record .

In This below trigger i am trying to update a standard field with the value from custom formula field . ANd the custom formula field looks at campaign date and if campaign date is today  then formula evaluates to ' In progress' and if date is in the past the n 'completed'' and if in date is in future then 'planned'.

 

And my formula works perfect and my trigger works perfect except in one scenario if i have my date as tommorw formula field evaluate to planned and my trigger copies the same value to the standard field and when i log back tommorw and go to the campaign formula field changes automatically to in progess because date is today but my trigger does not copy the value unless i edit the Opportunity .

 

trigger CampaignDelete on Campaign (before delete,before insert,before update) 
{
  //  for (Campaign cg: trigger.old)
    
      //  if(cg.Status == 'In Progress')
        
        //    {
        //    cg.addError('Cannot delete Campaigns with this status');
           
      //     }
     Set<Id> ids = new Set<Id>(); 
     
     list<Campaign> clist = [SELECT Id, Status, Campaign_Status__c FROM Campaign Where Id IN : ids];   
         
         for(Campaign cg : trigger.new){
        cg.Status = cg.Campaign_Status__c ; 
    }
    insert clist;

}

 

And one more question can i use before delete and before update logics in the same trigger ?

 

Thanks

Akhil

Best Answer chosen by Admin (Salesforce Developers) 
Noam.dganiNoam.dgani

I'd suggest a workflow on create or update and now meets criteria on the campaign

 

the criteria should be -

ISNEW() || ISCHANGED(startdate) || ISCHANGED(enddate)

 

The operations should be:

Immediate field update on status to planned

 

time based workflow to 0 days after startdate to update status to in progress

 

and

 

another time based workflow to 0 days after enddate to update status to completed.

All Answers

Noam.dganiNoam.dgani

Hi Akhil

 

a change to a formula field does fire triggers or workflows. 

it works when you update the record because you are firing the trigger yourself and it evaluates the formula.

 

additionally, you will probably get an exception on the trigger.old part, when running in before insert - there is no trigger old in before insert.

 

what is e query for? There is nothing in the set when you use it in the query - nothing will be returned..

 

and finally, why do you need to copy campaign status to status? They are both fields on campaign that show the same thing?

 

what is the real purpose of what you want?

 

regarding the after delete and before update question, yes you can but they rarely share the same logic.

Alex.AcostaAlex.Acosta

Formula values are generated on the fly each time you pull the record. They will not cause a record to update on it's own. If you want this to be automated you'll either have to have a scheduled job that will do the querying for you and update the nessessary record or have some sort of time based workflow.

 

As far as your last question you can have any mix and match of the events which will cause your trigger to fire off. If you wished you could have the same trigger fire off on all events

 

IE:

trigger myTrigger on Account (before insert, before update, before delete, before undelete, after insert, after update, after delete, after undelete){
...
}

 

SF7SF7

Hey thanks for the reply 

 

The purpose of having two status fields is i have a logic on the custom Status field which checks the dates 

 

IF( TODAY() < StartDate,"Planned", 
IF(AND(TODAY() >= StartDate, TODAY() <= EndDate),"In Progress", 
IF(TODAY() > EndDate,"Completed", 
"")))

 

And purpose of copying the value into the standard fied is because standard field is used in so many different places like reports other objects so its hard to replace every where so i thought it would be easy to copy the value .

 

The actual problem i had is if the campaign is cretaed with a start date like say tommorow and my formula evaluates staus into planned and when i come back tommorwo without editing the record my status value should be changed to in progress because techinally the start date is today but trigger does not fire unless i update the record.

 

And coming to trigger.old i am trying to use it for before delete

 

Thanks

Akhil.

Noam.dganiNoam.dgani

I'd suggest a workflow on create or update and now meets criteria on the campaign

 

the criteria should be -

ISNEW() || ISCHANGED(startdate) || ISCHANGED(enddate)

 

The operations should be:

Immediate field update on status to planned

 

time based workflow to 0 days after startdate to update status to in progress

 

and

 

another time based workflow to 0 days after enddate to update status to completed.

This was selected as the best answer