+ Start a Discussion
aspicciatiaspicciati 

Stuck writing a trigger to update ownership of a record with a look-up field... (Part 2)

Not sure if this is proper etiquette in the forums, but I have a continuation on a previous problem that I think should be a quick fix.  I replied to my past post, but it didnt get bumped to the top, so I'm making a new posting.  Any help in explaining would be great.

 

I am trying to do the same as here (http://boards.developerforce.com/t5/Apex-Code-Development/Stuck-writing-a-basic-trigger-to-update-ownership-of-a-record/m-p/493187), except to have any tasks (autocreated via workflow on a new case) also reassigned.  I am getting a "Save error: Invalid foreign key relationship: Task.WhatId" error on line 9.  Is this because of a similar problem that sfdcfox described in #2 above, whereas the value doesn't exist yet in a trigger?  If so, how do I get the value populated so that I can reference against it?  Or does the "WhatId" field work differently than a regular reference field?

 

Thanks so much!!  And also, for next time, should this be a new thread or is there a way to bump an existing thread to the top of the forum?  Do I need to uncheck the "Solution" that I had marked?

 

public with sharing class TaskClass {

	public static void updateTaskOwner (List<Task> tasks) {
	
    	  // map of opportunities
 			map<id,opportunity> opps = new map<id,opportunity>();
  			// obtain opportunity ids
  			for(Task t:tasks) {
    			opps.put(t.WhatId.opportunity__c,null);
  			}
  			// don't include null id
  			opps.remove(null);
  			// query all opps and place in map
  			opps.putAll([select id,impuser__c from opportunity where id in :opps.keyset()]);
  			// assign owner for each non-null value
  			for(Task t:tasks) {
    			if(opps.containskey(t.WhatId.opportunity__c) && opps.get(t.WhatId.opportunity__c).impuser__c != null) {
      				t.ownerid = opps.get(t.WhatId.opportunity__c).impuser__c;
    			}
  			}
	}
}

 

Richie DRichie D

Hi,

 

My guess would be that the Task.WhatId is a special relationship that will link to any object with tasks enabled - this is essentially just an id and so therefore when you try and access t.WhatId.Opportunity__c this will fail.

 

You will probably need to get all the whatIds in a set and query against the opportunity table and get aout the matching opportunities - any that aren't opportunities won't match and therefore be 'ignored'. Then you will be able to get the impuser__c value you use later on from the matching opportunity.

 

Regards,

Rich.

aspicciatiaspicciati

Thanks, Rich.  I'm going to keep on working on this section, but am going to just use the Case owner for the time being, as this is easily done.  I need to release this in a few days and for now, all tasks are going to the case owner.  In the future, we may want to assign based off of criteria in the task and information on the opportunity, but I think I was building it too complicated for now :)

 

I'm running into just one more problem :

 

I want to be sure that I am only updating the owner for the tasks that are being created via workflow, and not emails logged via LinkPoint, or calls logged by reps, etc.  So I created a formula field on the task called Imp_Task__c that is equal to "Yes" or "No" based off of the subject.  Because I can define the subject of my tasks being created via workflow, I figured this is the best place to identify as a workflow-created task.

 

Here is my code:

public with sharing class TaskClass {

	public static void updateTaskOwner (List<Task> tasks) {
    	  // map of cases
 			map<id,case> cases = new map<id,case>();
  			// obtain case IDs
  			for(Task t:tasks) {
    			if(t.Imp_Task__c == 'Yes'){
    				cases.put(t.WhatId,null);
    				return;
    			}
  			}
  			// don't include null id
  			//cases.remove(null);
  			// query all opps and place in map
  			cases.putAll([select id,ownerid from case where id in :cases.keyset()]);
  			// assign owner for each non-null value
  			for(Task t:tasks) {
    			//if(WhatId.type == case) {
    			if(cases.containskey(t.WhatId) && cases.get(t.WhatId).ownerid != null) {
      				t.ownerid = cases.get(t.WhatId).ownerid;
    			}
    			//}
  			}
	
	}
}

 Now, I switched it to an "After Insert", assuming that at this point, the formula field would exist, so I would be able to check for the value of it.  However, it seems that none of the tasks created are being caught by the class, and they're running right through it without anything happening.

 

Any ideas?

 

Thanks in advance!