You need to sign in to do that
Don't have an account?
Rebecca Hendricks 9
Trigger Conflict with Process
Hello,
I'm not getting my code to work out, and believe it is the result of a conflict between the Trigger I've created and a Process builder that was created. However, for the life of me, I can't figure out where the error in my logic is happening.
Background:
My boss would like to know how many times our Reps have a 30 minute lag between creating/editing tasks. There are two ways that a task can be created/edited: 1) directly creating/editing a new task from the contact and/or opportunity, and 2) Automatically from a process that is launched when a new contact is created.
As the task can be created as the first actions of the day, as well as not really having a good way of knowing how this task relates to others in the system, I've created some custom fields that will hold a flag on whether it was the first activity of the day (which will always have more than a 30 minutes gap since the last activity action) and whether it was created within 30 minutes of the last task action. It is these fields that my trigger will update.
Trigger code:
Process information for 2 day "automatic" task:
Process for 2 week task creation:
Process for 2 Month task creation:
Questions-Information:
As I stated, as the trigger is on before insert and before update, I think the Process is creating the task, and that is activating the trigger. However, I thought my trigger had taken into account this information with the check for the subject information. (I should mention that the words searched for are exactly what is "coded" in the process, and will be the only time this exact phrase is used.) Even though I've got the information placed into the process, I believe the trigger would over-write it, but thought the subject inclusion would have caught the over-writing and still given it the value it needed, but that's not the case, as my "within 30 minutes" field is not populating with a True value as it should be for these.
Should I be having my trigger execute on After Insert and After Update information, and then update the information on that task again?
I'm not getting my code to work out, and believe it is the result of a conflict between the Trigger I've created and a Process builder that was created. However, for the life of me, I can't figure out where the error in my logic is happening.
Background:
My boss would like to know how many times our Reps have a 30 minute lag between creating/editing tasks. There are two ways that a task can be created/edited: 1) directly creating/editing a new task from the contact and/or opportunity, and 2) Automatically from a process that is launched when a new contact is created.
As the task can be created as the first actions of the day, as well as not really having a good way of knowing how this task relates to others in the system, I've created some custom fields that will hold a flag on whether it was the first activity of the day (which will always have more than a 30 minutes gap since the last activity action) and whether it was created within 30 minutes of the last task action. It is these fields that my trigger will update.
Trigger code:
trigger UpdateActivityFlags on Task (before insert, before update) { Boolean within30Mins = Boolean.valueOf('true'); //assume task was created more than 30 mins since last activity modification String userID = UserInfo.getUserId(); //Get User Id for task owner DateTime rightNow = system.now(); //get the date/time of when the task is being created Date rightNowDate = date.newInstance(rightNow.year(), rightNow.month(), rightNow.day()); //date part only of date/time task is being created Task mostRecentTask; Date mostRecentTaskDate; //Need to get most recent activity information try{ mostRecentTask = [SELECT id, LastModifiedDate, LastModifiedTime__c, EditedWithin30MinsOfLastTask__c, Subject FROM Task Where OwnerId = :userID AND LastModifiedTime__c < :rightNow ORDER BY LastModifiedTime__c Desc Limit 1]; mostRecentTaskDate = Date.newInstance(mostRecentTask.LastModifiedDate.year(), mostRecentTask.LastModifiedDate.month() , mostRecentTask.LastModifiedDate.day()); //get date portion of most recent activity modification } catch(Exception e){ System.debug('There were no previous tasks for this user. Error information: Type - ' + e.getTypeName() + ' ; Message - ' + e.getMessage()); } //Check if task was created within 30 mins of last activity modification //if(mostRecentTask != NULL){ //There IS a previous task to compare myself to // if((rightNow.getTime() - mostRecentTask.LastModifiedTime__c.getTime()) < 1800000 // 30 mins = 1.8E6 milliseconds // || mostRecentTask.Subject.containsAny('- 2 Week Followup') || mostRecentTask.Subject.containsAny('- 2 Month Followup') // ){ // within30Mins = True; // } //} if(mostRecentTask != NULL){ //There IS a previous task to compare myself to if(Trigger.isInsert){ if((rightNow.getTime() - mostRecentTask.LastModifiedTime__c.getTime()) >= 1800000 && !mostRecentTask.Subject.containsAny('- 2 Week Followup') && !mostRecentTask.Subject.containsAny('- 2 Month Followup') ){ within30Mins = false; } } else{ if((rightNow.getTime() - mostRecentTask.LastModifiedTime__c.getTime()) >= 1800000){ within30Mins = false; } } } //update flag information for (Task t : Trigger.New){ t.EditedWithin30MinsOfLastTask__c = within30Mins; if(mostRecentTask == NULL){ //no previous tasks owned by user t.FirstActivityofDay__c = True; //no other activities means that this is the first one they are creating } else { if((Date.valueOf(rightNowDate).daysBetween(mostRecentTaskDate)) != 0){ //task creating and last activity modification are not on the same day t.FirstActivityOfDay__c = True; } t.PreviousTaskDateTime__c = mostRecentTask.LastModifiedTime__c; } } }the commented out code is when I set the initial value of the Boolean to false. However, this really didn't work for the automatic notifications items, so I tried to approach it from the reverse.
Process information for 2 day "automatic" task:
Process for 2 week task creation:
Process for 2 Month task creation:
Questions-Information:
As I stated, as the trigger is on before insert and before update, I think the Process is creating the task, and that is activating the trigger. However, I thought my trigger had taken into account this information with the check for the subject information. (I should mention that the words searched for are exactly what is "coded" in the process, and will be the only time this exact phrase is used.) Even though I've got the information placed into the process, I believe the trigger would over-write it, but thought the subject inclusion would have caught the over-writing and still given it the value it needed, but that's not the case, as my "within 30 minutes" field is not populating with a True value as it should be for these.
Should I be having my trigger execute on After Insert and After Update information, and then update the information on that task again?
Please see below aritcle to avoid recursion.
https://help.salesforce.com/articleView?id=000133752&language=en_US&type=1
If the problem persist, you can test by deactivating one by one the process builder and trigger.
As a result of the order of execution, process builder does not fire triggers.
In process builder there is an Action "Apex".
You need to create an apex class with your trigger logic in it, and it must have the @InvocableMethod annotation.
In the Process builder Apex Action, you should select this class and pass your object ids into it.
My suggestion is that you create a single helper class to meet your business needs, then call this helper class from both the trigger context AND the Process builder Apex Action.
Trigger code:
Then in your trigger helper, have a method called "onBeforeInsertOrUpdate" that takes a list of tasks to represent Trigger.New and a boolean for isInsert or not. You must have another method that is @Invocable in order to handle the context of the helper being called from the process. You can declare the
Trigger/Process Helper code: