1

I have used the below piece of code to select multiple cases from list view and update case owner. But it does not update the owner when I am selecting multiple cases while it works when I select only one case.

I had also added a line of code ( // sforce.connection.update([caseUpdate]); ) to test the beahviour and now multiple cases started updating. But updating each case one after another involves delay.

I wonder why Am I not able to Update the case in array. Please suggest.

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var records =     {!GETRECORDIDS($ObjectType.Case)};
var updateRecords = [];
var userId = "{!$User.Id}";

if (records[0] == null) {
    alert("Please Select atleast one Case")
} else {
    //Get more info on the cases that were selected and generate a query out of it
    var queryResults = sforce.connection.retrieve("CaseNumber, Id, OwnerId,Owner.Type", "Case", records);

    var casesAccepted = "";
    var casesRejected = "";
    //Iterate through the returned results
    for (var caseIndex=0;caseIndex<queryResults.length;caseIndex++) 
        {
        var caseObj = queryResults[caseIndex];
        //If it's already owned by a user, add it to the reject pile
        if (caseObj.Owner.Type=='User') 
                {
            casesRejected += caseObj.CaseNumber + " ";
        } 
                else {
            //Otherwise accept it
            var caseUpdate = new sforce.SObject("Case");
            caseUpdate.Id = caseObj.Id;
            caseUpdate.OwnerId = '{!User.Id}';
            caseUpdate.Status = 'In Progress';
            caseUpdate.Button_Clicked__C = 'True';
            updateRecords.push(caseUpdate);
                     *****//   sforce.connection.update([caseUpdate]);*****
            casesAccepted += caseObj.CaseNumber + " ";
             }


    } 

    //Give the user some feedback
    var alertText = "";

    if (updateRecords.length>0) {
        sforce.connection.update(updateRecords);
        alertText += "The Ownership For Selected Cases Has Changed To You:\n"+casesAccepted;
    }

    if (casesRejected!="") {
        alertText += "\n The Selected Case(s)Are Now Transferred To You:   :\n"+casesRejected;
    }

    alert(alertText);
    //Reload the list view to show what he now owns
    parent.window.location.reload();

}
0

@user2703132, I believe replacing your code "sforce.connection.update([caseUpdate]);" with "sforce.connection.update(updateRecords);" will solve your issue.

for (var caseIndex = 0; caseIndex < queryResults.length; caseIndex++) 
{
    var caseObj = queryResults[caseIndex];
    //If it's already owned by a user, add it to the reject pile
    if (caseObj.Owner.Type == 'User') {
        casesRejected += caseObj.CaseNumber + " ";
    } 
    else {
        //Otherwise accept it
        var caseUpdate = new sforce.SObject("Case");
        caseUpdate.Id = caseObj.Id;
        caseUpdate.OwnerId = '{!User.Id}';
        caseUpdate.Status = 'In Progress';
        caseUpdate.Button_Clicked__C = 'True';
        updateRecords.push(caseUpdate);

        casespted += caseObj.CaseNumber + " ";
    }
}
sforce.connection.update(updateRecords);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.