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
econ242econ242 

Time Based Workflow Assistance

Hello,

We're using activities to drive an Account "Status" and "Stage" both of which are custom fields. For Tasks, when a Task is completed, I have a trigger that automatically updates the Account "Status" and "Stage" based on the "Task" subject...so for example, when a Task is completed with a Subject of "Initial Needs Assessment Completed" my trigger fires and updates the Related Account "Status" to Qualified Prospect, and  "Stage" to Needs Assessment Completed.

Now, I need to accomplish the same thing on an "Event", so when the Event date and time pass, depending on the Event "Subject" the custom fields on the Related Account get updated as well. Now, I know using a trigger won't work per se, because you can't "Complete" an Event.

So...I'm told that using a time-based workflow is the way to go, but I'm not quite sure how to accomplish this and if a trigger is still necessary along with the workflow rule. Is it possible to have a cross object workflow update a field between an Event and an Account?

Any and all assistance on this would be greatly appreciated!!!

Thanks!!!

E
Best Answer chosen by econ242
AroraAnupAroraAnup
Hi E - This issue is because you have a single underscore in the Status field name in your If Statement - Status_c. Since this is a custom field, you need to use a double underscore, so essentially ev.Status__c
I tried doing this by replicating the same trigger in my dev org and it seems to be working now.
Pls make this change and re-test the trigger. M sure it will work this time :)

Let me know how it goes!

All Answers

AroraAnupAroraAnup
Hi E,

In my view, you are still going to need a trigger in this scenario as well. Key reason being that the fields that you want to be updated are at the Account level, and using a workflow you can only do cross-object field updates if the objects are related to each other via a master-detail relationship. 

Here, Events are related to Account via the "Related To" or "WhatId" field, which essentially is a lookup. When you write your workflow on Events and try to create a new field update, it will only give you Event object and related fields as the option to select the field to be updated. Another reason to why it won't give you the Account object plus fields for update is that the "WhatId" is linked to multiple objects (standard as well as custom) wherever Activities are allowed.

Since Events don't have a Status field to flag them as Completed, I am guessing you may want to do the following:

1. Create a custom picklist called "Status" on Events with values like - Not Started, In-Progress, Completed etc. You would need to make it a Required field
2. Have a validation rule on this field to check the status based upon the Event End Date
3. Create a trigger to update Account fields based upon the value selected on the Event

Hope this helps!
econ242econ242
Hi Arora,

Thank you for your response!!! But I still have one small issue...

I created the Custom picklist (Event_Status_c) with 2 values - Not Started and Completed. The default is not Started. I created a workflow rule that will update the custom field to "Completed" once the date/time passes...basically if the Event End Date is YESTERDAY it triggers the rule to run. Tested that and it works perfect...my problem is now with the trigger. Since I'm still a newbie to Apex and triggers, I tried modifying an existing trigger for the Event to update the fields on the Related To Account...below is that code:

trigger changeAccountStageEvent1 on Event (after update) {
    List<Account> accsToUpdate = new List<Account>();
        for (Event ev: Trigger.new){
            if(ev.Event_Status_c=='Completed' && ev.Subject=='Initial Sell/Needs Assessment'){
                Account ld = new Account(Id=ev.WhatId);                             
                     ld.Stage__c = 'Needs Assessment Completed';
                accsToUpdate.add(ld);          
            }
          
        }      
        update accsToUpdate;
    }

Problem is I keep getting this error - Error: Compile Error: Invalid field Event_Status_c for SObject Event at line 4 column 16.

Does anything jump out as being wrong in my code? Been battling this one for a while now...but I can't give up, lol!!!

Any thoughts?

Thanks again!!!

E
AroraAnupAroraAnup
I think I see the issue here E. In your for loop, you are creating a variable "ev" of type Event, which is correct. So in your if statement, you need to use ev.Status__c instead of ev.Event_Status__c (just like you have used ev.Subject)

Just changed this value in the if statement and I think the trigger should work.

Let me know how it goes!
econ242econ242
Hi Arora,

Still the same issue...I even changed the name of the custom field to "Status" and changed the if statement to reflect this per your suggestion...but same error:

Error: Compile Error: Invalid field Status_c for SObject Event at line 4 column 16

Updated code below:

trigger changeAccountStageEvent1 on Event (after update) {
    List<Account> accsToUpdate = new List<Account>();
        for (Event ev: Trigger.new){
            if(ev.Status_c=='Completed' && ev.Subject=='Initial Sell/Needs Assessment'){
                Account ld = new Account(Id=ev.WhatId);                             
                     ld.Stage__c = 'Needs Assessment Completed';
                accsToUpdate.add(ld);          
            }
          
        }      
        update accsToUpdate;
    }

Could it be the variable I'm using? i've seen so many other variables that relate to Event that I'm not sure which is correct...i've seen evnt, evntObj, e etc...

stuck!!!  :(
AroraAnupAroraAnup
Hi E - This issue is because you have a single underscore in the Status field name in your If Statement - Status_c. Since this is a custom field, you need to use a double underscore, so essentially ev.Status__c
I tried doing this by replicating the same trigger in my dev org and it seems to be working now.
Pls make this change and re-test the trigger. M sure it will work this time :)

Let me know how it goes!
This was selected as the best answer
econ242econ242
THANK YOU, THANK YOU, THANK YOU Arora!!! You are a lifesaver!!! Works like a charm!!!  :)

I just need to figure out how to write the test class so I can deploy to production, but this REALLY helped me out BIG TIME!!!

Thank you so much again!!!  :)
AroraAnupAroraAnup
Glad I could be of help E! Let me know if I can be of further help in the future :)

Njoy!