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
W Chad SmithW 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);
			}				
		}
	}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
W Chad SmithW Chad Smith

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.  

 

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, Contact_Ext__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 (trigger.isAfter && trigger.isInsert) {

		for (task t : trigger.new) {
			if (t.WhoId == null & productInterestMap.containsKey(t.whatid)) {
				Task newTask = t.clone(true,true);
				newTask.WhoId = productInterestMap.get(t.whatid).Contact__c;
				newTask.ext__c = productInterestMap.get(t.whatid).Contact_Ext__c;
				tasksToUpdate.add(newTask);
			}
		}
		upsert tasksToUpdate;
	}
}

 

All Answers

s_k_as_k_a

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);

 

}

W Chad SmithW Chad Smith

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.  

s_k_as_k_a

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

W Chad SmithW Chad Smith

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.  

 

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, Contact_Ext__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 (trigger.isAfter && trigger.isInsert) {

		for (task t : trigger.new) {
			if (t.WhoId == null & productInterestMap.containsKey(t.whatid)) {
				Task newTask = t.clone(true,true);
				newTask.WhoId = productInterestMap.get(t.whatid).Contact__c;
				newTask.ext__c = productInterestMap.get(t.whatid).Contact_Ext__c;
				tasksToUpdate.add(newTask);
			}
		}
		upsert tasksToUpdate;
	}
}

 

This was selected as the best answer