0

Salesforce uses the lineitemsort.jsp to reorder opportunity and quote line items. I am trying to create a visualforce page with Javascript fucntion (and apex controller) that includes the ability to reorder quotelineitems.

I have already researched this and found what I hope is a good starting point but am running into some unexpected trouble.

I do not have too much experience with Javascript and I am running into some errors. For some reason, when calling the below function via a command button, it says the function is unknown. Error code from chrome console:

Uncaught ReferenceError: sortList is not defined

I am stumped as to why it's claiming the function is not defined. Does anybody have an idea why?

After I can get the function to actually trigger, I can start working on making sure it re-sorts the line items correctly.

Javascript Function:

<script src ="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
function sortList(){
    var QuoteId = '{!qid}';

    //call the apex web service to get the OLIs in the desired sort order for this quote
    var result = '{!lineItemIDs}';   

    // get confirmation token and set webform field
    // get confirmation token and set webform field
    $.ajax({
        url:"./oppitm/lineitemsort.jsp?id={!qid}",
        contentType:"text/html",
        xhrFields:{withCredentials:true}
    }).done (
        function(data){
            key_start='id="_CONFIRMATIONTOKEN" value="';
            key_end='"';
            pos_start=data.indexOf(key_start);
            pos_end=data.indexOf(key_end,pos_start+key_start.length);
            confirmationtoken=data.substring(pos_start+key_start.length,pos_end);



            //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");

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



            //set the id of the request to the Quote ID
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", 'hidden');
            hiddenField.setAttribute("name", 'id');
            hiddenField.setAttribute("value", '{!qid}');   
            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", '?id={!qid}');
            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);
            form.submit();
            console.log(form);
        }
    );

};
</script>

VF Command Button Call

        <apex:commandButton title="Test" value="Test" onclick="sortList()" oncomplete="window.location.reload();"/>
2
  • As near as I can tell, it's probably var result = '{!lineItemIDs}'; ... Take a look at your page's source code (View > Page Source) or Developer Console (F12) and check for errors.
    – sfdcfox
    Commented Apr 30, 2019 at 18:54
  • @sfdcfox I commented out that line and hardcoded a string of comma-separated quotelineitems where it was being used. Unfortunately, it is still giving me the undefined error. Is there a reason you believe that line is to blame? Thank you!
    – Rory
    Commented Apr 30, 2019 at 19:30

1 Answer 1

0

Found the issue. The script src was the culprit.

Changed this:

<script src ="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">MY CODE HERE</script>

To this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script> MY CODE HERE </script>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .