in ADF lab 4.2.2, g_scratchpad does not work

MaxonLiang
Tera Contributor

in ADF course (Application Development Fundamentals (Zurich)), lab 4.2.2, two rules  are used to pass some data from server side to client side with g_scratchpad.

 

1. A "display" business rule, setting proerties to g_scratchpad

2. A client script, getting proerties from g_scratchpad and display a field in form
 
I strictly follow the guide in the course, except the "location_to_be_used" is replaced by me with "location_should_be_used" as the former one is not found, but this is not a matter.

When test above setting, I found client script get undefined values from g_scratchpad.
 
Then I make some debug:
1. Client script can display hard code value in "location_should_be_used" field.
2. Per some posts in community, in Client script side, I show content of g_scratchpad by getting keys with Object.keys(g_scratchpad), found only two keys/values in it,
browserSupported=true
isNewUI=true
there are no the properties set by business rule at all.
 
3. Per some posts in community, I rename the property in g_scratchpad with prefix 'u_', still not work
 
May I know what's the possible reason? Thanks.
 
below are details of the two rules, including my debug codes
 
business rules
MaxonLiang_0-1776483569298.png
 
code
 
(function executeRule(current, previous /*null when async*/) {

    g_scratchpad.city = current.requested_for.city;
    g_scratchpad.country = current.requested_for.country;
    g_scratchpad.u_city = 'city222';
    // Hooray for dotwalking!
    // By writing these values to the scratchpad, a client script can easily fetch them.
   
})(current, previous);
 
 
client script
MaxonLiang_1-1776483647860.png

 

function onLoad() {

    // This uses the dotwalking superpowers of the Scratchpad to use the requestor's city & state as the default location to be used. Make sure whomever you use for testing has values in their user record for this!

    if (g_form.getValue('location_should_be_used') != '') {
        return;
    }

    var city = g_scratchpad.city;
    var country = g_scratchpad.country;

    if (city && country) {
        g_form.setValue('location_should_be_used', city + ', ' + country);
    } else if (city) {
        g_form.setValue('location_should_be_used', city);
    } else if (country) {
        g_form.setValue('location_should_be_used', country);
    }
var u_city = g_scratchpad.u_city;
    var items = Object.keys(g_scratchpad);
//g_form.setValue('location_should_be_used', 'country111aaa'+'-'+u_city+'--'+country); // this return undefine for u_city and country
g_form.setValue('location_should_be_used', 'items len='+items.length+'item[1]='+JSON.stringify(g_scratchpad[items[1]]));
    if (city || country) {
        g_form.showFieldMsg('location_should_be_used', 'Value set automatically - you may override by editing', 'info');
    }
}
1 ACCEPTED SOLUTION

Tanushree Maiti
Mega Patron

Hi @MaxonLiang ,

If you’re seeing a g_scratchpad is not defined error, it usually means that no data has been passed to the g_scratchpad variable from the Display Business Rule.

This often occurs when the rule doesn’t include any logic to assign values to g_scratchpad.

 

Loaner request is custom table. from which table this custom table extends.

Asking because requested_for is OOB if field. If your custom table is not extending anything ,in that case requested_for  should be a custom field like u_requested_for.

 

Validate field name from your table. Pass the same to your Display  BR.

 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

View solution in original post

4 REPLIES 4

Tanushree Maiti
Mega Patron

Hi @MaxonLiang ,

If you’re seeing a g_scratchpad is not defined error, it usually means that no data has been passed to the g_scratchpad variable from the Display Business Rule.

This often occurs when the rule doesn’t include any logic to assign values to g_scratchpad.

 

Loaner request is custom table. from which table this custom table extends.

Asking because requested_for is OOB if field. If your custom table is not extending anything ,in that case requested_for  should be a custom field like u_requested_for.

 

Validate field name from your table. Pass the same to your Display  BR.

 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Despite requested_for  is valid or not, as we can see, i make another field u_city in the BR that hard code to a string

  g_scratchpad.u_city = 'city222';

however, in client script side, g_scratchpad.u_city is still undefined

A new finding, once i comment out below two lines in BR

// g_scratchpad.city = current.requested_for.city;
// g_scratchpad.country = current.requested_for.country;
then
g_scratchpad.u_city can be access normally in client script side.
So, seems we can say, once any undefined field is added to?g_scratchpad, even any valid field added later do not work, as?g_scratchpad seem has been "destroied" by the?undefined field.
Next? I will further check why?current.requested_for.city is undefined, though I had set it on the request for user per the lab requirement in advance .
Thanks for your prompt!

Finally it's found that

current.requested_for

should be

current.request_for

the field name is incorrect in the lab course🤣