```/** *@NApiVersion 2.x *@NScriptType ClientScr...
# suitescript
m
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(['N/search'],
function(search){
	
	const SIMILAR_USER = 'custrecord_similar_user';
	const SIMILAR_USER_ROLE = 'custrecord_similar_user_roles';

	function fieldChanged(context) {
		var currentRecord = context.currentRecord;
		var fieldName = context.fieldId;
		if (fieldName === SIMILAR_USER){
			var similarUser = currentRecord.getValue(SIMILAR_USER);
			if(similarUser){
				var allEmpRoles = getUereRoles(similarUser);
				if(allEmpRoles){
					currentRecord.setValue({
						fieldId: SIMILAR_USER_ROLE,
						value: allEmpRoles
					});
				}
			}
			else{
				currentRecord.setValue({
					fieldId: SIMILAR_USER_ROLE,
					value: null
				});
			}
			return true;
		}
	}
	
	function getUereRoles(empID){
		var empRoles = '';
		var employeeSearchObj = search.create({
			type: "employee",
			filters:
			[
				["internalid","anyof",empID]
			],
			columns:
			[
				"role"
			]
		});
		employeeSearchObj.run().each(function(result){
			var currRole = result.getText("role");
			empRoles = empRoles + currRole+" \n";
			return true;
		});
		return empRoles;
	}

	return {
		fieldChanged: fieldChanged
	};
});