2

I added a field in the Sales Order named Membership that will source from the customer record. Then when that field is set to the membership level (ex. elite member) it automatically set the discount item field to a specific discount item.... I encountered a notif saying SuiteScript 2.x entry point scripts must implement one script type function how do I fix this?

/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(["N/currentRecord", "N/runtime"], function(currentRecord,runtime) {

    function pageInit(context) {

     const record = currentRecord.get()  // Get value of Membership Field   
     const user = runtime.getCurrentUser().name
    }
    
    var membership = currentRecord.getField({
        fieldId : "custentity1",
    })

    if(membership == "Elite"){
        //Setting up discount
        record.setValue({
            fieldId: "discountitem", //fieldId of the Discount Item 
            value: 137 // Internal ID of the Discount Item
        })}

        else {
            record.setValue({
                fieldId: "discount item",
                value: 0
            });
        }

        return {
            pageInit : pageInit
        }
});    
Avi
  • 2,014
  • 1
  • 9
  • 21
apple
  • 21
  • 2

4 Answers4

0

your code needs to be in an entry point for it to have an affect on the Sales Order record, sounds like you want to use the fieldChanged entry point, look at that in the help docs.

Brian Duffy
  • 112
  • 8
0

Your logic must be present within the function i.e. the Entrypoint functions. In your code, it is outside the function (PageInit function). Hence the error.

Try this -

/** 
  *@NApiVersion 2.0
  *@NScriptType ClientScript
*/ 

define(["N/currentRecord", "N/runtime"], function(currentRecord,runtime) {

    function pageInit(context) {

        const record = currentRecord.get();  // Get value of Membership Field   
        const user = runtime.getCurrentUser().name;

        var membership = currentRecord.getField({
            fieldId : "custentity1"
        });
           
        if(membership == "Elite"){
            //Setting up discount
            record.setValue({
                fieldId: "discountitem", //fieldId of the Discount Item 
                value: 137 // Internal ID of the Discount Item
            });
        } else {
            record.setValue({
                fieldId: "discount item",
                value: 0
            });
        }
    }
       
    return {
        pageInit : pageInit
    }
});

Let me know in case of issues.

Sayeesh
  • 208
  • 1
  • 3
  • 13
0

I encounter this error in my initial days. Change the @NApiVersion to 2.1. Whenever we use latest ES features (let, const, forEach,...) assign the api version JSDocTag to 2.1. Let me know if the issue persists.

Ajith V
  • 53
  • 5
0

Because your using 2.x your global settings are going to determine if this is run as 2.0 or 2.1. I'm guessing you have it set at 2.1 because you're context is having an issue.

If you try to run 2.1 code on the client then you might run into issues. One of those issues is that the context will not be populated. Perhaps this is why you are using currentRecord.get() instead of context.currentRecord ?

Note: Any code that is outside a function (like pageInit) will be run when the module's callback is run.

   if(membership == "Elite"){
    //Setting up discount
    record.setValue({
        fieldId: "discountitem", //fieldId of the Discount Item 
        value: 137 // Internal ID of the Discount Item
    })}

    else {
        record.setValue({
            fieldId: "discount item",
            value: 0
            });
    }

so this code is running before pageInit, so the suggestion by Sayeesh is a good one.

You are mixing some EcmaScipt versions. I would get a good linter like eslint and make sure you are writing code for the target you want.

you got a field called discount item and one called discountitem? I'll assume that is a typo.

To play devil's advocate i'm going to say that you can use 2.1 if you really want to here. In 2.1 you're code could be as simple as : (not checked)

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 */

define(['N/record','N/currentRecord'],(record,currentRecord)=>{
  return {
    pageInit({ currentRecord : { id, type } }){
        
        const 
          so = record.load({ id, type }),
          membership = so.getValue({ fieldId : 'custentity1' }),
          fieldId = 'discountitem',
          value = membership === 'Elite' ? 137 : 0;
       


        so.setValue({ fieldId, value }); 
    }
  }
}); 
gillyspy
  • 1,578
  • 8
  • 14