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
Everton CP7Everton CP7 

Task OwnerID

I'm trying to add an specific OwnerID to an specific task.

But I couldn't query my others tasks.

 

I'm trying to select the OwnerID from task "1X".

And put in the task "3X" when I complete the task "2X".

 

trigger CriarTarefa_Nova_separacao on Task (after update) {
    
    List<Id> creatorIds = new List<Id>();
  	for (Task task : Trigger.new) {
    if (task.Status == '1X') {
      creatorIds.add(task.CreatedById);
    }
  } 
    
    List<Task> creators = [Select Id, CreatedById from Task Where Id in :creatorIds];
  	Map<Id,String> creatorIdsTo = new Map<Id,String>();
  	for (Task creator : creators) {
    creatorIdsTo.put(creator.Id,creator.CreatedByID);
  }
    
    List<Task> taskNova = new List<Task>();
    for (Task tt : Trigger.new)
    if (tt.subject == '2X' && tt.status == 'Completed'){
                taskNova.add (new Task(
                         Subject = '3X',
                         Status = 'Nenhum',
                         WhatID = tt.WhatID,
                         Description = tt.description,
                    	 OwnerId = creatorIdsTo.get(tt.CreatedById),
                         ActivityDate = Date.today()));
    }
insert taskNova;
}

 My error is:

"Assigned to: Assigned to ID: owner can not be blank"

 

Thanks !

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger CriarTarefa_Nova_separacao on Task (after update) {
    
    List<Id> creatorIds = new List<Id>();
  	for (Task task : Trigger.new) {
    if (task.Status == '1X') {
      creatorIds.add(task.CreatedById);
    }
  } 
    
//    List<Task> creators = [Select Id, CreatedById from Task Where Id in :creatorIds]; // you are checking task Id with User Ids.
List<Task> creators = [SELECT Id, CreatedById FROM Task WHERE CreatedById IN :creatorIds];
Map<Id,String> creatorIdsTo = new Map<Id,String>(); for (Task creator : creators) { creatorIdsTo.put(creator.Id,creator.CreatedByID); } // So the map will always be null. List<Task> taskNova = new List<Task>(); for (Task tt : Trigger.new) if (tt.subject == '2X' && tt.status == 'Completed'){ taskNova.add (new Task( Subject = '3X', Status = 'Nenhum', WhatID = tt.WhatID, Description = tt.description, OwnerId = creatorIdsTo.get(tt.CreatedById), // returns null from the map ActivityDate = Date.today())); } insert taskNova; }

 

All Answers

Naidu PothiniNaidu Pothini
trigger CriarTarefa_Nova_separacao on Task (after update) {
    
    List<Id> creatorIds = new List<Id>();
  	for (Task task : Trigger.new) {
    if (task.Status == '1X') {
      creatorIds.add(task.CreatedById);
    }
  } 
    
//    List<Task> creators = [Select Id, CreatedById from Task Where Id in :creatorIds]; // you are checking task Id with User Ids.
List<Task> creators = [SELECT Id, CreatedById FROM Task WHERE CreatedById IN :creatorIds];
Map<Id,String> creatorIdsTo = new Map<Id,String>(); for (Task creator : creators) { creatorIdsTo.put(creator.Id,creator.CreatedByID); } // So the map will always be null. List<Task> taskNova = new List<Task>(); for (Task tt : Trigger.new) if (tt.subject == '2X' && tt.status == 'Completed'){ taskNova.add (new Task( Subject = '3X', Status = 'Nenhum', WhatID = tt.WhatID, Description = tt.description, OwnerId = creatorIdsTo.get(tt.CreatedById), // returns null from the map ActivityDate = Date.today())); } insert taskNova; }

 

This was selected as the best answer
Everton CP7Everton CP7

Still giving me the same error.

 

Owner's first task is the same owner's case.

 

Maybe if a create an map to pull the owner's case.

Can you help me?

Everton CP7Everton CP7

I changed a few steps and works.

 

List<Id> creatorIds = new List<Id>();
    for (Case c : [Select id, CreatedById, motivo__c from Case where motivo__c = 'Impressão de álbum']) {
    if (c.motivo__c == 'Impressão de álbum') {
      creatorIds.add(c.CreatedById);
    }
  } 
    
    List<User> creators = [Select Id From User Where Id in :creatorIds];
  	Map<Id,String> creatorIdsTo = new Map<Id,String>();
  	for (User creator : creators) {
    creatorIdsTo.put(creator.Id,creator.id);
  }

 

Thanks for help Naidu.