What's the context, is this a client script I assu...
# suitescript
s
What's the context, is this a client script I assume?
g
Not the full script, but for context.
Copy code
function checkForChanges(request) {
    // Find Employees
    var employeeSearch = search.create({
        type: search.Type.EMPLOYEE,
        columns: [
            'internalid',
            'email',
            'firstname',
            'lastname',
        ],
    });

    employeeSearch.run().each(function (result) {
        var internalId = result.getValue('internalid');
        var emailAddress = result.getValue('email');
        var firstname = result.getValue('firstname');
        var lastname = result.getValue('lastname');

        // We require email address.
        if (emailAddress === '') {
            log.debug({
                title: 'Employee with empty email.',
                details: internalId + ' : ' + firstname + ' ' + lastname,
            });

            // Next!
            return true;
        }

        if (!Object.prototype.hasOwnProperty.call(request, emailAddress)) {
            // Not present.
            return true;
        }

        // Track which employees were found and updated.
        request[emailAddress].handled = true;

        // Found an entry.
        updateEmployee(internalId, request[emailAddress]);

        // Continue each iteration.
        return true;
    });
}

function updateEmployee(id, employeeData) {
    log.debug({
        title: 'Lookup: ' + id,
    });
    var employeeRecord = record.load({
        type: record.Type.EMPLOYEE,
        id: id,
    });
    log.debug({
        title: 'Found: ' + employeeRecord.getValue('email'),
    });

    if (isEmployeeAdmin(employeeRecord)) {
        // Not updating Administrator employees.
        log.debug({
            title: 'Not updating administrator: ' + employeeRecord.getValue('email'),
        });

        return;
    } else {
        log.debug({
            title: 'Checking: ' + employeeRecord.getValue('email'),
        });
    }

    var employee = new recordWrapper(employeeRecord);

    employee.setIfChanged('firstname', employeeData.givenName);
    employee.setIfChanged('lastname', <http://employeeData.sn|employeeData.sn>);

    employee.setIfChanged('officephone', employeeData.telephoneNumber);
    employee.setIfChanged('mobilephone', employeeData.cell);

    employee.setIfChanged('custentity_building', employeeData.location);

    var deptId = departments.getValueId(employeeData.department);
    employee.setIfChanged('custentity_functional_dept', deptId);

    employee.saveIfChanged();
}

function isEmployeeAdmin(employeeRecord) {
    var roleCount = employeeRecord.getLineCount({
        sublistId: 'roles',
    });

    for (var i = 0; i < roleCount; i++) {
        var roleId = parseInt(employeeRecord.getSublistValue({
            sublistId: 'roles',
            fieldId: 'selectedrole',
            line: i,
        }));
        if (roleId === ADMINISTRATOR_ID) {
            return true;
        }
    }

    return false;
}
It fails on
Copy code
var employeeRecord = record.load({
        type: record.Type.EMPLOYEE,
        id: id,
    });
s
You aren't going to be able to load employee records without a higher permission on the automation role. Is there a problem just giving that role permissions to employees?
g
I think the problem is that the restlet automation script executes as a non-administrator role. I don't want a restlet to be a full administrator.
The automation script can view the roles of the administrator through the UI. Just can't click edit on an administrator. I want to do the same in the script, view the roles on the employee, but not edit.
I have an alternative I can try, pulling in role to the saved search, but then that requires an extra step and a little more scripting to check all entries of the search results first and identify administrators to exclude, then iterate again to process updates.
n
If you're including the role in the search why not filter out administrators in the search rather than remove / skip them in the results?