+ Start a Discussion
Everton CP7Everton CP7 

Get OwnerID from task

I'm trying to get the same ownerID from task "test 1" to task "test 3" when I complete the task "test 2".

 

trigger CriarTarefa_Impressao_e_Nova_separacao on Task (before update) {
      
  List<Id> ownerIds = new List<Id>();
  for (Task task : Trigger.new) {
    if (task.subject == 'test 1') {
      ownerIds.add(task.ownerId);
    }
  } 
    
    List<Task> owners = [SELECT Id, ownerId FROM Task WHERE ownerId IN :ownerIds];
  	Map<Id,String> ownerIdsTo = new Map<Id,String>();
  	for (Task owner : owners) {
    ownerIdsTo.put(owner.Id,owner.ownerID); 
  }
  
    List<Task> taskNova = new List<Task>();    
    for (Task task : Trigger.new)
    if (task.subject == 'test 2' && task.status == 'Completed'){
                taskNova.add (new Task(
                         Subject = 'test 3',
                         Status = 'Nenhum',
                         WhatID = task.WhatID,
                         Description = task.description,
                    	 Ownerid = ownerIdsTo.get(task.OwnerId),
                         ActivityDate = Date.today()));
    }

 

With this code the OwnerID returns null.

@anilbathula@@anilbathula@

Hi

 

What you are trying to get createdby or AssignedTo?

If its createdby then in soql query use Createdbyid instead of ownerid.

just look at this link also:-http://blogs.developerforce.com/developer-relations/2011/03/a-neat-soql-trick.html

 

Everton CP7Everton CP7

It is possible to be createdby or AssignedTo.

 

I changed.

But still giving me the same error.

Manos SpanoudakisManos Spanoudakis

Question: Do you update all 3 Tasks with one bulk update or just Test 2 ?

 

If you only update Test 2 then your ownerIds list will be empty, your SoQL will return empty list and thus Map is empty...

Bhawani SharmaBhawani Sharma
trigger CriarTarefa_Impressao_e_Nova_separacao on Task (before update) {

	Id firstTaskOwnerId;
	for (Task task : Trigger.new) {
		if (task.subject == 'test 1') {
			firstTaskOwnerId = task.ownerId;
		}
	} 
    
    List<Task> taskNova = new List<Task>();    
    for (Task task : Trigger.new) {
		if (task.subject == 'test 2' && task.status == 'Completed') {
                taskNova.add (new Task(
                         Subject = 'test 3',
                         Status = 'Nenhum',
                         WhatID = task.WhatID,
                         Description = task.description,
                    	 OwnerId = firstTaskOwnerId;
                         ActivityDate = Date.today()));
		}
	}
}

 Try this.

EvertonSzekeresEvertonSzekeres

I had to create another account here.

I changed some things in my configuration.

 

 

I tried put this ID firstTaksOwnerId;

And the same happens.

 

I will get a better explanation.

 

"Test" 1 is one task that is been created by workflow.

From this task my trigger begins.

 

 

trigger CriarTarefa_Impressao_e_Nova_separacao on Task (before update) {
      
  List<Id> ownerIds = new List<Id>();
  for (Task task : Trigger.new) {
    if (task.subject == 'test 1') {
      ownerIds.add(task.ownerId);
    }
  } 
    
    List<Task> owners = [SELECT Id, ownerId FROM Task WHERE ownerId IN :ownerIds];
  	Map<Id,String> ownerIdsTo = new Map<Id,String>();
  	for (Task owner : owners) {
    ownerIdsTo.put(owner.Id,owner.ownerID); 
  }
  
    List<Task> taskNova = new List<Task>();    
    for (Task task : Trigger.new)
    if (task.subject == 'test 1' && task.status == 'Completed'){
                taskNova.add (new Task(
                         Subject = 'test 2',
                         Status = 'Nenhum',
                         WhatID = task.WhatID,
                         Description = task.description,
                    	 Ownerid = '005U0000001KrYv',
                         ActivityDate = Date.today()));
    }
List<Task> taskNova = new List<Task>();    
    for (Task task : Trigger.new)
    if (task.subject == 'test 2' && task.status == 'Completed'){
                taskNova.add (new Task(
                         Subject = 'test 3',
                         Status = 'Nenhum',
                         WhatID = task.WhatID,
                         Description = task.description,
                    	 Ownerid = ownerIdsTo.get(task.OwnerId),
                         ActivityDate = Date.today()));
    }
                insert taskNova;
            }

 

I have much more tasks in this same trigger, but its just copy and paste.

Manos SpanoudakisManos Spanoudakis

If Task1 is Created then it's not in your Trigger context... Your trigger fires only Before Update 

Am I missing something ?

EvertonSzekeresEvertonSzekeres

This will works if I pull the creator's case too.

 

Task "test 1" its always from a case.

 

I already tried this and didn't work. Probably I did something wrong.

Some help?

EvertonSzekeresEvertonSzekeres

That's right, "Test 1" it's not in my trigger context.

But this works if I put user ID in there.

Why didn't work if I try put ownerID from "test 1"?

Bhawani SharmaBhawani Sharma

Can you put a debug statement there to see what Owner Id you are getting in trigger from first task?

Manos SpanoudakisManos Spanoudakis

Because probably the User.Id somehow exists in the Map  and thus you get a result from the get operation ;-)

EvertonSzekeresEvertonSzekeres

FINALLY I DID !!!

 

trigger CriarTarefa_Impressao_e_Nova_separacao on Task (before update) {
  
    Id casecreatedbyId;
	  for (Case c : [Select id, motivo__c, createdbyid from Case]){
		if (c.motivo__c == 'Impressão de álbum') {
			casecreatedbyId = c.createdbyid;
		}
	} 

    List<Task> taskNova = new List<Task>();
    for (Task task : Trigger.new)
    if (task.subject == 'test 1' && task.status == 'Concluído'){
                taskNova.add (new Task(
                         Subject = 'test 3',
                         Status = 'Nenhum',
                         WhatID = task.WhatID,
                         Description = task.description,
                    	 OwnerId = casecreatedbyId,
                         ActivityDate = Date.today()));
    }

 

Thanks all of you guys !

For attention and help !