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
Sean BarczynskiSean Barczynski 

Updating Account based on Task created by Salesforce for Outlook

Hello,

I'm trying to update an Account field based upon certain criteria of an email that is sent in Outlook and synced to Salesforce using SFO.  When I manually create the task in Salesforce, the trigger works, but it doesn't work when I sync one using SFO.  

Here are the relevant snippets from my code:
 
trigger taskTrigger on Task (before insert, before update) 
{
	
	Set <Id> whatIdSet = new Set <Id> ();
    Set <Id> whoIdSet = new Set <Id> ();
    Contact contact;
	
    for(Task t : trigger.new)
    {
        if(t.WhatId != null)
        {
            whatIdSet.add(t.WhatId);
        }
        
        if(t.WhoId != null)
        {
            whoIdSet.add(t.WhoId);
        }
    }
        Map<ID, Account> accountMap = new Map<ID, Account>([select Id, 
    														   Name, 
                                        					   Number_of_Attempts_to_Schedule_Review__c, 
                                        					   Number_of_Attempts_to_Schedule_TaxPlan__c, 
                                        					   Number_of_Attempts_to_Schedule_TaxPrep__c
                                   					   	  from Account 
                                  					     Where Id in :whatIdSet]);

    Boolean accountNeedsUpdating = FALSE;
    Boolean attemptToScheduleReview = FALSE;
    List <Account> AccountsToUpdate = new List <Account> ();
    String whatObjectType;
    Account household;

    for(Task t : trigger.new)
    {    	
        if(t.WhatID != NULL && accountMap.containsKey(t.WhatId))
    	{
    		whatObjectType = 'household';
    		household = accountMap.get(t.WhatId);
    	}

if(trigger.isInsert)
    	{
    		if(t.Subject=='Email:Scheduling a Review Meeting')
    		{
    			if(t.Attempt_to_Schedule__c == NULL)
    				t.Attempt_to_Schedule__c = 'Client Review Meeting';
    			else
    				t.Attempt_to_Schedule__c += ';Client Review Meeting';
    				
    			attemptToScheduleReview = TRUE;
    		}

if(t.WhatId != NULL && whatObjectType == 'household')
			{
				if(t.Attempt_to_Schedule__c != NULL && (t.Attempt_to_Schedule__c.contains('Client Review Meeting') || attemptToScheduleReview))
				{
					if(household.Number_of_Attempts_to_Schedule_Review__c == NULL)
						household.Number_of_Attempts_to_Schedule_Review__c = 1;
					else
						household.Number_of_Attempts_to_Schedule_Review__c++;
						
					accountNeedsUpdating = TRUE;
				}
}
		if(accountNeedsUpdating)
			AccountsToUpdate.add(household);
					
		accountNeedsUpdating = FALSE;

try
    {
    	if(AccountsToUpdate.size()>0)
    	{
        	update AccountsToUpdate;
    	}
    }
    
    catch (System.DmlException ex)
    {
        System.Debug (ex);
    }
}

Any help is greatly appreciated!
SandhyaSandhya (Salesforce Developers) 
Hi Sean Barczynski,

Please check what values you are getting for whatId and WhoId.

After some research, I found this.

With Salesforce for Outlook, associations are completed after the Task is created, so the Task.WhoId and Task.WhatId fields aren’t immediately available for insert and update events, and their values are initially null. The WhoId and WhatId fields are set on the saved task record in a subsequent operation, however, so their values can be retrieved later. 

There is some workaround here in below links.

https://developer.salesforce.com/forums?id=906F000000095HTIAY
 
http://salesforce.stackexchange.com/questions/24194/trigger-off-event-added-via-outlook-side-panel
 
Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya

 
Sean BarczynskiSean Barczynski
Hi Sandhya,

Thank you for the reply.  I tried using the @future method, but it didn't seem to wait long enough to run and was running before WhatId or WhoId were populated.  I began looking into the solution with the Schedulable implementation, but then I realized that the email address that the email is going to is stored in the Task Description.  I wrote a trigger to pull that email address out and look up a contact or lead with that email address.  From there, I looked up Accounts and Opportunities and I was on my way.

Thank you for your help!

Sean