You need to sign in to do that
Don't have an account?
[Urgent] Buggy Case Trigger. Need Help!!!!
Hi guys this is the trigger which is buggy.
trigger taskassign on Case (after insert) {
for (Case c : trigger.new)
{
Event[] ownerinfo = [Select Id,OwnerId from Event where CreatedDate = today];
//query the ownerid of event
Case[] caseinfo = [Select Id,OwnerId from Case where CreatedDate = today AND Status = 'New'];
for(Integer x=0; x <ownerinfo.Size();x++)
{
Integer otheruser = [Select Count() from Event where OwnerId !=:ownerinfo[x].OwnerId AND CreatedDate = today];
//count the number of events created today & owned by the user
Integer assigned = [Select Count() from Case where CreatedDate = today AND OwnerId =:ownerinfo[x].OwnerId AND Status = 'New'];
//count the number of cases created today & owned by the user
Integer notassigned = [Select Count() from Case where CreatedDate = today AND OwnerId !=:ownerinfo[x].OwnerId AND Status = 'New'] / otheruser;
//count the number of cases created today & not owned by the user
//Case[] updated = [Select OwnerId from Case Where CreatedDate = today AND Status = 'New' and LastModifiedById !=: caseinfo[x].OwnerId];
for(Integer y=0; y <caseinfo.Size();y++)
{
if(caseinfo[y].OwnerId != ownerinfo[x].OwnerId)// if case ownerid is not = event ownerid
{
if(assigned < notassigned)// if case owned by user less than cases owned by other ppl divided by their number
{
caseinfo[y].OwnerId = ownerinfo[x].OwnerId;
}
}
}
}
update caseinfo;
}
}
This trigger fires whenever a case is created/cloned, thus getting the ownerid from Event and changed the newly created/cloned Case to the person in the Event. The issue here is that it update all records instead of the newly created/cloned Case.
Can you guys help out thanks!
I am not entirely certain what your code is trying to achieve. What is the purpose of the events, and how are they related to the cases? There's quite a bit that seems to be unclear as to your intent. However, please allow me to make some suggestions, following through the code I do (mostly) understand: