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
kprkpr 

WhatId in Lead Task updated successfully in trigger, but testCode not working

Hi,

 

I was able to develop a trigger to update the whatId of a Task associated with a Lead to that of an Account. The trigger works fine when I create a Task for a lead through the UI.(The trigger sets the whatId to that of an Account and executes successfully). But the same trigger fails when I try to invoke it using a test method. 

 

I even tried inserting Tasks using Dataloader and the trigger worked fine for this case too....only the test method wouldn't work. This is weird and I'm unable to develop a real test case for my trigger. Can anyone help please?  

EnthEnth
Can you supply the test method code? Assume you have created a Lead and an Account as part of your test case too?
kprkpr

Here is the code:

 

static testMethod void testSingleInsert() {

Account testAcc = new Account(name = 'testAcc1');

insert testAcc;

 

Lead testLead = new Lead(lastname = 'testLastname2',firstname = 'testFirstname2',leadAcc__c = testAcc.id);

insert testLead;

 

Task testTask = new Task(whoId = testLead.id);

insert testTask;

System.assertEquals(testLead.leadAcc__c , testTask.WhatId);

}

 Here is the exception I get:

 

EXCEPTION_THROWN|[40,10]|System.DmlException: Update failed. First exception on row 0 with id 00TQ0000004EUPmMAO; first error: FIELD_INTEGRITY_EXCEPTION, You cannot specify a non-null "Related To" field value with a Lead specified in the "Name" field.: [WhatId]

 

 

 

 

 

Message Edited by kpr on 03-29-2010 03:13 PM
jkucerajkucera

@kpr - you were able to associate it in the UI?  That's strange as the WhatId can't be used for leads as it is "saved" for the opportunity Id upon lead convert. 

 

Separate from that issue, in your code you didn't specify the WhatId directly in the task insert (or a subsequent update).  It won't automatically grab related ID's on a custom field such as leadAcc__c.

 

kprkpr

Hi John,

 

I have a trigger that sets the whatId of the task to that of the Account using the 'leadAcc__c' custom field. 

 

When I said I was able to do it via the UI, I meant I created a task for the lead through the UI and it fired the trigger, which updated the whatId of the task. I was also able to do it with task insertions using dataloader.

 

I didn't supply the whatId in the test method, as this test method is for the trigger. When I insert the testTask, the trigger is fired(which is supposed to set the whatId), but the update statement in the trigger fails with the above exception. 

Message Edited by kpr on 03-29-2010 04:32 PM
EnthEnth

Can you also include the code from your trigger on Task ?

 

I presume it:

1. Is a before insert trigger.

2. Checks the instanceof for the whoid to confirm it's a Lead

3. Reads the Lead record with the given id

4. Assigns the whatId of the trigger.new record to the leadAcc__c field.

 

Also worth ensuring your trigger checks that the leadAcc__c field is not null.