0

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
  };
});
  • You don't need to load the record "record.load(....)" use the loaded record from context `var estimate = scriptContext.newRecord;`, to show the value after http call use "estimate.setValue(...)". To save the latest value use record.submitFields({ type: record.Type.ESTIMATE, id: estimate.id, values: { "custbody_contract_status": "1" } }) ps: use try catch for http calls ;) – fullstack.studio Jun 22 '23 at 15:09

1 Answers1

0

The reason you have to load it twice is that the record that is displayed is the record from context.newRecord. You update it but don't do a redirect.

// include'N/redirect'
function beforeLoad(context){
   if (scriptContext.type === scriptContext.UserEventType.VIEW) {
        var estimate = scriptContext.newRecord;
        
        // check to see if the status is fresh; 
        // you could also set a timestamp when you update the status and
        // only get the status again when some time has passed. 
        // Depending on how many people are looking at the estimate that 
        // could make the UI noticeably snappier.

        // custparam_fresh_status set when redirecting to the newly updated record
        if(scriptContext.request && 'T' == scriptContext.request.parameters.custparam_fresh_status) return; 

        var insphireContractNumber = estimate .getValue({
          fieldId: "custbody_f5_insp_contract_num",
        });


        var currentStatus = getContractStatus(insphireContractNumber ); // your http call in this function. on error log the error and return null;
        if(currentStatus && currentStatus != estimate .getValue({fieldId:'custbody_contract_status'}){
             record.submitFields({
                type:estimate.type,
                id:estimate.id,
                values:{
                    custbody_contract_status:currentStatus 
                }
             });
             // now redirect to show the update
             redirect.toRecord({
                type:estimate.type,
                id:estimate.id,
                parameters:{
                   custparam_fresh_status:'T'
                }
             });
             return;
          }
        }
    }
bknights
  • 14,408
  • 2
  • 18
  • 31