To utilize an employee search created in a client script, even with a role that does not have employee and employee record permissions. We can use the backend suitelet script to overcome the permission issue.
This approach involves passing search criteria data as parameters to the suitelet script and executing the suitelet in an admin role. Once the search is completed, the results are transmitted to the client script for further processing.
Client script code part:
/**
* Function to be executed when field is changed.
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
* @since 2015.2
*/
function fieldChanged(scriptContext) {
try {
if (scriptContext.fieldId == "entity") {
let location = scriptContext.currentRecord.getValue('location');
console.log('entlocation', location);
let salesRepValue = scriptContext.currentRecord.getValue('salesrep');
console.log('entsalesRepValue', salesRepValue);
if (userLocation && !salesRepValue) {
scriptContext.currentRecord.setValue('location', userLocation);
} else {
getRepLocation(salesRepValue)
.then(function (repLocation) {
if (repLocation && repLocation != location)
scriptContext.currentRecord.setValue('location', repLocation);
console.log('entrepLocation', repLocation);
})
.catch(function (error) {
console.log('Error in getRepLocation:', error);
});
}
}
} catch (err) {
console.log("error@fieldChanged", err)
}
};
/**
* Function to call suitelet to perform the employee search
* @param {number} salesRepValue
* @returns
*/
function getRepLocation(salesRepValue) {
return new Promise(function (resolve) {
try {
console.log("salesRepValue", salesRepValue)
let repLocation;
let suiteletURL = url.resolveScript({
scriptId: 'customscript_jj_sl_employee_srch_pow_419',
deploymentId: 'customdeploy_jj_sl_employee_srch_pow_419',
returnExternalUrl: false,
params: {
'repId': salesRepValue,
}
});
// Use https.get.promise to make the request
https.get.promise({
url: suiteletURL
}).then(function (response) {
console.log("response", response)
// Handle the response from the Suitelet (repLocation)
repLocation = JSON.parse(response.body);
console.log("Rep Location: " + repLocation);
console.log("repLocation.status",repLocation.status);
console.log("repLocation.value",repLocation.value);
if(repLocation.status == "success" && repLocation.value ){
resolve(Number(repLocation.value));
}
// Resolve the Promise with repLocation
}).catch(function (error) {
console.log("Error in https.get.promise: " + error);
});
} catch (err) {
console.log("error@getRepLocation", err);
}
});
};
Suitelet script:
/**
* Defines the Suitelet script trigger point.
* @param {Object} scriptContext
* @param {ServerRequest} scriptContext.request - Incoming request
* @param {ServerResponse} scriptContext.response - Suitelet response
* @since 2015.2
*/
const onRequest = (scriptContext) => {
try{
if (scriptContext.request.method === 'GET') {
let repLocation = null;
let repId = Number(scriptContext.request.parameters.repId);
if (!repId) {
log.error("No repId passed to SuiteLet");
scriptContext.response.write(JSON.stringify({"status":"error", "value": "No sales rep id is found."}));
}
let employeeSearchObj = search.create({
type: "employee",
filters:
[
["internalid", "anyof", repId]
],
columns:
[
search.createColumn({ name: "location", label: "Business Unit" })
]
});
employeeSearchObj.run().each(function (result) {
repLocation = result.getValue({ name: "location", label: "Business Unit" });
return false;
});
scriptContext.response.write(JSON.stringify({"status":"success", "value": repLocation}));
}
}catch(e){
log.error("error@suielet",e);
scriptContext.response.write(JSON.stringify({"status":"error", "value":e.message}));
}
}
return { onRequest }
});