Sample multi step RESTlet serverWidget.assistant script:
/**
*@NApiVersion 2.x
*@NScriptType Suitelet
*/
define(['N/ui/serverWidget', 'N/record'], function (serverWidget, record) {
function onRequest(context) {
log.debug('START')
var assist = serverWidget.createAssistant({
title: 'Sample Assistant'
});
var step1 = assist.addStep({
id: 'step_1',
label: 'info_1'
})
var step2 = assist.addStep({
id: 'step_2',
label: 'info_2'
})
var step3 = assist.addStep({
id: 'step_3',
label: 'info_3'
})
var step4 = assist.addStep({
id: 'step_4',
label: 'info4'
})
if (context.request.method == 'GET') {
if (!assist.isFinished()) {
log.debug('dwadawd'); log.debug(step1)
if (assist.currentStep == null) {
log.debug('set step1')
assist.currentStep = assist.getStep({ id: 'step_1' });
}
// assist.currentStep = assist.getStep({ id: 'step_1' });
step = assist.currentStep.id;
log.debug(step)
// STEP1 INIT
if (step == 'step_1') {
log.debug(step)
assist.addField({
id: 'comp_name',
type: serverWidget.FieldType.TEXT,
label: 'Company Name'
})
assist.addField({
id: 'subsidiary',
type: serverWidget.FieldType.SELECT,
label: 'subsidiary',
source: 'subsidiary'
})
assist.addField({
id: 'phone',
type: serverWidget.FieldType.PHONE,
label: 'phone'
})
assist.addField({
id: 'email',
type: serverWidget.FieldType.EMAIL,
label: 'email'
})
context.response.writePage(assist);
// STEP2 INIT
} else if (step == 'step_2') {
log.debug(step);
var countryList = assist.addField({
id: 'country',
type: serverWidget.FieldType.SELECT,
label: 'Country'
})
countryList.addSelectOption({
value: 'US',
text: 'United States'
})
countryList.addSelectOption({
value: 'CZ',
text: 'Czech Republic'
})
assist.addField({
id: 'address',
type: serverWidget.FieldType.TEXT,
label: 'address'
})
assist.addField({
id: 'zip',
type: serverWidget.FieldType.INTEGER,
label: 'zip'
})
context.response.writePage(assist);
// STEP3 INIT
} else if (step == 'step_3') {
log.debug('redirect' + context.request.body);
log.debug(step);
assist.addField({
id: 'contact_name',
type: serverWidget.FieldType.TEXT,
label: 'Company Name'
})
assist.addField({
id: 'contact_phone',
type: serverWidget.FieldType.PHONE,
label: 'contact_phone'
})
assist.addField({
id: 'contact_email',
type: serverWidget.FieldType.EMAIL,
label: 'contact_email'
})
context.response.writePage(assist);
// STEP4 INIT
} else if (step == 'step_4') {
log.debug(step);
assist.addField({
id: 'contact_namdawdaawdwade',
type: serverWidget.FieldType.TEXT,
label: 'Company dwaName'
});
context.response.writePage(assist);
}
}
}
else if (context.request.method == 'POST') {
if ((assist.getLastAction() == serverWidget.AssistantSubmitAction.NEXT) || (assist.getLastAction() == serverWidget.AssistantSubmitAction.BACK)) {
assist.currentStep = assist.getNextStep();
assist.sendRedirect({
response: context.response
})
}else if(assist.getLastAction() == serverWidget.AssistantSubmitAction.FINISH){
// step_1
var step_1 = assist.getStep({id: 'step_1'})
var comp_name = step_1.getValue({id:'comp_name'})
var subsidiary = step_1.getValue({id:'subsidiary'})
var phone = step_1.getValue({id:'phone'})
var email = step_1.getValue({id:'email'})
// Step_2
var step_2 = assist.getStep({id: 'step_2'})
var country = step_2.getValue({id:'country'})
var address = step_2.getValue({id:'address'})
var zip = step_2.getValue({id:'zip'})
// Step_3
var step_3 = assist.getStep({id: 'step_3'})
var contact_name = step_3.getValue({id:'contact_name'})
var contact_phone = step_3.getValue({id:'contact_phone'})
var contact_email = step_3.getValue({id:'contact_email'})
// Step_4
var step_4 = assist.getStep({id: 'step_1'})
// Create Customer
var custObj = record.create({
type: record.Type.CUSTOMER,
isDynamic:true
})
custObj.setValue({fieldId:'companyname',value:comp_name})
custObj.setValue({fieldId:'subsidiary',value:subsidiary})
custObj.setValue({fieldId:'phone',value:phone})
custObj.setValue({fieldId:'email',value:email})
// Address subrecord
custObj.selectNewLine({
sublistId:'addressbook'
})
custObj.setCurrentSublistValue({
sublistId:'addressbook',
fieldId:'label',
value:'Primary'
})
var addRec = custObj.getCurrentSublistSubrecord({
sublistId:'addressbook',
fieldId:'addressbookaddress'
})
addRec.setValue({fieldId:'country',value:country})
addRec.setValue({fieldId:'addr1',value:address})
addRec.setValue({fieldId:'zip',value:zip})
custObj.commitLine({sublistId:'addressbook'})
custObj.selectNewLine({
sublistId:'contact'
})
custObj.setCurrentSublistValue({sublistId:'contact',fieldId:'firstname',value:contact_name})
custObj.setCurrentSublistValue({sublistId:'contact',fieldId:'phone',value:contact_phone})
custObj.setCurrentSublistValue({sublistId:'contact',fieldId:'email',value:contact_email})
custObj.setCurrentSublistValue({sublistId:'contact',fieldId:'subsidiary',value:subsidiary})
custObj.commitLine({sublistId:'contact'});
var custId = custObj.save();
assist.finishedHtml = 'FINISHED'
context.response.writePage(assist);
assist.isFinished = true;
}
}
}
return {
onRequest: onRequest
}
});
*@NApiVersion 2.x
*@NScriptType Suitelet
*/
define(['N/ui/serverWidget', 'N/record'], function (serverWidget, record) {
function onRequest(context) {
log.debug('START')
var assist = serverWidget.createAssistant({
title: 'Sample Assistant'
});
var step1 = assist.addStep({
id: 'step_1',
label: 'info_1'
})
var step2 = assist.addStep({
id: 'step_2',
label: 'info_2'
})
var step3 = assist.addStep({
id: 'step_3',
label: 'info_3'
})
var step4 = assist.addStep({
id: 'step_4',
label: 'info4'
})
if (context.request.method == 'GET') {
if (!assist.isFinished()) {
log.debug('dwadawd'); log.debug(step1)
if (assist.currentStep == null) {
log.debug('set step1')
assist.currentStep = assist.getStep({ id: 'step_1' });
}
// assist.currentStep = assist.getStep({ id: 'step_1' });
step = assist.currentStep.id;
log.debug(step)
// STEP1 INIT
if (step == 'step_1') {
log.debug(step)
assist.addField({
id: 'comp_name',
type: serverWidget.FieldType.TEXT,
label: 'Company Name'
})
assist.addField({
id: 'subsidiary',
type: serverWidget.FieldType.SELECT,
label: 'subsidiary',
source: 'subsidiary'
})
assist.addField({
id: 'phone',
type: serverWidget.FieldType.PHONE,
label: 'phone'
})
assist.addField({
id: 'email',
type: serverWidget.FieldType.EMAIL,
label: 'email'
})
context.response.writePage(assist);
// STEP2 INIT
} else if (step == 'step_2') {
log.debug(step);
var countryList = assist.addField({
id: 'country',
type: serverWidget.FieldType.SELECT,
label: 'Country'
})
countryList.addSelectOption({
value: 'US',
text: 'United States'
})
countryList.addSelectOption({
value: 'CZ',
text: 'Czech Republic'
})
assist.addField({
id: 'address',
type: serverWidget.FieldType.TEXT,
label: 'address'
})
assist.addField({
id: 'zip',
type: serverWidget.FieldType.INTEGER,
label: 'zip'
})
context.response.writePage(assist);
// STEP3 INIT
} else if (step == 'step_3') {
log.debug('redirect' + context.request.body);
log.debug(step);
assist.addField({
id: 'contact_name',
type: serverWidget.FieldType.TEXT,
label: 'Company Name'
})
assist.addField({
id: 'contact_phone',
type: serverWidget.FieldType.PHONE,
label: 'contact_phone'
})
assist.addField({
id: 'contact_email',
type: serverWidget.FieldType.EMAIL,
label: 'contact_email'
})
context.response.writePage(assist);
// STEP4 INIT
} else if (step == 'step_4') {
log.debug(step);
assist.addField({
id: 'contact_namdawdaawdwade',
type: serverWidget.FieldType.TEXT,
label: 'Company dwaName'
});
context.response.writePage(assist);
}
}
}
else if (context.request.method == 'POST') {
if ((assist.getLastAction() == serverWidget.AssistantSubmitAction.NEXT) || (assist.getLastAction() == serverWidget.AssistantSubmitAction.BACK)) {
assist.currentStep = assist.getNextStep();
assist.sendRedirect({
response: context.response
})
}else if(assist.getLastAction() == serverWidget.AssistantSubmitAction.FINISH){
// step_1
var step_1 = assist.getStep({id: 'step_1'})
var comp_name = step_1.getValue({id:'comp_name'})
var subsidiary = step_1.getValue({id:'subsidiary'})
var phone = step_1.getValue({id:'phone'})
var email = step_1.getValue({id:'email'})
// Step_2
var step_2 = assist.getStep({id: 'step_2'})
var country = step_2.getValue({id:'country'})
var address = step_2.getValue({id:'address'})
var zip = step_2.getValue({id:'zip'})
// Step_3
var step_3 = assist.getStep({id: 'step_3'})
var contact_name = step_3.getValue({id:'contact_name'})
var contact_phone = step_3.getValue({id:'contact_phone'})
var contact_email = step_3.getValue({id:'contact_email'})
// Step_4
var step_4 = assist.getStep({id: 'step_1'})
// Create Customer
var custObj = record.create({
type: record.Type.CUSTOMER,
isDynamic:true
})
custObj.setValue({fieldId:'companyname',value:comp_name})
custObj.setValue({fieldId:'subsidiary',value:subsidiary})
custObj.setValue({fieldId:'phone',value:phone})
custObj.setValue({fieldId:'email',value:email})
// Address subrecord
custObj.selectNewLine({
sublistId:'addressbook'
})
custObj.setCurrentSublistValue({
sublistId:'addressbook',
fieldId:'label',
value:'Primary'
})
var addRec = custObj.getCurrentSublistSubrecord({
sublistId:'addressbook',
fieldId:'addressbookaddress'
})
addRec.setValue({fieldId:'country',value:country})
addRec.setValue({fieldId:'addr1',value:address})
addRec.setValue({fieldId:'zip',value:zip})
custObj.commitLine({sublistId:'addressbook'})
custObj.selectNewLine({
sublistId:'contact'
})
custObj.setCurrentSublistValue({sublistId:'contact',fieldId:'firstname',value:contact_name})
custObj.setCurrentSublistValue({sublistId:'contact',fieldId:'phone',value:contact_phone})
custObj.setCurrentSublistValue({sublistId:'contact',fieldId:'email',value:contact_email})
custObj.setCurrentSublistValue({sublistId:'contact',fieldId:'subsidiary',value:subsidiary})
custObj.commitLine({sublistId:'contact'});
var custId = custObj.save();
assist.finishedHtml = 'FINISHED'
context.response.writePage(assist);
assist.isFinished = true;
}
}
}
return {
onRequest: onRequest
}
});
No comments:
Post a Comment