define(['N/ui/serverWidget','N/search','N/crypto','N/record'], function(serverWidget,search,crypto,record) {
function onRequest(context) {
try{
if (context.request.method === 'GET') {
var form = serverWidget.createForm({
title: 'Simple Form'
});
var field1= form.addField({
id: 'username',
type: serverWidget.FieldType.EMAIL,
label: 'USARNAME'
});
field1.layoutType = serverWidget.FieldLayoutType.NORMAL;
field1.isMandatory = true;
// field1.updateBreakType({
// breakType: serverWidget.FieldBreakType.STARTCOL
// });
var field2 = form.addField({
id: 'password',
type: serverWidget.FieldType.PASSWORD,
label: 'PASSWORD'
});
field2.isMandatory = true;
form.addSubmitButton({
label: 'Submit Button'
});
context.response.writePage(form);
} else {
var email = context.request.parameters.username;
var password = context.request.parameters.password;
log.debug("email",email);
log.debug("password up",password);
var customerSearchObj = search.create({
type: "customer",
filters:
[
["email","is",email]
],
columns:
[
search.createColumn({name: "pricingitem", label: "Pricing Item"}),
search.createColumn({name: "itempricinglevel", label: "Item Pricing Level"}),
search.createColumn({name: "itempricingunitprice", label: "Item Pricing Unit Price"})
]
});
var searchResultCount = customerSearchObj.runPaged().count;
log.debug("customerSearchObj result count",searchResultCount);
var mySearchResult = customerSearchObj.run();
var columns =mySearchResult.columns;
var arr =[];
log.debug("columns",columns);
customerSearchObj.run().each(function(result){
// .run().each has a limit of 4,000 results
var id =result.id;
log.debug("id",id);
log.debug("password",password);
var options = {
recordType: record.Type.CUSTOMER,
recordId: JSON.parse(id),
fieldId: 'custentity_pass_',
value: password
};
log.debug("options",options);
if (crypto.checkPasswordField(options)) {
log.debug('True');
var pricingitem = result.getValue(columns[0]);
var itempricinglevel= result.getValue(columns[1]);
var itempricingunitprice = result.getValue(columns[2])||0;
var obj ={
"pricingitem":pricingitem,
"itempricinglevel":itempricinglevel,
"itempricingunitprice":itempricingunitprice
};
arr.push(obj);
}
return ;
});
log.debug("return data",arr)
context.response.write(JSON.stringify(arr));
}
}catch(error){
log.debug("erroe",JSON.stringify(error));
}
}
return {
onRequest: onRequest
};
});