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
Ankit Khurana24Ankit Khurana24 

HOW ASSIGN CASE TO A USER HAVING MINIMUM NUMBER OF CASES

Whenever a new case is created, Assign case to most free user (who have minimum no of cases). There is a counter on user which show that how many cases has been assigned to an user.  Based on this counter cases will be assigned to users....

 

i   am doing like this but not able to...

 

Assigned_Cases__c is a custon field on user...

 

trigger assignUser on Case (before insert,after insert)
{

String userId = '';
Map<id,user> userMap= new Map<id,user>([select id, Assigned_Cases__c from User]);

if(Trigger.isInsert && Trigger.isBefore)
{
for(case c : Trigger.new)n
{
List<User> uu = userMap.values();
List<id> i =[select id from uu ORDER BY uu.Assign_Case__c ASC];
userId = i[0].id;
user up = userMap.get(userid);
up.Assign_Case__c++;
userMap.put(userId,up);
c.ownerId = userId;
}
update userMap.values();
}
}

 

ERROR:-Error: Compile Error: sObject type 'uu' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 12 column 41

JitendraJitendra

SOQL is not allowed on Collection. You are trying to query LIST.

 

Please try below code.

 

trigger assignUser on Case (before insert,after insert)
{
	String userId = '';
	Map<id,user> userMap= new Map<id,user>([select id, Assigned_Cases__c from User ORDER BY uu.Assign_Case__c ASC LIMIT 1]);

	if(Trigger.isInsert && Trigger.isBefore)
	{
		for(case c : Trigger.new)n
		{
		List<User> uu = userMap.values();
		userId = uu[0].id;
		user up = userMap.get(userid);
		up.Assign_Case__c++;
		userMap.put(userId,up);
		c.ownerId = userId;
		}
		update userMap.values();
	}
}

 

Ankit Khurana24Ankit Khurana24

hi..

its not working fine...

error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger assignUser caused an unexpected exception, contact your administrator: assignUser: execution of BeforeInsert caused by: System.StringException: Invalid id: : Trigger.assignUser: line 13, column 1