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

Value is not updating properly



I have one custom object as service request(Service_Request__C).it is having lookup to user (filed name:Service_Assistant__c).

I am updating this field value with one user name using work flow.

After creating record in service request i want to create 2 tasks. one is with the current user name and another one is with another user name which i am updating with work flow.(Service_Assistant__c).


but my trigger is creating two tasks with same owner id.task is not getting created with the other username which i am updating with work flow..


below is my code


trigger taskcreation on Service_Request__c (after insert) {

 Task t= new Task();
 t.ActivityDate = sr.Due_Date__c;
 t.WhatId =;
 insert t;

 Task t1= new Task();
 t1.ActivityDate = sr.Due_Date__c;
 t1.WhatId =;
 if(t1.OwnerId !=null)
  t1.OwnerId = sr.Service_Assistant__r.Id;   //This is not working
 insert t1;



pleaseguide me...


Thanks in advance.....


Anup JadhavAnup Jadhav

Hello swapna9,


This is because 't1.ownerId' is equal to null in this statement:


 if(t1.OwnerId !=null)
  t1.OwnerId = sr.Service_Assistant__r.Id;   //This is not working
 insert t1;



so the 'if' condition block will not execute. The reason t1.ownerid is null is because, you have NOT inserted the task yet. I think you need to check. If you remove the if condition, it will work.


It is also worth reviewing the triggers and order of execution section in the apex docs here (


Hope this helps!


- Anup



If i remove if condition i am getting the following error:


taskcreation: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId]: Trigger.taskcreation: line 25, column 1


order of trigger execution also i checked.after firing  the work flow also trigger will fire.

but i am seeing the sr.Service_Assistant__r.Id value as null..








Anup JadhavAnup Jadhav

Hi swapna9,


It seems that there is no value in 'sr.Service_Assistant__r.Id', which is why when you execute the following statement:


t1.ownerId = sr.Service_Assistant__r.Id;


the ownerId is set to null, hence the error message.


The reason you don't have any value in Service_Assistant__c variable is because the workflow rule which assigns a value to this variable is executed after the "after insert" trigger is executed. (See the order of execution section in the document).





Thanks for you response anup,


i changed  t1.ownerId = sr.Service_Assistant__c;


Its working fine.But problem is if i clone the Service request record its working fine.

If i create new record again getting same error as above.I need to change this trigger as before insert.but i changed my trigger as before insert and i removed dml insert statement.but its not working

Anup JadhavAnup Jadhav

I am assuming that your workflow is executed on after the service request is inserted. Is that correct?


In that case, you don't need to clone anything. Put the logic in an 'after update' trigger, and it should work, because then the sr.service_assistant__c field will have the correct value that you can assign to t1.ownerId.





after insert ,after update both are same logic.

in insert we wil  create new task Task t= new Task(); .

In update we will Query Task t = [select id,WhatId from task where limit 1]; In update already task is created.

But while inserting task is not yet created...we can not query...