I am trying to load field values onto a purchase o...
# suitescript
k
I am trying to load field values onto a purchase order record when the page is loaded. I was able to get the field values to populate on page Init but I changed it to beforeLoad to get it show in view mode. I started to receive the error: Cannot read property 'setValue' of undefined [at Object.beforeLoad (/SuiteScripts/beforeLoadPO.js345)] Are there any recommendations on how to get the field values to populate with beforeLoad?
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/log', "N/format"], (record, log, format) => {
   function beforeLoad(context){
    
    log.debug({
        title: "test",
        details: "Script Triggered This is the date" + d + "/n" + 
        "This is the formatted date: " + formattedDateString + 
        "This is the sale order date" + salesOrder 

    })
    context.currentRecord.setValue({
        fieldId: "duedate",
        value: new Date(),
        ignoreFieldChange: true
        
    })

    context.currentRecord.setValue({
        fieldId: "custbody_lf_expected_delivery_date",
        value: new Date(),
        ignoreFieldChange: true
    })

    context.currentRecord.setValue({
        fieldId: "custbody_lf_deadline_to_release",
        value: new Date(),
        ignoreFieldChange: true
    })

    // custbody_lf_exp_ship_date
    // context.

   
   }return{
    beforeLoad: beforeLoad
   }
});
a
message has been deleted
k
How can I set a value using beforeLoad so the field shows a value when the page is loaded
@Anthony OConnor
a
... you can't
I mean you'd want context.newRecord instead of context.currentRecord but it will just ignore any setValues
if you're loading this record in view mode then it already exists, right?
so I'd set these values in the beforeSubmit and they'll be populated from when it was previously saved
if you want/need to mass update existing records then use a massupdate script or MR script for that
so if my assumptions are right, its a 2 step fix. 1. user event beforeSubmit and/or clientScript pageInit to set the fields on any newly created records going forward 2. some 1 time process to fix all historical records, this can be a massupdate, csv import, or map reduce script