Andrew Arrow
Andrew Arrow

Reputation: 4565

Netsuite: how can I add an employee with a role, access, and a password via SuiteScript?

This code work great for creating the employee, but the password and giveAccess fields are not set:

function CreateEmployee() {
  nlapiLogExecution('DEBUG','running create employee',1);
  var employeeRec = nlapiCreateRecord('employee'); 
  employeeRec.setFieldValue('lastname', 'Aloe');
  employeeRec.setFieldValue('firstname', 'Danny');
  employeeRec.setFieldValue('email', '[email protected]');
  employeeRec.setFieldValue('subsidiary', 3);
  employeeRec.setFieldValue('giveAccess', true);
  employeeRec.setFieldValue('role', 3);
  employeeRec.setFieldValue('password', 'Somepassword1!');
  employeeRec.setFieldValue('password2', 'Somepassword1!');
  var id = nlapiSubmitRecord(employeeRec);
  nlapiLogExecution('DEBUG','done: ' + id + ' employee',id);

  var result = new Object();
  result.id = id;

  return result;
}

When I go to the web interface and pull up the employee record, the "Access" tab does not have the giveAccess checkbox checked. And trying to login as the new user does not work. Is there a trick other than employeeRec.setFieldValue to set these values?

Upvotes: 1

Views: 3478

Answers (4)

sirtrumsalot
sirtrumsalot

Reputation: 1

/**

  • @NApiVersion 2.x
  • @NScriptType Suitelet
  • @NModuleScope SameAccount */

define(['N/record', 'N/ui/serverWidget'], function(record, serverWidget) {

function onRequest(context) {

  if (context.request.method === 'GET') {
      // Create a form
      var form = serverWidget.createForm({
          title: 'Create User'
      });
      
      // Add fields to the form
      var firstNameField = form.addField({
          id: 'custpage_first_name',
          label: 'First Name',
          type: serverWidget.FieldType.TEXT
      });
      
      var lastNameField = form.addField({
          id: 'custpage_last_name',
          label: 'Last Name',
          type: serverWidget.FieldType.TEXT
      });
      
      var emailField = form.addField({
          id: 'custpage_email',
          label: 'Email',
          type: serverWidget.FieldType.EMAIL
      });
      
      var passwordField = form.addField({
          id: 'custpage_password',
          label: 'Password',
          type: serverWidget.FieldType.PASSWORD
      });
      
      var roleField = form.addField({
          id: 'custpage_role',
          label: 'Role',
          type: serverWidget.FieldType.SELECT,
          source: 'role'
      });
      
      // Add a submit button to the form
      form.addSubmitButton({
          label: 'Create User'
      });
      
      // Display the form
      context.response.writePage(form);
      
  } else {
      
      // Retrieve form data
      var firstName = context.request.parameters.custpage_first_name;
      var lastName = context.request.parameters.custpage_last_name;
      var email = context.request.parameters.custpage_email;
      var password = context.request.parameters.custpage_password;
      var roleId = context.request.parameters.custpage_role;
      
      // Create a new user record
      var user = record.create({
          type: record.Type.EMPLOYEE
      });
      
      user.setValue({
          fieldId: 'firstname',
          value: firstName
      });
      
      user.setValue({
          fieldId: 'lastname',
          value: lastName
      });
      
      user.setValue({
          fieldId: 'email',
          value: email
      });
      
      user.setValue({
          fieldId: 'password',
          value: password
      });
      user.setValue({
        fieldId: 'password2',
        value: password
    });
      
      user.setValue({
          fieldId: 'giveaccess',
          value: true
      });
      
      user.setSublistValue({
        sublistId: 'roles',
        fieldId: 'selectedrole',
        line: 0,
        value: roleId,
    });
      
       try {
          // Save the new user record
          var userId = user.save({
              enableSourcing: false,
              ignoreMandatoryFields: true
          });
          
          // Display a success message
          var message = 'User created successfully with ID: ' + userId + ' ' + firstName;
          context.response.write(message);
      } catch (ex) {
        
          // Display the error message with the role value
          var message = 'Error creating user. ' + ex.message;
          context.response.write(message);
      }
  }

}

return { onRequest: onRequest };

});

Upvotes: 0

hesiode
hesiode

Reputation: 33

Here is a working example in suitescrpit 2.0 to add a role on an employee.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record'],
/**
 * @param {record} record
 */
function(record) {

    function beforeSubmit(scriptContext) {
        log.debug('in the script');
        scriptContext.newRecord.setSublistValue({
            sublistId: 'roles',//'jobresources
            fieldId: 'selectedrole',//'jobresource',
            line: 3,
            value: 4
        });
    }



    return {
        beforeSubmit: beforeSubmit,
    };

});

Upvotes: 0

w3bguy
w3bguy

Reputation: 2250

Not sure if you ever found the answer or not... I know this is an old post.

I was able to update the user roles programmatically Below is the Mass Update Script I'm using. It can show you how to add a role to a user with SuiteScript.

function massUpdate(recType,recID){
  var roleID=1234;
  var empRec=nlapiLoadRecord(recType,recID);
  var roleCount=empRec.getLineItemCount('roles');
  var thiRole=empRec.setLineItemValue('roles','selectedrole',roleCount+1,roleID);
  var submitRec=nlapiSubmitRecord(empRec);
}

The issue you had was that you were setting a field value, instead of a sublist field value. Hope that helps.

Upvotes: 1

Mike Robbins
Mike Robbins

Reputation: 3287

Checking the box by itself, whether through the UI or scripting, isn't enough to grant access. You also need to specify a role for the user to use.

First glance through the scriptable records browser, it doesn't seem that roles are scriptable except as search filters and search columns.

https://system.netsuite.com/help/helpcenter/en_US/RecordsBrowser/2013_2/Records/employee.html

Upvotes: 0

Related Questions