Using g_form On Fields Outside Of Your Scope

I am working on a new scoped app. One of the things it does is sets fields to mandatory.

My app is a generic app designed to work on all fields, whether in scope or not. But with the scoped apps this isn’t possible.

It turns out there’s a check that looks to see if the field you’re amending is part of the scope or not, and if not, will print an error in the console along the lines of:

Mandatory true not set on field impact: cross-scope access denied.

While I understand the general purpose and rationale behind blocking cross scope g_form, in this particular instance, my application is designed to work across applications and the platform was getting in the way.

A perfect candidate then for me to go digging to see how it was working.

Firstly, it turns out scoped applications don’t use the standard g_form script, they have a very slightly customised version called scoped_g_form, which is then assigned to the g_form variable allowing it to function seamlessly.

The main difference in the scoped g_form script is that it checks the scope of elements before modifying them with functions such as setDisplay, setReadonly, setDisabled, setMandatory etc.

(It also includes a change that forces getReference to be asynchronous with a callback function, but that’s not the topic of this article).

So with all the above functions, it has a new internal function called inScope which returns true or false if an element is in the same scope as the script. If it returns true, the script is allowed to continue, if not, it prints the error above.

Next step was to understand how an element is marked in or out of scope. There’s a generic object called g_form.elements which is an array of objects storing all fields on a form.

Within the object, is simply a property called “scope” which stores the scope of the field and this is what’s checked in the “inScope” function. Simply updating this will give access to modify the field using setMandatory.

Rather than looping through the g_form.elements array, I used the helper function g_form.getGlideUIElement to get the correct object within the array.

//First set a variable with your scope
var myScope = "x_12345_myapplication";
//Set the field you want to edit
var field = "short_description";
//then find the variable and store its original scope so we can revert it later
var fElement = g_form.getGlideUIElement(field);
var originalScope = fElement.getScope() + "";
//Update the fields scope to your scope
fElement.scope = myScope;
//Then do whatever it is you wanted to do
g_form.setMandatory(field, true);
//Reset the scope for the next script
fElement.scope = originalScope;

Here we modify the scope, do our setMandatory and then re-set the scope back to it’s original so any other scripts that need to run still adhere to the cross scope access rights

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.