1

This seems like a pretty simple trigger, but for some reason the update isn't being committed to the record. I want to automatically set the WhoId on a task based a custom field (Contact__c) on Opportunity based on the task's WhatId. When I include debug statements, the trigger fires and goes through all code, however it never updates the WhoId field.

trigger trgSetContactOnMeaningfulTask on Task (before insert, before update)  
{ 
    List<Id> oppIdList = new List<Id>();
    List<Opportunity> oppList = new List<Opportunity>();
    Map<Id, Id> oppContMap = new Map<Id, Id>();
    List<Task> taskListToUpdate = new List<Task>();

    for (Task t : Trigger.new)
    {
        if(t.WhoId == null && t.WhatId != null && String.ValueOf(t.WhatId).StartsWith('006'))
        {
            oppIdList.add(t.WhatId);
            taskListToUpdate.add(t);
        }
    }

    if(oppIdList.size() > 0)
    {
        oppList = [Select Id, Contact__c from Opportunity where Id in: oppIdList];

        if(oppList.size() > 0)
        {
            for(Opportunity o: oppList)
            {
                if(o.Contact__c != null)
                {
                    oppContMap.put(o.Id, o.Contact__c);
                }
            }
        }
    }

    if(taskListToUpdate.size() > 0)
    {
        for (Task t: taskListToUpdate)
        {
            if(t.WhoId == null && t.WhatId != null && String.ValueOf(t.WhatId).StartsWith('006'))
            {
                t.WhoId = oppContMap.get(t.WhatId);
                System.debug('WhoId: ' + t.WhoId);
            }
        }
    }
}

I've tried adding a update taskListToUpdate; line, however that throws an error due to trying to update on a before insert/update trigger.

6
  • What's the output from System.debug('WhoId: '+t.WhoId);? If it's an ID, the record should be updated. Are you sure you're reading the logs correctly?
    – sfdcfox
    Commented Oct 21, 2016 at 17:25
  • Yes, I can include the full debug log if it's useful, but here's the specific WhoId debug line: 09:50:56.28 (49296883)|USER_DEBUG|[40]|DEBUG|WhoId: 0035B000004ti0IQAQ
    – krisuvik
    Commented Oct 21, 2016 at 17:31
  • 1
    I copy-pasted your code into my org, and it actually does work.
    – sfdcfox
    Commented Oct 21, 2016 at 17:38
  • 1
    I also wrote a new version that's optimized, and tested that as well, and it also works. Other than being really hard to read, your code is just fine.
    – sfdcfox
    Commented Oct 21, 2016 at 17:40
  • Would you mind posting your version? I'm unsure why the trigger isn't updating in my org if it's working in yours. The record is successfully saving without any errors being thrown, the field just isn't updating.
    – krisuvik
    Commented Oct 21, 2016 at 17:54

1 Answer 1

0

Other than being incredibly verbose, I don't see anything wrong with your code. Here's a re-write that I also tested it, and it seems to work just fine.

trigger trgSetContactOnMeaningfulTask on Task(before insert, before update) {
    Map<Id, Id> oppConMap = new Map<Id, Id>();
    Task[] updates = new Task[0];
    for(Task record: Trigger.new) {
        if(record.WhoId == null && record.WhatId != null &&
           record.WhatId.getSObjectType() == Opportunity.SObjectType) {
            oppConMap.put(record.WhatId, null);
            updates.add(record);
        }
    }
    if(!updates.isEmpty()) {
        for(Opportunity record: [SELECT Contact__c FROM Opportunity
                                 WHERE Id = :oppConMap.keySet() AND
                                 Contact__c <> null]) {
            oppConMap.put(record.Id, record.Contact__c);
        }
        for(Task record: updates) {
            record.WhoId = oppConMap.get(record.WhatId);
        }
    }
}
2
  • Thanks - confirmed this doesn't work in my org either. However when I test both triggers in a dev org outside of my standard company's org, it does work fine. One thing I've noticed that's different is under Activity settings, my company's org has the checkbox Allow Users to Relate Multiple Contacts to Tasks and Events, while my own developer org does not. When I try updating a different field on the task, it does update. I'm going to contact SF support and disable this feature and see if this is in fact the issue.
    – krisuvik
    Commented Oct 21, 2016 at 18:54
  • 1
    @krisuvik Perhaps you need to instead insert TaskRelation records?
    – sfdcfox
    Commented Oct 21, 2016 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .