+ Start a Discussion
ckellieckellie 

Javascript Button not working properly

I have a javascript button called "sort products" related to an opportunity based on a custom field. We have related list hoverlinks enacted. If I click "sort products" at the top of the page in the related list, the page is refreshed within the  hover window. Conversely if I scroll to the bottom of the page to where products related list and click "sort products" button, nothing happens.

 

Why wiould the button work as intended the if it is clicked in the related hoverlist, but not at the bottom of the page. Why this problem? How can I solve it?

 

Thank you

iBr0theriBr0ther

where is the js code?

ckellieckellie

The js code is in an opportunity line item object and is as follows:

 

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js")}

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

//call the apex web service to get the OLIs in the desired sort order for this opp
var result = sforce.apex.execute("customSort","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");

//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", '{!Opportunity.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", '/{!Opportunity.Id}');
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();

 It is also using the following class in the js:

global class customSort {
    webservice static String MRsort(Id oppID)
    {        
    //pull back the OLIs in a specific sort order        
    List<OpportunityLineItem> olis = [Select oli.Id, oli.Quote_Item_Number__c         
           From OpportunityLineItem oli        
                   Where oli.OpportunityId = :oppId
                   ORDER BY oli.Quote_Item_Number__c];
                //build the comma separated 15 character OLI Id string to send back
        String sortedIds = '';
                            for(OpportunityLineItem oli : olis)
        {
            sortedIds += String.valueOf(oli.Id).substring(0,15) + ',';
              }
                //remove the last comma
            sortedIds = sortedIds.substring(0,sortedIds.length() - 1);
        return sortedIds;
    }
}

 

jshimkoskijshimkoski

Just wondering... could your problem be caused by creating a duplicate form everytime the button is clicked?

 

If you click the button at the bottom of the page before you click the button at the top of the page, does it function correctly but does it make the top button not work?

 

If this is the case, you may need to destroy the generated form if it already exists then recreate it on the button click action.

ckellieckellie

The button is on a related list on a standard ui page. I don't think this is the problem. This is working in one sandbox, but not in a second sandbox. Why would this be?