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
ckellieckellie 

Help Correcting the following JS Button

A product sort button has been working on my org for the last 4 years without problems until now. The button is resorting the products in the standard product sort page, but it is returning the product sort page without saving the new product order.  How can append the button and code to resort the products and save the products in the new sort order? Below is the javascript button and supporting class.
 
Button:

{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/36.0/apex.js")}
{!REQUIRESCRIPT("https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js")}

var oppID = '{!Quote.Id}';

//call the apex web service to get the OLIs in the desired sort order for this opp
var result = sforce.apex.execute("customQuoteSort","MRsort",{oppID: oppID});   

//need to post a form to /oppitm/lineitemsort.jsp because this is how SFDC
//does it but there is not direct API to do the sorting (thus the awkward workaround)
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "/oppitm/lineitemsort.jsp?id="+oppID+"&retURL=%2F"+oppID);

//set the id of the request to the opportunity ID
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", 'hidden');
hiddenField.setAttribute("name", 'id');
hiddenField.setAttribute("value", '{!Quote.Id}');   
form.appendChild(hiddenField);

//the name of the sorted OLI list that the JSP is expecting is "duel0"
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", 'hidden');
hiddenField.setAttribute("name", 'duel0');
hiddenField.setAttribute("value", String(result));
form.appendChild(hiddenField);

var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", 'hidden');
hiddenField.setAttribute("name", 'retURL');
hiddenField.setAttribute("value", '/oppid');
form.appendChild(hiddenField);

//set to save
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", 'hidden');
hiddenField.setAttribute("name", 'save');
hiddenField.setAttribute("value", ' Save ');
form.appendChild(hiddenField);

//need to do this so it works in IE
document.body.appendChild(form);

//submit!!
form.submit();
 
Supporting Class:

global class customQuoteSort {
    webservice static String MRsort(Id QID)
    {        
    //pull back the qlis in a specific sort order        
    List<QuoteLineItem> qlis = [Select qli.Id, qli.Quote_Item_Number__c       
           From QuoteLineItem qli        
                   Where qli.QuoteId = :QId
                   ORDER BY qli.Quote_Item_Number__c];
                //build the comma separated 15 character qli Id string to send back
        String sortedIds = '';
                            for(QuoteLineItem qli : qlis)
        {
            sortedIds += String.valueOf(qli.Id).substring(0,15) + ',';
              }
                //remove the last comma
            sortedIds = sortedIds.substring(0,sortedIds.length() - 1);
        return sortedIds;
    }
}

The above automation was based on this link: https://developer.salesforce.com/forums/?id=906F000000094fvIAA

While I have not seen anything doing away with Javascript buttons in classic. Ideally it would be great to get this button back up and running and then work on a longer-term solution if I need to.

Any help will be appreciated.