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
zero1578zero1578 

Issue Updating Ownership - Dev Please Review

I have created a case assignment logic tool out of a set of s-controls.
 
Essentially we have teams of customer service representatives.
Each team is a public group in salesforce.  Each group consists of various users.
 
My tool determines who you are, what team you belong to and then compiles a list of all the cases in the "queues" you are supposed to be working.  It then passes each of those cases through a bunch of filters and scoring functions to determine which item is the most important to be worked next.
 
Once it determines the most important item, it assigns that item to the user, and they become the owner of that case.
 
The issue I am running into is multiple people using the tool at the same time, or within seconds of each other, are both becoming the owner of the case.
 
User 1 at 10:00:01 uses the tool, and is assigned case 123456
User 2 at 10:00:0X uses the tool and is assigned case 123456
 
I assume that it takes just enough seconds, that if a person is using the tool, another person can end up with the same group of cases and not realize that its been taken away from the set by someone else.
 
This technically shouldnt be happening with the speed that the tool processes the cases, so I think it has something to do with record updating logic that salesforce might have in place outside of my control.  When a record is updated, and someone else requests an update to that same record at or around the exact same time, what happens?   Is there any prevention in place to ensure multiple edits arent overwriting each other? Do you queue up the api calls and they all take place in order, but definitely do take place no matter what?
 
I am going to try to see if I can run a quick query on the  LastModifiedDate field just before assigning the case, and if it doesnt match what I had originally when they started using the tool, have them process the next item in the list, but I wanted to post here to see if someone might be able to offer a better solution that would be more reliable.
zero1578zero1578

Is it possible to do Conditional Updates?

 

I will show two pieces of the code that I think are where I need to make corrections, and hopefully that will help in finding a solution.

When two people use the tool at the same time, after it builds the case array, it attempts to assign the case, but first it checks to be sure the current owner and the last modified time match up to what i originally queried when they first hit the tool page.  If it does, then i assign the case, if it doesnt then I start them over from the beginning.  So far people are still getting the same cases assigned to more than 1 person at a time.

Code:

function checkOwner(caseArray) 
{ 
var ownerId = caseArray[0].owner; 
var caseId = caseArray[0].id;
var modDate = caseArray[0].mDate; 

var qr = sforce.connection.query("SELECT LastModifiedDate,OwnerId FROM Case WHERE Id = '" + caseId + "'"); 
var records = qr.getArray("records"); 


var currentOwnerId = records[0].get("OwnerId"); 
var currentModDate = records[0].get("LastModifiedDate");

if(ownerId == currentOwnerId) 
{ 
 if(currentModDate == modDate)
 {
 return true; 
 }
 else
 {
 return false;
 }
} 
else 
{ 
return false; 
} 
} 


 

 

Code:

if(checkOwner(caseArray) == true) 
{ 
 assignCase(caseArray); 
} 
else 
{ 
 getTeam(); 
} 

} 

function assignCase(cases) 
{ 
//find out if the user already owns the case 
if(cases[0].owner != "{!$User.Id}") 
{
 //9/6/07 implement another check to prevent duplicate assignment
 if(checkOwner(cases) == true)
 {
  var selectedCase = new sforce.SObject("Case"); 
  selectedCase.Id = cases[0].id; 
  selectedCase.OwnerId = "{!$User.Id}"; 
  var result = sforce.connection.update([selectedCase])[0]; 
  //is the New Window check box checked— 
  if(document.getElementById("chkWindow").checked == 1) 
  { 
  window.open("https://na4.salesforce.com/" + cases[0].id + ""); 
  } 
  else 
  { 
  self.parent.location = "https://na4.salesforce.com/" + cases[0].id + ""; 
  } 
 }
 else
 {
 getTeam();
 }
} 

} 


 

Is there some sort of APi call I can make that will lock a record and prevent it from being reassigned again during the first persons use, or a conditional update method that will allow me to only change the owner on my selectedCase item to a new user if the current owner matches a specific criteria?