+ Start a Discussion
nlsnydernlsnyder 

How to submit form using ajax or jquery ajax in Salesforce???

I have done the workaround for the sort line items.   The lines are sorted via a custom web service so there is no need to pop up the /oppitm/lineitemsort.jsp page.    I create the form and hidden fields dynamically and hit submit.   This pops up the jsp page.   I want to skip that and auto submit via ajax or something.    It works when I have form.submit() and the form pops up and I click the Save button.   Is there a way to replace the form.submit() and having the user click the save button.

 

I tried to use jquery ajax and ajaxSubmit, but I get an error with the error message so not sure what is wrong.   Can this be done and does anyone know how to do that part.

 

--------------------------------------------------------------------------------------------

 

So I had this originally:

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

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

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

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

form.setAttribute("id", "sortForm");

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

 

I have been commenting out the form.submit and trying to use jquery / ajax to automate:

 

I include the jquery js scripts:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
    <script src="http://malsup.github.com/jquery.form.js"></script>

 

I tried using $.ajax where formData is the form data serialized but get the error alert with error=error. 

 

         $.ajax({
             type: "post",
             url:  "https://na14.salesforce.com/oppitm/lineitemsort.jsp",
             data: formData,
             success: function() {
                 alert('>> ajax submit successful <<');
             },
             error: function(req, status, error) {
                 alert('>> ajax submit failed: status='+ status + ' error=' + error + ' <<');
             }
         });
        

Then tried and get the error alert with error=error again.
         
         $('#sortForm').ajaxSubmit({
             success: function() {
                 alert('>> ajax submit successful <<');
             },
             error: function(req, status, error) {
                 alert('>> ajax submit failed: req='+req+' status='+ status + ' error=' + error + ' <<');
             }
         });

 

Aakanksha Singh 11Aakanksha Singh 11
Hello,

If you want to use class WSDL, you can try the following link:
https://wedgecommerce.com/how-to-use-apex-in-ajax-salesforce/

Thank You
Aakanksha Singh