Handle errors from script Include to business rule

Fabrizio Joaqui
Mega Guru

I need to handle 500 error of  script include in business rule, i need your help to do it, i have these 2 functions in my include script and i want to call them in my business rule only when insertUser gets 500 error how can i do it?:

i tried to do it but I don't think this is the best solution.

 

these are my 2 functions in script include:

 

 

 

//script Include
newUserId: function(){
		var r = new sn_ws.RESTMessageV2('success_factors_api','GETUSERS');
		r.setRequestHeader("Authorization", "Bearer " + token);
        var response = r.execute();
        var responseBody = JSON.parse(response.getBody());
		gs.info(responseBody);
        var httpStatus = response.getStatusCode();
		var arr = responseBody.d.results;	 
			
		//take the first string number
		function getUser(arr){
		for(var i = 0; i < arr.length; i++)
			if(/^\d+$/.test(arr[i].userId))
				return arr[i];
		}

		var user = getUser(arr);
		var sum = parseInt(user.userId) + 1;
		function padLeadingZeros(num, size) {
			var s = num+"";
			while (s.length < size) s = "0" + s;
			return s;
		}	
		var newUserId = padLeadingZeros(sum, 8);
		gs.info(newUserId);
		return newUserId; 
	},
	insertUser: function(reqUser){
		var requestUser = reqUser;
		var rinsertUser = new sn_ws.RESTMessageV2('success_factors_api','POSTUSER');
		rinsertUser.setRequestHeader("Authorization", "Bearer " + this.getToken());
        rinsertUser.setStringParameterNoEscape('messageBody', reqUser);
        var responseUser = rinsertUser.execute();
        var responseBodyUser = responseUser.getBody();
        var httpStatusUser = responseUser.getStatusCode();
		if(httpStatusUser === 200){
			gs.info(responseBodyUser);
			gs.info(httpStatusUser);
		}else{
			return httpStatusUser;
		}
		
	},

 

 

 

 

 I want to call them here in my business rule when i send User only if give me the error 500:

 

 

 

//Business Rule
var newUserId =  intUtils.newUserId // call newUserId function from script include
var reqBodyUser = {};
		reqBodyUser.__metadata = {
		uri: 'User(\'' + newUserId.toString() +'\')'
		};
		reqBodyUser.username = newUserId.toString();
		reqBodyUser.firstName = gr.u_nome.getDisplayValue();
		reqBodyUser.lastName = gr.u_cognome.getDisplayValue();
		reqBodyUser.status = 'active';
	
		reqBodyUserData = JSON.stringify(reqBodyUser);
		gs.info(reqBodyUserData);
		var rUser = intUtils.insertUser(reqBodyUserData);  //inserUser
		var times = 0;	
         if(rUser == 500){   //check if status of inserUser is 500
			while(times < 10){
				times ++;
				newUserId;  //Call the function that sum the last Userid +1
				rUser; // Call the function if times < 10
			}
			if(times == 10){
				throw new Error('STATUS: '+ rUser+' Something wrong with UserId');
			}		
		}

 

 

 

 

5 REPLIES 5

jaheerhattiwale
Mega Sage
Mega Sage

@Fabrizio Joaqui You are already doing it. What's the issue?

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Weird
Mega Sage

Assuming your script include is called "intUtils" you need to call it as new intUtils()

For example:

 

var rUser = new intUtils().insertUser(reqBodyUserData);

OR

var newUserId =  new intUtils().newUserId();

OR
var utils = new intUtils(); //Note that the whole script include call is now just a variable
var newUserId = utils.netUserId(); //Function still needs ()

 


Also, your first function call doesn't include a parameter, but your script include is using "token" variable.
You'd want to include that either as a parameter in the function call or get the value in the script include first.

You also need to use curly brackets in your if and for if you have more than one line inside.
For example:

 

function getUser(arr){
		for(var i = 0; i < arr.length; i++) //This is missing {}
			if(/^\d+$/.test(arr[i].userId)) //this is missing {}
				return arr[i];
}

Pretty sure your script would not run correctly with that.
Luckily you're not using this function

 

Also, it's generally bad practice to throw functions inside functions. Especially in between.
For example 

var newUserId = padLeadingZeros(sum, 8);

is calling a function that's above it. Makes it a lot harder to read the code.

Your BR is also referencing gr. Is it supposed to be current, or have you just cut of parts of the code?

@Weird  thanks for your advice, i put the gr. because i'm being tested by a fix script, I take the token from a previous function like I did for the "insertUser" it would be getToken().  this is my code in fix script:

 

 

 

 

var intUtils = new sn_hr_core.successFactorsUtils();
var newUserId =  intUtils.newUserId // call newUserId function from script include
var reqBodyUser = {};
		reqBodyUser.__metadata = {
		uri: 'User(\'' + newUserId.toString() +'\')'
		};
		reqBodyUser.username = newUserId.toString();
		reqBodyUser.firstName = gr.u_nome.getDisplayValue();
		reqBodyUser.lastName = gr.u_cognome.getDisplayValue();
		reqBodyUser.status = 'active';
	
		reqBodyUserData = JSON.stringify(reqBodyUser);
		gs.info(reqBodyUserData);
		var rUser = intUtils.insertUser(reqBodyUserData);  //inserUser
		var times = 0;	
         if(rUser == 500){   //check if status of inserUser is 500
			while(times < 10){
				times ++;
				newUserId;  //Callback the function that sum the last Userid +1
				rUser; // Callback the function if times < 10
			}
			if(times == 10){
				throw new Error('STATUS: '+ rUser+' Something wrong with UserId');
			}		
		}

 

 

 

  my question is: is correct to write newUserId and rUser how did i do it inside the while?

No, at least the way you're doing it is incorrect since you're not storing that information anywhere.
When you set

var rUser = intUtils.insertUser(reqBodyUserData); 

you're actually storing the value it returns, not the function itself.
So it could be 500 and you're checking for that, but in the while loop you're just saying the value and nothing is done with it.

Next you actually want to check that the value is not 500 while loping.
while(rUser == 500 && times < 10)
And in the script you can again try calling the function. That way it will loop until rUser is not 500 or times = 10.

var times = 0;	
         if(rUser == 500){   //check if status of inserUser is 500
			while(times < 10 && rUser == 500){
				times ++;
				//newUserId;  //Callback the function that sum the last Userid +1, this logic should change as well.
				var rUser = intUtils.insertUser(reqBodyUserData);  //inserUser
			}


If you need to initialize newUserId every time as well, then you'd have to do that every time before rUser as well. I'd recommend calling newUserId as a separate function, so you don't need to write the same script multiple times.