+ Start a Discussion
re_plagasre_plagas 

[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!

sfdcfoxsfdcfox

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:

 

 

  1. Use a "before insert" trigger, as it appears you are trying to change ownership of records that are in the process of being created. There's no need to update the records recursively.
  2. Do not use your queries inside the for loop. You are inviting disaster down the road; you can't assume that only a few cases will be created at once. Maybe one day you'll set up some portal that uses some type of caching mechanism that breaks because your code fails, or you need to use the Data Loader to insert a ton of archived records, etc.
  3. You can reduce the number of queries by combining the queries and looping through the results.
Maybe you'd like to elaborate on what you're trying to do, and we could see if we can fix your trigger so it works the way you expect?