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
magdamagda 

create Task with Apex code // Attempt to de-reference a null object

Hi,

I want to create a Task after saving a customer Object.
In my Customer Object I give a User FIeld: Editor.
I try with this Code

Code:
task.WhatId = Object.id;
task.WhoId = Object.Editor__c;
task.Subject = 'Other';
task.priority= Object.Priority__c;
task.status = 'Not Started';
task.description = 'New  Work';
insert task;

 And I become this failure:

Attempt to de-reference a null object

Do you have an Idea to help me?

Thx

Magda




Message Edited by magda on 02-13-2008 12:32 AM
Best Answer chosen by Admin (Salesforce Developers) 
hector.asp2hector.asp2
hi arun
convert ActivityDate date to datetime variable

Like

datetime DateVar=new datetime ("1/7/2009");
SfObj.ActivityDate=DateVar;

hector.asp2@gmail.com
Message Edited by hector.asp2 on 06-03-2009 03:59 AM

All Answers

SuperfellSuperfell
you missed Task task = new Task();
magdamagda
Hi,

I have on the top of the class: Task task = new Task();


And it is not running....


Do you have another Idea?
magdamagda
Hi,

know i becom this failure

Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Contact/Lead ID: id value of incorrect type: 005700000014lZZAAY: [WhoId]



And 005700000014lZZAAY is a user.... Do you have an Idea?

Thx

Magda

Ok Solution: Not using WhoId but OwnerId ;)
Maybe can help another person having the same problem

Message Edited by magda on 02-13-2008 02:32 AM

Message Edited by magda on 02-13-2008 02:58 AM
Nazeer AhamedNazeer Ahamed

Magda,

I need to create the same, could you please post your full code or guide me where i can access related samples.

thanks you very much

TCAdminTCAdmin
Hello magda,

Unfortunately the WhoId can only point to a Contact or a Lead. This will not accept a User id. You can't use the WhatId because it will also not allow you to use a User Id in it. Within a task you can only use a UserID in a custom lookup field or the Owner field.
macedon2020macedon2020

hi all

 

 i am trying to create Task with Apex code and flexbut it not working at all Please help me out 

 

 

    var SfObj:SObject=new SObject("Task");
                    SfObj.ActivityDate="1/7/2009";
                    SfObj.Subject="Subject";
                    SfObj.Status="Active";
                    SfObj.Description="Description";
                    SfObj.WhoId="00Q8000000111ww";

 

thanks 

arun thakur 

er.arunthakur@gmail.com

hector.asp2hector.asp2
hi arun
convert ActivityDate date to datetime variable

Like

datetime DateVar=new datetime ("1/7/2009");
SfObj.ActivityDate=DateVar;

hector.asp2@gmail.com
Message Edited by hector.asp2 on 06-03-2009 03:59 AM
This was selected as the best answer
MunmunMunmun

Can any body suggest how  create stand alone task using apex coding..

 

 

Is it possible that we can create tak inside the case....plz plz suggest.

 

 

can we have sub cases inside the cases

 

Ahmed AbbasAhmed Abbas

 

You have to assign the user to the tasks "OwnerId" not its "WhoId", like follows:

 

myTask.OwnerId = myAccount.OwnerId;

 

*NOT* like this:

 

Task.WhoId = Account.OwnerId;

 

and also *NOT* like this:

 

Task.WhoId = Account.Owner.Id;

 

As that will likely assign it to the current user rather than the record owner.

Carls.ax1844Carls.ax1844

I had the same issue as magda. The problem was that I had a trigger firing "before insert" that referenced Trigger.oldMap, which obviously doesn't yet exist. I fixed the trigger by adding "if (Trigger.isInsert)" set of logic to avoid references to Trigger.oldMap before insert.

Naresh AltokkNaresh Altokk
Facing below problem while assigning Acitvity Date

A value cannot be stored to ActivityDate in type Task

Thanks,
Naresh