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
Mike 317Mike 317 

New to APEX, need to change case ownership

Hey All, 

I need a way to allow a user to reassign a case when they are creating it.

I created a picklist with user aliases. I'd like to be able to choose a value on that picklist and have APEX change the owner after the record is created. Can that be done? I know I can't do it with a workflow, so I'm stuck. Any help would be appreciated. Thanks!

-Mike
sharathchandra thukkanisharathchandra thukkani
based on alias you can query on the user object and get the record id and assign it to OwnerId field of the record.

You can do this in the before insert and before update event of the trigger.
AshlekhAshlekh
Hi Mike,
Case c = [select id, ownerid from Case where id = 'your record id'];
c.ownerId = 'new id of owner';
update c;

Bulk handling
List<Case> clist = new List<Case>();
for(Case c : Trigger.new or [select id . ownereid from Case where id in:<your list of id>])
{
c.ownerId = 'new ownen id or user id';
clist.add(c);
}

if(clist.size()>0) //if this not a part of trigger
update clist

-Thanks
Ashlekh Gera
Mike 317Mike 317
Thanks AKG. How do I get the new owner's ID? All I've got to work with is their Alias. 

-Mike
AshlekhAshlekh
Hi,
 
User u = [select id from User where Alias = 'ashlekh' limit 1];
String ownerId = u.id;

Case c = [select id, ownerid from Case where id = 'your record id'];
c.ownerId = ownerId;
update c;

Bulk handling
List<Case> clist = new List<Case>();
for(Case c : Trigger.new or [select id . ownereid from Case where id in:<your list of id>])
{
c.ownerId = ownerId;
clist.add(c);
}

if(clist.size()>0) //if this not a part of trigger
update clist



-Thanks
Ashlekh Gera
LakshmanLakshman
Write a simple trigger on case object, below goes the code:
trigger GenerateOwnerFromAlias on Case (after insert, before update) {
	Map<Id,Case> caseToUpdate = new Map<Id, Case>();
	Set<String> aliasesSet = new Set<String>();
	
    for(Case c: Trigger.new) {
		if((c.User_Aliases__c <> null && Trigger.isInsert) OR (Trigger.isUpdate && c.User_Aliases__c <> Trigger.oldMap.get(c.Id) && c.User_Aliases__c <> null)) {
			caseToUpdate.put(c.Id, c);
			aliasesSet.add(c.User_Aliases__c);
		}
	}
	if(!aliasesSet.isEmpty()) {
		List<Case> lstCaseToUpdate = new List<Case>();//this will update the case for Insert scenario
		Map<String, Id> mapAliasToId = new Map<Id, String>();
		for(User u: [Select Id, Alias from usere where Alias =: aliasesSet]) {
			mapAliasToId.put(u.Alias, u.Id);
		}
		for(Case c: Trigger.new) {
			if(Trigger.isUpdate) {
				if(caseToUpdate.containskey(c.Id) && mapAliasToId.containskey(c.User_Aliases__c)) {
					c.OwnerId = mapAliasToId.get(c.User_Aliases__c);
				}
			} else if(Trigger.isInsert && mapAliasToId.containskey(c.User_Aliases__c)) {
				c.OwnerId = mapAliasToId.get(c.User_Aliases__c);
				lstCaseToUpdate.add(c);
			}
		}
		
		if(!lstCaseToUpdate.isEmpty()) {
			update lstCaseToUpdate;
		}
	}

}

The above code is not compiled but the logic should work 100%. Please share the results with us.