+ Start a Discussion
mbshickmbshick 

EmailToCase: Set case owner to specific user associated to the account

We use EmailToCase and are looking to enhance it a bit. Each account has a specific technical consultant assigned to it and they are supposed to handle Tier 1 support for the account, so we would like the case to be owned by them as soon as it is submitted. It seems that the only way of accomplishing this is via an Apex trigger, which I've been trying to write for the past couple of days. I've successfully written one previously and this one seems conceptually simple, but there seem to be some nuances that I'm just not getting. My rudimentary stab at it currently looks like this:

 

trigger AssignTechnicalConsultantToCase on Case (before insert) {
	for (Case c : trigger.new) {
		if (c.AccountId != null) {
			c.Owner = [SELECT Technical_Consultant__c FROM Account WHERE Id =: trigger.new[0].AccountId];
		}
	}	
}

 The current problem (aside from the whole approach possibly being completely wrong) is with the query. I've tried multiple iterations, none of which have compiled without an error of some sort, the latest of which doesn't even make sense to me:

 

Save error: Illegal assignment from LIST<Account> to SOBJECT:Name

I had been trying to reference the "Name" field on account and that error sort of made sense then, but now I don't know what it's trying to tell me. Technical_consultant__c is a lookup field to User on Account.

 

I also question whether this can be a "before insert" trigger -- does the account for EmailToCase cases get looked up before insert, or is it a post insert operation? Obviously if the account hasn't been assigned until after the case is inserted then I'll have to change the trigger type and I'll have more work to do. I'm going to keep pounding my head against this today, but if anyone can offer any suggestions on just how completely wrong I am, I'd appreciate it.

 

Thanks,

Matt

Best Answer chosen by Admin (Salesforce Developers) 
Mohith Kumar ShrivastavaMohith Kumar Shrivastava
trigger AssignTechnicalConsultantToCase on Case (before insert) {
	for (Case c : trigger.new) {
		if (c.AccountId != null) {
			c.Owner = [SELECT Technical_Consultant__c FROM Account WHERE Id =: trigger.new[0].AccountId LIMIT 1 ].Technical_Consultant__c;
		}
	}	
}

 Try the above one .The compiler is correct the query returns a list and you are trying to assign to a Sobject field .

 

Thanks 

All Answers

Mohith Kumar ShrivastavaMohith Kumar Shrivastava
trigger AssignTechnicalConsultantToCase on Case (before insert) {
	for (Case c : trigger.new) {
		if (c.AccountId != null) {
			c.Owner = [SELECT Technical_Consultant__c FROM Account WHERE Id =: trigger.new[0].AccountId LIMIT 1 ].Technical_Consultant__c;
		}
	}	
}

 Try the above one .The compiler is correct the query returns a list and you are trying to assign to a Sobject field .

 

Thanks 

This was selected as the best answer
mbshickmbshick

Thanks a lot, Mohith. I must have tried 50 different permutations of code to get that value and there was always something wrong with it. This makes sense and is now working in production. I couldn't actually guarantee that the Technical_Consultant__c field would be populated, so I had to add a check to that as well, making the relevant part of my code look like this:

 

		if (c.AccountId != null) {
			String TCID = [SELECT Technical_Consultant__c FROM Account WHERE Id =: trigger.new[0].AccountId LIMIT 1 ].Technical_Consultant__c;
			if (TCID != null){
				c.OwnerId = TCID;
			}
		}

 I had missed the LIMIT keyword and I still don't get why you're basically forced to requery the result of the select to get the value that you asked for to begin with, but at least I know how it works now.

 

Thanks again,

Matt