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
Kellie MillardKellie Millard 

Why is my workflow not triggering?

Scenario:
I have a custom Related Activity List which, (through the use of a custom field called "hubspot generated activity" , a workflow that updates the Custom field based on subject line of the task and a process builder), gives me the listing of all activity not generated by hubspot and this is working brilliantly - with the exception of those tasks that are created using the BCC email (email to salesforce)

For some reason the workflow is not "seeing" the email tasks as they are created in salesforce, so the workflow is not triggering and subsquently the process builder is not pulling them into the right list.  If I open each of these tasks and re-save it, the workflow is started and the rest of the process works through to the end- so i just need a way to set the workflow off without having to manually open the record. (we are talking about 250 records+ per day). 

I understood that the standard functionality of workflow rules couldnt do this so needed to created an Apex trigger... 

The apex trigger set to run when an email (bcc) is sent which populates the "hubspot generated activity" field to "To Sync" so that I can see which records need to be updated and the workflow process in place says if hubspot generated activity field equals "To Sync"- change field to "no" 

What can I do to make the task be seen? 

I did wonder if i need the Apex trigger to be an update after the record is created? But not sure if thats the right thing to do and if so how to change the code to reflect that?   Below is the Apex Trigger I have currently..

trigger HubspotGenActivity on Task (before insert) {
 for(Task newTask : trigger.new){
if(
            !String.isBlank(newTask.Subject) &&
            newTask
                .Subject
                .toLowerCase()
                .startsWith('email:')
        ){
            newTask.Hubspot_generated__c = 'To Sync';
        }
    }
}


Any ideas please? 
Thanks in Advance
Kellie 
 
Kellie MillardKellie Millard
I'm getting an error message using this Akhil - Error: Compile Error: Variable does not exist: isAfter at line 3 column 12 . 
Any ideas?
 
Kellie MillardKellie Millard
Using the following;
    trigger HubspotGenActivity on Task (before insert, before update, after insert, after update) {
    if(isInsert){
        if (isAfter){
            for(Task newTask : trigger.new){
                if(String.isNotBlank(newTask.Subject) && newTask.Subject.startsWith(String.toLowerCase('email:')) && newTask.Subject.contains(String.toLowerCase('email:'))){
                    newTask.Hubspot_generated__c = 'To Sync';
                }
            }

        }

    }
}

but am getting the same error sort of error message; Error: Compile Error: Variable does not exist: isAfter at line 3 column 13
 
Kellie MillardKellie Millard
same error message if  I use IsBefore

  trigger HubspotGenActivity on Task (before insert, before update, after insert, after update) {
    if(isInsert){
        if (isBefore){
            for(Task newTask : trigger.new){
                if(String.isNotBlank(newTask.Subject) && newTask.Subject.startsWith(String.toLowerCase('email:')) && newTask.Subject.contains(String.toLowerCase('email:'))){
                    newTask.Hubspot_generated__c = 'To Sync';
                }
            }

        }

    }
}

Any ideas?
Kellie MillardKellie Millard
Error: Compile Error: Variable does not exist: isAfter at line 3 column 13
 
Kellie MillardKellie Millard
same error message if  I use IsBefore
Kellie MillardKellie Millard
Thanks Akhill - i am now getting the following error message:
Error: Compile Error: Variable does not exist: String at line 5 column 81
 
Akhil ReddyAkhil Reddy
trigger HubspotGenActivity on Task (before insert, before update, after insert, after update) {
if(Trigger.isInsert){
	if (Trigger.isBefore){
        for(Task newTask : trigger.new){
            if(String.isNotBlank(newTask.Subject) && newTask.Subject.startsWith(newTask.Subject.toLowerCase())){
                    newTask.Hubspot_generated__c = 'To Sync';
			}
		}
      }
	}
}

 
Kellie MillardKellie Millard
Great - it saved - will now test it and let you know how I get on. Huge thanks for your help so far Akhil!
Akhil ReddyAkhil Reddy
Plese use above code... It is changed little bit
Kellie MillardKellie Millard
ok - not quite right... 

I am using the following trigger (which saves ok without any errors) but is now have these problems... 
1. The trigger is not updating the hubspot generated field to read "To Sync"  - its leaving it blank
2. workflow is still not being triggered automatically but will if i open the record, save and close

Its still going "unseen" into my salesforce.. 

Here the the trigger I am currently using 

trigger HubspotGenActivity on Task (before insert, before update, after insert, after update) {
if(Trigger.isInsert){
    if (Trigger.isBefore){
        for(Task newTask : trigger.new){
            if(String.isNotBlank(newTask.Subject) && newTask.Subject.startsWith(newTask.Subject.toLowerCase('email:'))){
                    newTask.Hubspot_generated__c = 'To Sync';
            }
        }
      }
    }
}

Appreciate any ideas?

Kellie 
Akhil ReddyAkhil Reddy
please remove  'email:' in this line if(String.isNotBlank(newTask.Subject) && newTask.Subject.startsWith(newTask.Subject.toLowerCase('email:'))){
Kellie MillardKellie Millard
Morning Akil

I have removed 'email' as suggested but get the same result.  The hubspot field is not being populated at all now and I was expecting this to say "to sync" and the task is still coming in as unseen so the workflow isnt picking the record up.

Thoughts?

kellie 
Kellie MillardKellie Millard
This is the current Apex trigger:

trigger HubspotGenActivity on Task (before insert, before update, after insert, after update) {
if(Trigger.isInsert){
    if (Trigger.isBefore){
        for(Task newTask : trigger.new){
            if(String.isNotBlank(newTask.Subject) && newTask.Subject.startsWith(newTask.Subject.toLowerCase())){
                    newTask.Hubspot_generated__c = 'To Sync';
            }
        }
      }
    }
}
Kellie MillardKellie Millard
For the time being I have reverted to my original trigger;

trigger HubspotGenActivity on Task (before insert) {
 for(Task newTask : trigger.new){
if(
            !String.isBlank(newTask.Subject) &&
            newTask
                .Subject
                .toLowerCase()
                .startsWith('email:')
        ){
            newTask.Hubspot_generated__c = 'To Sync';
        }
    }
}
 
Kellie MillardKellie Millard
I am still struggling with this issue - can anyone share their thoughts on how i might overcome this - previous apex triggers as suggested above have failed to resolve this issue.

As a reminder to my situation: I have a custom Related Activity List which, (through the use of a custom field called "hubspot generated activity" , a workflow that updates the Custom field based on subject line of the task and a process builder), gives me the listing of all activity not generated by hubspot and this is working brilliantly - with the exception of those tasks that are created using the BCC email (email to salesforce)

For some reason the workflow is not "seeing" the email tasks as they are created in salesforce, so the workflow is not triggering and subsquently the process builder is not pulling them into the right list.  If I open each of these email tasks and re-save it, the workflow is started and the rest of the process works through to the end- so i just need a way to set the workflow off without having to manually open the record. (we are talking about 250 records+ per day). 

I understand (after many hours of forum searches) that the standard functionality of workflow rules couldnt do this (workflow cant update workflow?) so it was suggested I needed to created an Apex trigger... 

The thinking was that the Apex trigger would update the custom field "hubspot generated activity" on the task record (which on creation was blank) This field would be updated to read "To Sync" so that i could easily identify which tasks needed to be updated. The idea was that as the trigger updated the custom field, the workflow would then pick up the "to sync" field and change to "no" which in turn would then activate the process builder.  

However.. whatever we seem to do the Apex trigger the workflow is not picking up this change in record and instigating the workflow and process builder.  

What can I do to make the task be seen?  Regardlesss of whether the custom field in the task record is blank or says no - I need something that basically opens, refreshes and then closes the email task record after its created/ inserted into salesforce and then i believe it would work.   

Thanks in advance for any suggestions!

Kellie