I have a script that is giving me an error and I h...
# suitescript
t
I have a script that is giving me an error and I have no idea what to fix, this is my first attempt at writing a script so any help would be great The error is below, along with my script/** * @NApiVersion 2.x * @NScriptType UserEventScript * @NModuleScope SameAccount */ define(['N/record', 'N/runtime'], function(record, runtime) { function beforeLoad(context) { // Check if the user has the Advanced Partner Center role var currentUser = runtime.getCurrentUser(); var userRoleIds = currentUser.roleIds; if (userHasRole(userRoleIds, '1127')) { if (context.type === context.UserEventType.VIEW || context.type === context.UserEventType.EDIT) { var currentRecord = context.newRecord; var lineItemCount = currentRecord.getLineCount({ sublistId: 'item' }); // List of column IDs to disable var columnsToDisable = ['description', 'price', 'rate']; // Add more column IDs as needed // Loop through each line item for (var i = 0; i < lineItemCount; i++) { // Disable specific columns on each line item columnsToDisable.forEach(function(columnId) { currentRecord.getSublistField({ sublistId: 'item', fieldId: columnId, line: i }).isDisabled = true; // Set to true to disable the field }); } } } } return { beforeLoad: beforeLoad }; });
e
It can't find the function userHasRole
t
Here is the updated version, now I get a new error, any ideas? /** * @NApiVersion 2.x * @NScriptType UserEventScript * @NModuleScope SameAccount */ define(['N/record', 'N/runtime'], function(record, runtime) { function beforeLoad(context) { // Check if the user has the Advanced Partner Center role var currentUser = runtime.getCurrentUser(); var userRoleIds = currentUser.roleIds; if (userHasRole(userRoleIds, 1127)) { if (context.type === context.UserEventType.VIEW || context.type === context.UserEventType.EDIT) { var currentRecord = context.newRecord; var lineItemCount = currentRecord.getLineCount({ sublistId: 'item' }); // List of column IDs to disable var columnsToDisable = ['description', 'price', 'rate']; // Add more column IDs as needed // Loop through each line item for (var i = 0; i < lineItemCount; i++) { // Disable specific columns on each line item columnsToDisable.forEach(function(columnId) { currentRecord.getSublistField({ sublistId: 'item', fieldId: columnId, line: i }).isDisabled = true; // Set to true to disable the field }); } } } } // Helper function to check if user has a specific role function userHasRole(roleIds, targetRoleId) { return roleIds.indexOf(targetRoleId) !== -1; } return { beforeLoad: beforeLoad }; });
e
runtime.User
does not have a `roleIds`property. It has
roleId
which is the role the user is currently using.
t
@ehcanadian - I am sorry, I am new to doing this, I am not sure I am following you, what do I need to change. I am sorry I am very novice at this.
e
Copy code
var userRoleId = currentUser.roleId;
if (userRoleId === 1127))
t
Thank you, that got rid of the error, but the fields (columns) are not disabled. Sorry I am full of questions, but I am trying to learn this on the fly.
e
NS has inconsistencies when it comes to strings and numbers, so you may need to try
if(userRoleId == 1127)
instead. Double vs triple equals. You'd also need to be viewing or editing the record using the role with that id.
t
So your saying get rid of this line and replace it with what your suggesting: if (userHasRole(userRoleId, '1127'))
e
Yeah, unless you want to create a search to find what roles the user has. runtime.getCurrentUser().roleId will only return one id, which is what the user is using
t
It will only always be 1 id, that is the only role that will ever be affected by this script.
UGH, I have tired your suggestion, both with 3 equal signs, and 2 equal signs and the columns are still not disabled. And yes, I am logged in using that specific role.
Will this only work when editing or viewing a Sales, not when they are creating one. I need this to fire when they are creating a Sales Order and or Editing an existing one.
e
That's what the line that contains the
context.type === context.UserEventType.VIEW....
is for
You could add some logging to determine what is happening with
log.debug('TITLE', value)
t
I added a Hello World message so I could tell if my script is even running, but I do not get the message pop up. Below is my current script state. I tried adding Create to the user event type line, but that is not an option, I did create a sales order but even when I go in and edit the SO, it still lets me edit the columns. /** * @NApiVersion 2.x * @NScriptType UserEventScript * @NModuleScope SameAccount */ define(['N/record', 'N/runtime'], function(record, runtime) { function beforeLoad(context) { // Check if the user has the Advanced Partner Center role var currentUser = runtime.getCurrentUser(); var userRoleId = currentUser.roleId; if (userRoleId == 1127) { if (context.type === context.UserEventType.VIEW || context.type === context.UserEventType.EDIT) { var currentRecord = context.newRecord; var lineItemCount = currentRecord.getLineCount({ sublistId: 'item' }); // List of column IDs to disable var columnsToDisable = ['description', 'price', 'rate']; // Add more column IDs as needed // Loop through each line item for (var i = 0; i < lineItemCount; i++) { // Disable specific columns on each line item columnsToDisable.forEach(function(columnId) { currentRecord.getSublistField({ sublistId: 'item', fieldId: columnId, line: i }).isDisabled = true; // Set to true to disable the field }); } } } // Log "hello world" message log.debug({ title: 'Hello World!', details: 'This is a hello world message from the script.' }); } // Helper function to check if user has a specific role function userHasRole(roleId, targetRoleId) { return roleId.indexOf(targetRoleId) !== -1; } return { beforeLoad: beforeLoad }; });
n
instead of this:
Copy code
currentRecord.getSublistField({
                            sublistId: 'item',
                            fieldId: columnId,
                            line: i
                        }).isDisabled = true;
try this:
Copy code
let thisField = context.form.getSublist('item').getField(columnId) || '';
if(thisField !==''){
 thisField.updateDisplayType({displayType: 'disabled'}) : '';
}
Also note, log.debug does not "pop a message" you will see it in the script logs. (make sure your logging level on the deployment is set to "DEBUG")