VicDid VicDid - 1 year ago 106
Javascript Question

Separating the POST requests of a Suitelet

My goal is to attach a custom Submit button to a Suitelet that sends a post request to an external server. The problem I am having is that when rendering the page the function

within my custom button in
form.addButton('submit','Submit', send(form));
is running when the page is loaded. Because of this, no data is being sent to the
function and therefore not being handled properly.

From what I understand, this problem has to do with the original POST request rendering the Suitelet and then interfering with the POST request I want to send.
What I need to do is separate or differentiate the POST request that renders the form from the one that sends the form. The ideal would be to have this done in a User Event Script but forms can only be used in Suitelets in SS1.0...

Is there any way this is possible? Should I use SuiteScript 2.0 to achieve this?
Below is how my code looks:

function main(request, response){
var form = nlapiCreateForm('Submission Form');
form.addField('one', 'text', 'Field 1');
form.addButton('submit','Submit', send(form));

function send(form){ process form data somewhere... }

Answer Source

When you are doing send(form) on the Suitelet you are actually running the function. The form.addButton method 3rd parameter accepts a string, so you want to do something like

form.addButton('submit','Submit', "alert('Sending form')")

You can add a whole stringified function if you want to build your own POST, although I think it would be easier to just include a form.addSubmitButton(label) and that sends the form data to the same Suitelet and from there you can just send that data by POST using nlapiRequestURL

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download