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.

How to use 'setWorkflow' in scoped app scripting

sheikhassan
Tera Expert

I am getting an error Access to api 'setWorkflow' has been refused due to the api's cross-scope access policy.

How to fix this error?

Any help wold be appreciated.

5 REPLIES 5

hi Mandy,

 

I have pasted two examples:

 

Example A: An example to avoid business rule before queries in a script include in the same scope as the object, can be done as follow:

Get a Hr Profile record in a "Human Resources: Core" scope script include

//get the hr profile based on unique key "intuserid"
var hrProfile = new GlideRecord('sn_hr_core_profile');
hrProfile.setWorkflow(false); //Avoid BS Rule before query which limits results for users
var profile = hrProfile.get(intuserid) ? hrProfile : null;

 

Example B : When you want to query a record in a script include which is in a different scope than the object , you need to have an additional script include.

 Example: script include below Global scope performing a query on HR Scope using the function getHrProfile

//get HR Profile by User_ID (sys_id) from a Script Include in a Global scope
var coreHRScript = new sn_hr_core.scriptsIncludeHrCore();
var grHrProfile = coreHRScript.getHrProfile(User_ID);

gs.info("HR Profile number: "+grHrProfile.getValue('number'));

Below an example script include in Human Resources: Core Scope having a function getHrProfile with a query with setworkflow(false) which avoids Before Business Rule queries.

var scriptsIncludeHrCore = Class.create();
scriptsIncludeHrCore.prototype = {

initialize: function() {},

getHrProfile: function(userId){
	var grHrProfile = new GlideRecord('sn_hr_core_profile');
	grHrProfile.addQuery('user', userId);
	grHrProfile.setWorkflow(false);
	grHrProfile.query();
	if (grHrProfile.next()) {
		return grHrProfile;
	}else{
		return;
	}
},

 

Please mark helpfull or answered when it does 🙂

 

regards, Peter