You need to sign in to do that
Don't have an account?
W Chad Smith
Apex Trigger Not Updating Task Who ID
I have a trigger on Task, which should look at the WhatID, grab the contact that's associated with and then populate the task WhoID for that contact. What's strange is that the task field updates related to description and Contact_ID_Test__c are working just fine and the ID is correct, so the maps, etc. are all OK. It just seemas as though SFDC isn't letting me update the WhoID Field. Here's the code, and thanks in advance!
trigger trBeforeInsertBeforeUpdateTask on Task (before insert, before update, after insert, after update) { //Set of WhoIDs being updated set<ID> whoIDs = new set<ID>(); //Set of What IDs being updated set<ID> whatIDs = new set<ID>(); //Map of Contact IDs and Contacts map<ID,Contact> contactMap = new map<ID,Contact>(); //Map of Product Interest IDs map<ID,Product_Interest__c> productInterestMap = new map<ID,Product_Interest__c>(); //List of tasks for after update list<Task> tasksToUpdate = new list<Task>(); //Get all of the whoIDs of the tasks being inserted/updated for (task t : trigger.new) { whoIDs.add(t.whoid); whatIDs.add(t.whatid); } //Populate contactMap with the contact IDs and contact record for (contact c : [select id, Extension__c from contact where id in : whoIDs]) { contactMap.put(c.id,c); } //Populate productInterestMap with the IDs from the Task Record for (Product_Interest__c pi : [select id, Contact__c from Product_Interest__c where id in : whatIDs]) { productInterestMap.put(pi.id,pi); } if (trigger.isBefore) { for (task t : trigger.new) { if (contactMap.containsKey(t.whoid)) { t.ext__c = contactMap.get(t.whoID).Extension__c; } if (t.WhoId == null & productInterestMap.containsKey(t.whatid)) { string contactID = productInterestMap.get(t.whatid).Contact__c; contactID = contactID.substring(0,15); id testid = contactID; t.WhoId = testid; t.Contact_ID_Test__c = contactID; t.Description = date.today() + ':' + productInterestMap.get(t.whatid).Contact__c; System.Debug(':::: T What ID:::' + t.whatid); System.Debug(':::t.WhoID::::' + t.WhoID); System.Debug('::ContactID:::' + productInterestMap.get(t.whatid).Contact__c); } } } }
The debug log looked fine, everything was pulling a correct Contact ID, I even had it updating a text field, and that worked fine, just the WhoID that would not update. I was able to solve the issue using an Is After trigger, this is working fine. Code is below in case anyone else runs into the problem.
All Answers
I think here is the problem.
your assigning productInterestMap.get(t.whatid).Contact__c value which is id to string variable and then substring value which is also string is assining to id variable.
Can you try by assigning productInterestMap.get(t.whatid).Contact__c value directly to t.whoId.
//productInterestMap.get(t.whatid).Contact__c value is a id of contact, which is id data type.
if (t.WhoId == null && productInterestMap.containsKey(t.whatid))
{
t.WhoId = productInterestMap.get(t.whatid).Contact__c;
// Contact_ID_Test__c is text then string.valurof converts id into string.
t.Contact_ID_Test__c = (string.valueof(productInterestMap.get(t.whatid).Contact__c)).substring(0,15);
}
That's how I had it originally, but it wasn't working. Changed it back to what you suggested and still no dice. I'm beginning to think there's a problem w/ the SFDC platform not allowing Who ID updates, I'm toying now with creating a brand new task, and when I do that the Who ID gets populated just fine... I could then go back and delete the original task, but this seems like a sledge hammer approach.
Hi,
Can you send debug log when you are creating and updateing task.
We can do ypdate of whoId in salesforce.
Please see below link for task whoId properties.
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_task.htm
The debug log looked fine, everything was pulling a correct Contact ID, I even had it updating a text field, and that worked fine, just the WhoID that would not update. I was able to solve the issue using an Is After trigger, this is working fine. Code is below in case anyone else runs into the problem.