Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

integration getting gS\" is not defined while creating the REST - Create user in IAM - POST method

sierra2
Mega Expert

i am working on integration and while creating REST - Create user in IAM - POST method i am getting this error , please help me to fix this . the code is stopped running after line number 15, script has been attached.

//OIM Manager ID and Location values. We'll pass these into the function that creates the new user
var oimMgrID = workflow.scratchpad.oimMgrId;
gs.log('oimMgrID1', oimMgrID);
var oimMgrLoc = workflow.scratchpad.oimMgrLoc;
gs.log('oimMgrLoc1', oimMgrLoc);
var worknote = []; //work notes we'll write to the RITM
gs.log('worknote1', worknote);
workflow.scratchpad.operation = 'Run script \"REST - Create user in IAM - POST\"';

//Create a new user in OIM via REST POST
try {
	gs.log('u');
	workflow.info('Run script \"REST - Create user in IAM - POST\" - Create new user START');
	gs.log('w');
	var ccu = new cbi_COPAUtils();
	gs.log('123');
	var response = ccu.createOIMUser(current, oimMgrID, oimMgrLoc);
	//var response = ccu.createOIMUser(current, mgrVal, mgrRef);
	gs.log(' response01', response );
	var respArr = [];
	gs.log('respArr01', respArr);
	respArr = response.split('|');
	gs.log('e');
	//respArr[0] is the HTTP status code
	//respArr[1] is the response body
	var status = respArr[0];
	gs.log('status451',status);
	var respBody = respArr[1];
	gs.log(' respBodyoo', respBody);
	
	var jsonObj = new JSON.parse(respBody);
	gs.log('jsonObj001', jsonObj);
	
	var detail = jsonObj.detail; //if there's an error this object property will be present and have a value
	gs.log('detail000', detail);
	
	workflow.info('Run script \"REST - Create user in IAM - POST\" - Create OIM User response HTTP Status:' + status);
	workflow.info('Run script \"REST - Create user in IAM - POST\" - Create OIM User response body: ' + respBody);
	
	gs.log('t');
	if(status == '200' || status == '201') {
		activity.result = 'success';
		//Get info about the user from OIM that we'll need to add the user role
		var oimUID = jsonObj['id'];
		var metaObj = jsonObj['meta']; //the "meta" object contains the user's location URL
		var oimLoc = metaObj['location'];
		
		//Get the new user's info
		var userObj = jsonObj['urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'];
		workflow.scratchpad.employeeNumber = userObj.employeeNumber;
		workflow.scratchpad.oimUserID = oimUID;
		workflow.scratchpad.oimUserRef = oimLoc;
		
		workflow.info('OIM User ID: ' + oimUID);
		workflow.info('OIM Record Locator: ' + oimLoc);
		workflow.info('New User employee nbr: ' + workflow.scratchpad.newEmpNbr);
		
		//User creation successful. Update the RITM's work notes
		worknote.push('New user created in OIM with Employee number/User ID: ' + workflow.scratchpad.employeeNumber);
	} else if(status == '400' && detail.indexOf('already exists') > -1) { //Status 400 may indicate that the user already exists
		activity.result = 'success';
		var userEmail = current.variables.email.toString();
		workflow.info('Run script \"REST - Create user in IAM - POST\" - Getting existing OIM User record for email ' + userEmail);
		//Get the user record in OIM from their email
		var getUserResp = ccu.getOIMUser(userEmail);
		workflow.info('Run script \"REST - Create user in IAM - POST\" - Response from getOIMUser:\n ' + getUserResp);
		var respArr2 = [];
		respArr2 = getUserResp.split('|');
		//respArr[0] is the HTTP status code
		//respArr[1] is the response body
		var status = respArr2[0];
		var respBody2 = respArr2[1];
		var jsonObj2 = new JSON.parse(respBody2);

// 		for (var key in jsonObj2) {
// 			workflow.info('>>>Key: ' + key + ' | value: ' + jsonObj2[key]);
// 		} 

		var resources = jsonObj2["Resources"]; //get the Resources value, which is an array object with a single element
		var userObj = resources[0]; //array element 0 is a JSON object
		var isActive = userObj["active"]; //gets a boolean value [true|false]

		workflow.scratchpad.errCode = status;
		workflow.scratchpad.userIsActive = isActive;

		//If the user is inactive, update the worknotes
// 		if(!isActive) {
// 			worknote.push('User with email ' + userEmail + ' already exists and is inactive in OIM. Cancelling the request');
// 		}
	} else { //All other statuses and/or details indicate an error condition.
		activity.fault_description = status + ' | ' + respBody;
		workflow.scratchpad.errCode = status;
		workflow.scratchpad.errMsg = response.toString();
		workflow.scratchpad.cancelReason = 'Unable to create user in IAM';
		workflow.scratchpad.cancelDetail = 'ServiceBar was either unable to contact IAM or IAM returned an error that prevented further processing. The IT service desk has been notified.';
		worknote.push('Cancelling request due to web service error ' + status + ' in workflow: ' + workflow.scratchpad.operation);
		worknote.push(workflow.scratchpad.cancelReason);
		worknote.push(workflow.scratchpad.cancelDetail);
		
	}
	current.work_notes = worknote.join('\n');
}
catch(e) {
	workflow.error('CATCH Error in Run script \"REST - Create user in IAM - POST\" - ' + JSON.stringify(e));
}

{"message":"\"gS\" is not defined.","fileName":"#0(eval)","sourceName":"#0(eval)","lineNumber":41,"name":"ReferenceError","stack":"\tat sys_script_include.80ac0014dbc49700ee805dd5ce96192b.script:224 (anonymous)\n\tat #0(eval):41 (getApproverMgr)\n\tat #0(eval):2\n\tat <refname>:17\n\tat sys_script_include.d3669766c0a8016901828e92d1202ac5.script:279 (anonymous)\n\tat sys_script_include.0360b36d0a0a0b260a89dfec60c339c4.script:844 (anonymous)\n\tat sys_script.070a46fe0a0a0b260555062b357f0a83.script:13 (runWorkflow_userApprove)\n\tat sys_script.070a46fe0a0a0b260555062b357f0a83.script:9\n\tat sys_ui_action.845b8667c611227d01a264f153aaf953.script:2\n","rhinoException":{}}

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Is "cbi_COPAUtils" a custom class? There may be a typo of "gS" instead of "gs" in that class. 

View solution in original post

2 REPLIES 2

Hitoshi Ozawa
Giga Sage
Giga Sage

Is "cbi_COPAUtils" a custom class? There may be a typo of "gS" instead of "gs" in that class. 

Thank you Hitoshi it got resolved