Hi Guys, How I can update the lebel of a field usi...
# general
n
Hi Guys, How I can update the lebel of a field using script?
e
There are a number of ways you can do this without a script depending on the context. For example, it's very easy to do just update a form and change a field label. But, that's not always possible if the field is added with a script. But, since you're asking about a script, the best way is to use a UserEvent script and update it in the beforeLoad context. It would look something like this below. Keep in mind that if the field is added by another script you would need to make sure this script fires after that script by reordering on the scripted record type.
Copy code
const myField = context.newRecord.getField({fieldId: "myfieldname"});
myField.label='New Field Label';
n
thanks, I’m using the same, but I’m not able to see the updated label in VIEW mode!!
I don’t understand why?? It should show in view mode too!
e
Check your deployment and make sure you're not filtering the contexts in which the script will fire.
n
No the context is all okay
/**
*@NApiVersion 2.x *@NScriptType UserEventScript */ define([‘N/record’], function(record) { function beforeLoad(context) { if (context.type == context.UserEventType.EDIT || context.type == context.UserEventType.VIEW){ var cust = context.newRecord; var form = context.form; var field = form.getField({id:‘vatregnumber’}); field.label = ‘VAT ID Number’; log.debug(‘field label’,field.label); } } } return { beforeLoad: beforeLoad }; });
code is this
did I miss something here?
e
I could be wrong, but I was thinking that you should call getField from the record, not the form.
Copy code
var field = cust.getField({fieldId:'vatregnumber'});
n
cust is a record not a form
e
I agree, it doesn't make a lot of sense.
There is a getField on the form as well but I think for what you're trying to do, you should be using the getField from the record and setting the label there.
n
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record'],
    function(record) {
        function beforeLoad(context) {
            if (context.type == context.UserEventType.EDIT || context.type == context.UserEventType.VIEW){

                var cust = context.newRecord;
                var form = context.form;
                var formId = cust.getValue({fieldId:'customform'});
                log.debug('form id',formId);
              
                var subsi = cust.getValue({fieldId:'subsidiary'});
            
                if(formId == '162'){
                if(subsi == '6' || subsi == '10' || subsi == '12' || subsi == '7' || subsi == '8'){
                    var field = cust.getField({id:'vatregnumber'});
                    field.label = 'VAT ID Number';
                    
                 
                  log.debug('field label',field.label);
                }
                }

            }
                
                
        }

        
        
        return {
            beforeLoad: beforeLoad
            
        };
    });
tried that too!
but still not working!
e
The getField() method on the record expects an argument of "fieldId" instead of "id". Try that.
n
updated that, but not affecting! stil same! does it matter if its a standard field?
e
I didn't think so. Curious, what are you seeing in your debug log. Does it appear that it's being updated when you're logging the value. Try updating a different field on the form to see if it changes. Could be related to that specific field. Also, check to see if the field has a custom label set on the form being used. Maybe that's overriding the label you're setting here. I didn't think it would.
n
okay, I’ll check and let you know!