'How to set a field on an invoice after sourcing from a customer record using Suitescript

I have the following scenario:

There is a custom field on the invoice record called 'contract description' (id:'custbody_contract_description_invoice')

The value for this should equal the value of the 'contract description' field which sits on the project record

Every time the end user selects a project on the invoice (at the header level), this should set the value of the contract description field.

The following script achieves this:


        /**
     *@NApiVersion 2.x
     *@NScriptType ClientScript
     */
    define(["N/search"], function (search) {
      // function fieldChanged(context) {
    
      // }
    
      function postSourcing(context) {
        var customerRecord = context.currentRecord;
    
        if (context.fieldId == "job") {
          var projectId = customerRecord.getValue({ fieldId: "job" });
          log.debug("projectid", projectId);
          var jobSearchObj = search.create({
            type: "job",
            filters: [["internalidnumber", "equalto", projectId]],
            columns: [
              search.createColumn({
                name: "custentity_contract_desc_project",
                label: "Contract Description",
              }),
              search.createColumn({
                name: "entityid",
                join: "customer",
                label: "Name",
              }),
            ],
          });
    
          var firstResult = jobSearchObj.run().getRange({
            start: 0,
            end: 1,
          })[0];
    
          log.debug("firstresult", firstResult);
          var contractDescription = firstResult.getValue(firstResult.columns[0]);
          log.debug("contract description", contractDescription);
    
          var invoiceContract = customerRecord.setValue({
            fieldId: "custbody_contract_desc_invoice",
            value: contractDescription,
          });
          log.debug("customer contract", invoiceContract);
        }
      }
    
      return {
        postSourcing: postSourcing,
      };
    });

However, this is creating issues with the record loading time when the customer field is changed.

Is there a better way to achieve the same result? (different script type for example)

The record set up is (in this example): Customer: ABC Marketing Sub Customer: ABC Marketing Subcustomer Project (which is a project of the subcustomer and not the parent customer): ABC Marketing Subcustomer The main customer Customer The subcustomer: subcustomer

The project project



Solution 1:[1]

If you don't need to reference the contents of the field during the time the user is editing the record, you could set the value on either beforeSubmit or on afterSubmit in a User Event Script. That would run on the server side, so would be faster than running on the client side like a Client Script.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pierre Plourde