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
Jyoti SinghJyoti Singh 

Task not created by trigger using upsert for firs time .Gets created while updating case.

Hi

I am using trigger to create task for a case.Using upsert .everytime user saves case for the first time everything else trigger is doing are performed ,but task creation.While updating that record task gets created .can anyone help PLS.?

 

Using code

 

 if(someCondition){
               case1.Submit_Date__c = date.Today();
               case1.Status = 'Submitted';
               dateTime openedDate = case1.Actual_Date_Opened__c;
               dateTime finalDueDate =openedDate.addDays(7);
               List<User> userIdList =[Select id from User where Email = : 'fjkhfrj@jfjd.com'];
               
               Task tsk = new Task(whatID = case1.ID, Ownerid =userIdList[0].id, Subject = 'XYZ', ActivityDate = finalDueDate.date() , IsReminderSet = true, recordTypeId = 'XYZ');
               tasks.add(tsk);
             } 

if(tasks != null && tasks.size() > 0){
               upsert  tasks;
            }

Neha LundNeha Lund

Trigger Event before insert is considered in your case trigger ?

Jyoti SinghJyoti Singh

before/After Insert/Update is considered..

souvik9086souvik9086

Hi,

 

Try the changes in blue colored text

 

if(someCondition){
               case1.Submit_Date__c = date.Today();
               case1.Status = 'Submitted';
               dateTime openedDate = case1.Actual_Date_Opened__c;
               dateTime finalDueDate =openedDate.addDays(7);
               List<User> userIdList =[Select id from User where Email = : 'fjkhfrj@jfjd.com'];
               
               Task tsk = new Task(whatID = case1.ID, Ownerid =userIdList[0].id, Subject = case1.Subject, ActivityDate = finalDueDate.date() , IsReminderSet = true, recordTypeId = 'XYZ');
               tasks.add(tsk);
             } 

if(tasks != null && tasks.size() > 0){
               upsert  tasks;
            }

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Jyoti SinghJyoti Singh

Actually some hardcoded values are needed to be given as task subject not from case.

souvik9086souvik9086

Another thing why you gave it like this?

List<User> userIdList =[Select id from User where Email = : 'fjkhfrj@jfjd.com'];

 

Can you change it like this?

List<User> userIdList =[Select id from User where Email = 'fjkhfrj@jfjd.com'];

 

You are assigning this user to the ownerid. So please change that and let me know what happens.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

Jyoti SinghJyoti Singh

Syntax i snot the problem.I have added email label,thats why accessing with':' .but when Case is created task is not added.If you update it to satisfy the condition then Task gets added.something wrong with UPSERT DML command I am adding.Please Suggest anything ..

souvik9086souvik9086

Yes the problem is with the UPSERT DML command, but you wrote that when you update case then it is working fine i.e task is creating. 

 

You can do one thing you can give condition like 

if(Trigger.isAfter){

//Your Code

}

 

Try this and let me know what happened.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Jyoti SinghJyoti Singh
if(someCondition){
case1.Submit_Date__c = date.Today();
case1.Status = 'Submitted';
dateTime openedDate = case1.Actual_Date_Opened__c;
dateTime finalDueDate =openedDate.addDays(7);
List<User> userIdList =[Select id from User where Email = : 'fjkhfrj@jfjd.com'];

Task tsk = new Task(whatID = case1.ID, Ownerid =userIdList[0].id, Subject = case1.Subject, ActivityDate = finalDueDate.date() , IsReminderSet = true, recordTypeId = 'XYZ');
tasks.add(tsk);
}

if(tasks != null && tasks.size() > 0){
upsert tasks;
}




case status ans submit date is getting updated it means control is coming to this block ,but its not creating task at the same time is case is being created.
souvik9086souvik9086

Yes I understand that it is updating. that will be updated in both before and after, but if you want to create a new task based on that case id it had to be after trigger. Thats why I told to try with the after condition. Check once what happen with that.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Jyoti SinghJyoti Singh

Had to use a future method call,Tasks were geting created they were not getting linked to case ,as no whatID was there when tasks list was inserted.