I'm running script that will load an API (https://ag-dev-dock.accessgroup.net.au/aps/Public/ProductionContractStatus?) with contract number (contractNo) as parameter. That parameter will be coming from a field (custbody_f5_insp_contract_num). Also, the API is holding contractStatus data that will be set to custbody_contract_status field in the Estimate record. However, for some reason, the Estimate record has to be loaded twice before the custbody_contract_status field be populated.
Requirement:
- custbody_contract_status field should be populated with contractStatus data right after the Estimate record is loaded.
Here's the script
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(["N/https", "N/record", "N/log", "N/url"], function (
https,
record,
log,
url
) {
function beforeLoad(scriptContext) {
if (scriptContext.type === scriptContext.UserEventType.VIEW) {
var estimate = scriptContext.newRecord;
var estimateRecord = record.load({
type: "estimate",
id: estimate.id,
});
var insphireContractNumber = estimateRecord.getValue({
fieldId: "custbody_f5_insp_contract_num",
});
// log.debug("estimateRecord", estimateRecord);
var insphireContractStatusUrl = url.format({
domain:
"https://ag-dev-dock.accessgroup.net.au/aps/Public/ProductionContractStatus?",
params: {
contractNo: insphireContractNumber,
},
});
var insphireContractStatusResponse = https.get({
method: https.Method.GET,
url: insphireContractStatusUrl,
});
log.debug(
"insphireContractStatusResponse",
insphireContractStatusResponse
);
var insphireContractStatusParsed = JSON.parse(
insphireContractStatusResponse.body
);
if (insphireContractStatusParsed.hasOwnProperty("resultFlag")) {
log.debug(
"Insphire Contract Status Error",
insphireContractStatusParsed.message
);
} else {
var contractStatusValue = insphireContractStatusParsed.contractStatus;
log.debug("contractStatusValue1", contractStatusValue);
switch (contractStatusValue) {
case "0":
contractStatusValue = 1;
break;
case "1":
contractStatusValue = 2;
break;
case "2":
contractStatusValue = 3;
break;
case "4":
contractStatusValue = 4;
break;
default:
contractStatusValue = 5;
break;
}
log.debug("contractStatusValue2", contractStatusValue);
log.debug("typeof", typeof contractStatusValue);
estimateRecord.setValue({
fieldId: "custbody_contract_status",
value: contractStatusValue
});
estimateRecord.save();
}
}
}
return {
beforeLoad: beforeLoad
};
});