- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2017 10:47 PM
Hi All, I am trying to learn using g_scratchpad variable in business rules and have made a simple program that is not working, so request some guidance.
1. I have a table called check-in and it has a reference field called guest.
2. I have a table called Guest and that has a field called VIP
3. I want a message to be shown using g_scratchpad when someone tries to checkin a VIP guest.
4. I created a display business rule on checkin table (Get VIP Flag) with the following script:
(function executeRule(current, previous /*null when async*/) {
g_scratchpad.vip='current.x_53167_hotel1_guest.vip';
})(current, previous);
5. Then I have the following client script (Alert for VIP)
function onChange(control, oldValue, newValue, isLoading,
isTemplate) {
var showVIP = function() {
g_form.showFieldMsg('x_53167_hotel1_check_in.guest','Guest is a VIP');
};
if (isLoading) {
if(g_scratchpad.vip'){
showVIP();
}
return;
}else{
g_form.hideFieldMsg('x_53167_hotel1_check_in.guest');
}
if(newValue==''){
return;
}
}
6. The logs dont seem to have any error captured
22:42:32.622: App:Hotel1 ==> 'Get VIP flag' on x_53167_hotel1_check_in:CHE0001128
22:42:32.623: >> Entering scope [x_53167_hotel1]
22:42:32.623: << Exited scope [x_53167_hotel1], popped back into [rhino.global]
22:42:32.623: App:Hotel1
<== 'Get VIP flag' on x_53167_hotel1_check_in:CHE0001128
22:42:32.623: Finished executing before_display before business rules on x_53167_hotel1_check_in:CHE0001128
Please help. Thanks a lot.
Solved! Go to Solution.
- Labels:
-
Personal Developer Instance

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2017 09:45 AM
Hi Vivek,
I think you need to do GlideAjax, as per my understanding Business Rule get trigger when form loads or you save the record. When you will do onChange then at time there is no action happening on form. I tried below code and it works fine, please check if this helps.
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var sys_id = newValue;
var userDetails = new GlideAjax("DisplayUserDetails");
userDetails.addParam("sysparm_name", "getUserDetails");
userDetails.addParam("sysparm_sys_id", sys_id);
userDetails.getXML(ajaxResponse);
function ajaxResponse(serverResponse) {
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
if(answer == 'true')
g_form.showFieldMsg('caller_id', 'User is a VIP');
else
g_form.showFieldMsg('caller_id', 'User is NOT VIP');
}
}
Script Include:
var DisplayUserDetails = Class.create();
DisplayUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails : function() {
var sysid = this.getParameter('sysparm_sys_id');
var newUserRecord = new GlideRecord('sys_user');
newUserRecord.addQuery('sys_id', sysid);
newUserRecord.query();
if(newUserRecord.next())
{
var guestIsVIP = (newUserRecord.vip) ? true : false;
return guestIsVIP;
}
},
type: 'DisplayUserDetails'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2017 08:48 AM
Hi Vivek,
Sorry for little confusions .
it will not work in onChange(). you need to travel to server to get the value and here we are just doing manipulation on form level.
if you write the script in onLoad() it will work perfectly.
g_scratchpad is only required if/when we go down the route of adding a custom field on the user table to trigger the alert.
if your requirement is to show the alert on the changing of users then as chirag.bagdai explorenow have provided the solution will work perfectly.
Note: if you want to use g_scratchpad then you must save or reload the form to get the value from the server . "You need to travel to the server to get the value in client".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2017 04:58 PM
the solution should work when I change the user from a non VIP to VIP as well as when user field is blank at first and it is getting changed to VIP user. The second scenario is the one that I am trying right now.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2017 05:06 PM
Hi Vivek,
Since you do not want to use getReference() through client script then i will recommend you to use GlideAjax (below code).
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var sys_id = newValue;
var userDetails = new GlideAjax("DisplayUserDetails");
userDetails.addParam("sysparm_name", "getUserDetails");
userDetails.addParam("sysparm_sys_id", sys_id);
userDetails.getXML(ajaxResponse);
function ajaxResponse(serverResponse) {
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
if(answer == 'true')
g_form.showFieldMsg('caller_id', 'User is a VIP');
else
g_form.showFieldMsg('caller_id', 'User is NOT VIP');
}
}
Script Include:
var DisplayUserDetails = Class.create();
DisplayUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails : function() {
var sysid = this.getParameter('sysparm_sys_id');
var newUserRecord = new GlideRecord('sys_user');
newUserRecord.addQuery('sys_id', sysid);
newUserRecord.query();
if(newUserRecord.next())
{
var guestIsVIP = (newUserRecord.vip) ? true : false;
return guestIsVIP;
}
},
type: 'DisplayUserDetails'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2017 05:19 PM
yes, sure this would be my next step. I am just trying to get all my bearings right first on scratchpad. I am not able to understand why picking a VIP and Non VIP is always returning false. Any thoughts ?
(function executeRule(current, previous /*null when async*/) {
g_scratchpad.uvip=current.guest.vip;
gs.info('this is test:' + g_scratchpad.uvip);
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2017 06:03 PM
Hi Vivek,
Let me try to explain what the issue is though harsh vardhan has already explained
there is no issue in using g_scratchpad in Business Rule, it's getting the VIP true/false but it won't work with onChange() Client Script, you can make it work with onLoad() Client script but for that you need to save the form every time when you make changes in Guest field.
On Change of Guest field you are invoking the client side onChange Script that is not invoking any server side code since there is no GlideAjax call to make server side call, that's why every time you get false because there is no return value (no validation if user is VIP or not), but when you use GlideAjax then you are getting the value from server side and which is validating is it's true or false.
onLoad client script, when you are saving the record after changing the Guest Value then at that time Business Rule gets call and it returns the value to Client Script using g_scratchpad and display the message.
So, that means you can't use g_scratchpad with onChange() client script instead you can use the same with onLoad() client script OR onChange() Client Script with GlideAjax.
Just for you understanding, below Code Used with Business Rule and onLoad() client script using g_scratchpad.
Non-VIP User
VIP User
Business Rule:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
g_scratchpad.isUserVIP = current.caller_id.vip;
gs.addInfoMessage('User is VIP' + g_scratchpad.isUserVIP);
})(current, previous);
onLoad Client Script:
function onLoad() {
//Type appropriate comment here, and begin script below
if(g_scratchpad.isUserVIP == 'true')
g_form.showFieldMsg('caller_id', 'User is a VIP');
else
g_form.showFieldMsg('caller_id', 'User is NOT VIP');
}
Hope this helps.