Abdul bazith
Abdul bazith

Reputation: 11

how to set values using aftersubmit function in create mode

I need to set the values while creating the inventory item form, I deployed the script in the corresponding form, and it is working as I tried to send an email. For this, it is working, But I don't know when I tried but nothing happened.

 <!--   /**
 *@NApiVersion 2.0
*@NScriptType UserEventScript
*/
define(["N/url", "N/record", "N/runtime"], function (url, record, runtime) {
    function afterSubmit(context){
        var recordobj = context.newRecord;
        var Type=context.type;
        if(Type== context.UserEventType.CREATE)
        recordobj.setValue({
            fieldId:'custitem27',
            Value:'something',
        }); 
    }
    return{
        afterSubmit:afterSubmit
        } 
}); 
 -->

Upvotes: 0

Views: 519

Answers (2)

bknights
bknights

Reputation: 15462

Unless you are depending on something being set (e.g. the record internalid) then the snippet in your question would be better as part of the beforeSubmit event.

You can just use your code as entered (except for the beforeSubmit part) without having to reload the record.

Loading and submitting a record in an after submit increases the risk that some other script is going to run into or trigger a "This record has been changed" error.

Also object field names are case sensitive so unless your code has a transcription typo:

recordobj.setValue({
            fieldId:'custitem27',
            Value:'something',
        }); 

should be:

recordobj.setValue({
    fieldId:'custitem27',
    value:'something' //value property is lowercase
}); 

Upvotes: 1

Camilo Castellano
Camilo Castellano

Reputation: 76

You have to load the record, set the field value and save or just submitfield. Example:

 <!--   /**
 *@NApiVersion 2.0
*@NScriptType UserEventScript
*/
define(["N/url", "N/record", "N/runtime"], function (url, record, runtime) {
    function afterSubmit(context){
        var recordobj = context.newRecord;
        var Type=context.type;
        if(Type== context.UserEventType.CREATE)        
          record.submitFields({
              type: recordobj.type,
              id: recordobj.id,
              values: {
                  custitem27: 'something'
              },
              options: {
                  enableSourcing: false,
                  ignoreMandatoryFields : true
              }
          });
    }
    return{
        afterSubmit:afterSubmit
        } 
}); 
 -->

Upvotes: 2

Related Questions