Store form fields’ data in JavaScript Object

Hello, I have a form in Suitelet which contains various text fields, select, radio buttons etc., Once the data is entered in the available fields, on submitting the form, all the data entered must be stored in a JavaScript object (for eg: in “Age” field, if the value entered in “22”, in “Gender” field, value entered is “male”. The Javascript object should be {age: 22, gender: male}). But in the suitelet form, the fields are dynamically generated, so I cannot specifically use request.parameters.age pr request.parameters.gender. How to create and store data in Javascript object in the above said format for a dynamically generated suitelet form and send it to a particular record.

Thanks

Rookie Asked on September 12, 2019 in SuiteScript.

You probably need to give more details about how your form is generated and its relation to the keys you need to set on your javascript object.

on September 12, 2019.
Add Comment
3 Answer(s)

What if you add a prefix to all of the dynamic fields? So use ‘myprefix_age’ and ‘myprefix_gender’ instead. Then in the Suitelet, retrieve them with something like this:

var obj = {};
for (var key in context.request.parameters) {
    if (key.indexOf('myprefix_') === 0) {
          obj[key.replace('myprefix_', '')] = context.request.parameters[key];
    }
}
Beginner Answered on September 12, 2019.
Add Comment

I have done this in the past.

In the Suitelet while creating the form add

a code behind page… This will be a NetSuite client script behind your Suitelet.

form.clientScriptModulePath = './clientscriptname.js';

 

In the Client scripts Save Event

you and grad the values entered into your fields on the suitelet like this

 

var rec = scriptContext.currentRecord;
var newphone = rec.getValue('phone');

Or better create an object

 

function saveRecord(scriptContext) {
var rec = scriptContext.currentRecord;
var newcontact = {}
newcontact.name = rec.getValue('custpage_name');
newcontact.age = rec.getValue('custpage_age');
newcontact.age = rec.getValue('custpage_gender');
newcontact.phone = rec.getValue('custpage_phone');
 ...do stuff with the object...
return true;
}

 

Then do whatever you like with the JS Object newcontact,  Create or update a record,  Pass the object as paramenter to somewhere else etc…

 

Hope this helps

Sean

 

Rookie Answered on September 12, 2019.
Add Comment

Also note that even dynamically generate fields have an id.  You may need to look at the Suitelet to see what it is, but it should have one.  For example,

var form = nlapiCreateForm("Serial Number Search");
form.addField('custpage_snsearch_insn', 'text', 'Serial Number');
form.addSubmitButton('Search');
response.writePage(form);
 

You’ll notice that the field has an id ‘custpage_snsearch_insn‘.  Even fields that are added conditionally will have this, and you can use that with request.parameters to get the data.

If the text fields, select, and radio buttons aren’t created using the form API (for example, if your using something like Vue for data entry), you’ll need to attach a client script which finds the fields (using document.getElementById()), converts them to a JS object, and saves them to a hidden field created using the form method shown above.  You can attach this code to the saveRecord() event and I think that should do it.

Also, it’s much easier to help if you post some code :).  If you can, that’d be fantastic.

Rookie Answered on September 12, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Become a Sponsor   Become a Sponsor