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
erakanerakan 

Help: Can't change Task's WhoId value.

Hi everybody. We have a trigger on task object which works before insert and changes WhoId value like this:

trigger x on Task (before insert ) {
	Set<String> taskIds = new Set<String> ();
	for(Task t : trigger.new)
		taskIds.add(t.WhatId);
	Map<id, Custom_Object__c> mas = new Map<id, Custom_Object__c>([Select Id, Account__r.PersonContactId From Custom_Object__c where Account__r.IsPersonAccount = true and Id in : taskIds]);
	for(Task t : trigger.new) {
		Custom_Object__c a = mas.get(t.WhatId);
		if(a == null) continue;
		t.WhoId = a.Account__r.PersonContactId;
	}
}

 

When I create a task record. The created tasks whoId field is always NULL. When I check the debug logs, I can clearly see this trigger works and assings a new whoid value. But somehow in the end, it always returns to null and there are no errors, or warnings...

 

When I run someting like update new Task(ID='XXX', WhoID='YYY'); (where the values of XXX is a Task ID and YYY is a PersonAccount Contact ID) from the developer console, I can see that WhoID value is changing.

 

We are using Person Accounts. The Custom_Object__c has a lookup field for Account object named Account__c. 

 

What do you think, what is the problem of this code?

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

Please update the question with proper __c and __r .The code in some places refres __r and some place __c

erakanerakan

mohit_shrivastava wrote:

Please update the question with proper __c and __r .The code in some places refres __r and some place __c


Hello mohit_shrivastava. I've updated the original post with the proper tags. Sorry for the inconvenience.

vishal@forcevishal@force

Can you add a debug statement in the last loop

 

for(Task t : trigger.new) {
Custom_Object__c a = mas.get(t.WhatId);

system.debug('===========     custom object:  ' + a);
if(a == null) continue;
t.WhoId = a.Account__r.PersonContactId;
}

 

Let me know the output.

erakanerakan

vishal@force wrote:

Can you add a debug statement in the last loop

 

for(Task t : trigger.new) {
Custom_Object__c a = mas.get(t.WhatId);

system.debug('===========     custom object:  ' + a);
if(a == null) continue;
t.WhoId = a.Account__r.PersonContactId;
}

 

Let me know the output.


Hello, I've added the debug line and output as follows:
21:58:15.163 (163877000)|LIMIT_USAGE|[37]|SCRIPT_STATEMENTS|4|200000
21:58:15.163 (163925000)|SYSTEM_METHOD_ENTRY|[37]|MAP<Id,Custom_Object__c>.get(Object)
21:58:15.163 (163964000)|SYSTEM_METHOD_EXIT|[37]|MAP<Id,Custom_Object__c>.get(Object)
21:58:15.163 (163976000)|VARIABLE_SCOPE_BEGIN|[37]|a|Custom_Object__c|true|false
21:58:15.164 (164012000)|VARIABLE_ASSIGNMENT|[37]|a|{"serId":1,"value":{"Account__r":{"serId":2,"value":{"PersonContactId":"003M000000Idqi4IAB","Id":"001M000000LI3fvIAD"}},"Account__c":"001M000000LI3fvIAD","Id":"a0cM0000000t6CaIAI"}}|0x63b7470e
21:58:15.164 (164023000)|STATEMENT_EXECUTE|[38]
21:58:15.164 (164028000)|LIMIT_USAGE|[38]|SCRIPT_STATEMENTS|5|200000
21:58:15.164 (164038000)|HEAP_ALLOCATE|[38]|Bytes:32
21:58:15.164 (164066000)|SYSTEM_METHOD_ENTRY|[38]|String.valueOf(Object)
21:58:15.164 (164143000)|HEAP_ALLOCATE|[38]|Bytes:72
21:58:15.164 (164158000)|SYSTEM_METHOD_EXIT|[38]|String.valueOf(Object)
21:58:15.164 (164177000)|HEAP_ALLOCATE|[38]|Bytes:104
21:58:15.164 (164197000)|SYSTEM_METHOD_ENTRY|[38]|System.debug(ANY)
21:58:15.164 (164208000)|USER_DEBUG|[38]|DEBUG|===========     custom object:  Custom_Object__c:{Id=a0cM0000000t6CaIAI, Account__c=001M000000LI3fvIAD}
21:58:15.164 (164217000)|SYSTEM_METHOD_EXIT|[38]|System.debug(ANY)
21:58:15.164 (164224000)|STATEMENT_EXECUTE|[39]
21:58:15.164 (164229000)|LIMIT_USAGE|[39]|SCRIPT_STATEMENTS|6|200000
21:58:15.164 (164234000)|STATEMENT_EXECUTE|[40]
21:58:15.164 (164237000)|LIMIT_USAGE|[40]|SCRIPT_STATEMENTS|7|200000
21:58:15.164 (164822000)|VARIABLE_ASSIGNMENT|[40]|this.WhoId|"003M000000Idqi4IAB"|0x1f40ccaf
21:58:15.164 (164843000)|SYSTEM_METHOD_ENTRY|[36]|system.ListIterator.hasNext()
21:58:15.164 (164859000)|HEAP_ALLOCATE|[36]|Bytes:5
21:58:15.164 (164872000)|SYSTEM_METHOD_EXIT|[36]|system.ListIterator.hasNext()
21:58:15.164 (164889000)|VARIABLE_ASSIGNMENT|[36]|t|null|
21:58:15.954 (164908000)|CUMULATIVE_LIMIT_USAGE
21:58:15.954|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 7 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

21:58:15.954|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
21:58:15.954|STATIC_VARIABLE_LIST|
  double:MIN_NORMAL:0
  double:POSITIVE_INFINITY:0
  long:serialVersionUID:0
  Boolean:TRUE:0
  double:MIN_VALUE:0
  int:SIZE:0
  int[]:sizeTable:0
  char[]:DigitOnes:0
  String:_sfdcSuppressedCodeLocations:0
  char[]:DigitTens:0
  double:NaN:0
  double:NEGATIVE_INFINITY:0
  int:MIN_VALUE:0
  int:SIZE:0
  double:MAX_VALUE:0
  long:serialVersionUID:0
  int:MAX_EXPONENT:0
  int:MIN_EXPONENT:0
  Boolean:FALSE:0
  int:MAX_VALUE:0
  char[]:digits:0
  long:serialVersionUID:0

21:58:15.954|CUMULATIVE_LIMIT_USAGE_END

21:58:15.165 (165131000)|CODE_UNIT_FINISHED|x on Task trigger event BeforeUpdate for [00TM0000007Nvcx]

 

 

W Chad SmithW Chad Smith

erakan

 

I seem to be having the same problem, did you ever find a solution?

 

TIA,

Chad

erakanerakan

chad.smith76 wrote:

erakan

 

I seem to be having the same problem, did you ever find a solution?

 

TIA,

Chad


I've created a scheduleable batch class to do the same job as trigger, and scheduled it to work every minute. I can't understand why but the trigger just doesn't work.

W Chad SmithW Chad Smith

Thanks for the suggestion.  I've opened a case with Salesforce Support, I'll report back on if we can come up with a solution. 

W Chad SmithW Chad Smith

It's a bug.  If you have the ability to schedule an Activity against multiple contacts you cannot update the WhoID in a regular fashion.  What you can do, is your batch idea, tech support also suggest a future method. So my standard trigger calls a future method that actually does all the processing.  

Yogesh BiyaniYogesh Biyani
I am having the same issue.

Chad, Can you please elaborate on this "So my standard trigger calls a future method that actually does all the processing." ? How is this done? Please point me to some sample code.