Button to activate/deactivate a task on a object

Sometimes we feel like to create a button/checkbox to activate/deactivate a task on that respective object. There are two ways to do that
1) Workflows (Easier, but has limits)
2) Trigger
I am sharing the code with you for creating a checkbox. If set to true, create a task for reminding after 30 days, if deactivated, delete the task.

for (Contact c :trigger.new){

  contact oldc = trigger.oldmap.get(c.id);

  if(c.Activate__c==true && oldc.Activate__c==false){

    task t = new task();

    t.subject = 'Reminder about calling Contact';

    t.whoid = c.id;

    t.isreminderset = true;

    t.reminderdatetime = system.today()+30;

    t.status = 'In Progress';

    t.IsRecurrence = false;

    insert t;}


  else if(c.Activate__c==false && oldc.Activate__c==true){

   List<task> t = [select id from task t where t.whoid=:c.id and t.subject='Reminder about calling Contact'];

   delete t;

  }} }

No comments:

Post a Comment