Skip to content

Commit 02b34a8

Browse files
authored
Merge branch 'ServiceNowDevProgram:main' into main
2 parents 87c34bf + 7ef9ddf commit 02b34a8

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Auto-fill form from base64encoded data
2+
3+
This widget automatically populates a form on service portal using base64-encoded JSON passed via a URL parameter. Use the variable name as key or for surveys use the sys_id of the [asmt_assessment_instance_question]
4+
5+
## How to
6+
7+
1. Create a new widget and add the server script and client controller from the *Survey or form filler widget.js* file
8+
2. Add the widget for example as a catalog item variable or add it anywhere on your survey taking page such as *take_survey*
9+
3. Encode json of form fields and values key-value pairs and add it as a parameter called data into your url
10+
4. Navigate to page and form autofills
11+
12+
## To note
13+
For catalog items the variable name is the key, for surveys the key to use is the sys_id of the [asmt_assessment_instance_question]
14+
15+
16+
## Example usage
17+
18+
```javascript
19+
var obj = {
20+
is_this_a_replacement_for_a_lost_or_broken_iphone: "yes",
21+
what_was_the_original_phone_number: "1234567980",
22+
monthly_data_allowance: "Unlimited",
23+
color: "red",
24+
storage: "256",
25+
}
26+
27+
gs.info(GlideStringUtil.base64Encode(JSON.stringify(obj)))
28+
/*
29+
*** Script: eyJpc190aGlzX2FfcmVwbGFjZW1lbnRfZm9yX2FfbG9zdF9vcl9icm9rZW5faXBob25lIjoieWVzIiwid2hhdF93YXNfdGhlX29yaWdpbmFsX3Bob25lX251bWJlciI6IjEyMzQ1Njc5ODAiLCJtb250aGx5X2RhdGFfYWxsb3dhbmNlIjoiVW5saW1pdGVkIiwiY29sb3IiOiJyZWQiLCJzdG9yYWdlIjoiMjU2In0=
30+
-->
31+
https://{instancename}.service-now.com/esc?id=sc_cat_item&sys_id=ec80c13297968d1021983d1e6253af32&data=eyJpc190aGlzX2FfcmVwbGFjZW1lbnRfZm9yX2FfbG9zdF9vcl9icm9rZW5faXBob25lIjoieWVzIiwid2hhdF93YXNfdGhlX29yaWdpbmFsX3Bob25lX251bWJlciI6IjEyMzQ1Njc5ODAiLCJtb250aGx5X2RhdGFfYWxsb3dhbmNlIjoiVW5saW1pdGVkIiwiY29sb3IiOiJyZWQiLCJzdG9yYWdlIjoiMjU2In0%3D
32+
*/
33+
var arr = {
34+
"b3bf8ec283683210557ff0d6feaad327": 1,
35+
"bbbf8ec283683210557ff0d6feaad326": 2,
36+
"b7bf8ec283683210557ff0d6feaad327": 3,
37+
"bfbf8ec283683210557ff0d6feaad326": 4,
38+
"fbbf8ec283683210557ff0d6feaad325": "it is good"
39+
}
40+
gs.print(GlideStringUtil.base64Encode(JSON.stringify(arr)))
41+
/*
42+
*** Script: eyJiM2JmOGVjMjgzNjgzMjEwNTU3ZmYwZDZmZWFhZDMyNyI6MSwiYmJiZjhlYzI4MzY4MzIxMDU1N2ZmMGQ2ZmVhYWQzMjYiOjIsImI3YmY4ZWMyODM2ODMyMTA1NTdmZjBkNmZlYWFkMzI3IjozLCJiZmJmOGVjMjgzNjgzMjEwNTU3ZmYwZDZmZWFhZDMyNiI6NCwiZmJiZjhlYzI4MzY4MzIxMDU1N2ZmMGQ2ZmVhYWQzMjUiOiJpdCBpcyBnb29kIn0=
43+
-->
44+
https://{instancename}.service-now.com/esc?id=take_survey&type_id=cf6e97d35d371200964f58e4abb23f18&data=eyJiM2JmOGVjMjgzNjgzMjEwNTU3ZmYwZDZmZWFhZDMyNyI6MSwiYmJiZjhlYzI4MzY4MzIxMDU1N2ZmMGQ2ZmVhYWQzMjYiOjIsImI3YmY4ZWMyODM2ODMyMTA1NTdmZjBkNmZlYWFkMzI3IjozLCJiZmJmOGVjMjgzNjgzMjEwNTU3ZmYwZDZmZWFhZDMyNiI6NCwiZmJiZjhlYzI4MzY4MzIxMDU1N2ZmMGQ2ZmVhYWQzMjUiOiJpdCBpcyBnb29kIn0%3D
45+
*/
46+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//Server script:
2+
(function () {
3+
//get the data parameter with base64encoded json with field names for keys and matching values
4+
data.encodedData = $sp.getParameter("data");
5+
try {
6+
//turn the data back into a js object
7+
data.decodedData = JSON.parse(GlideStringUtil.base64Decode(data.encodedData));
8+
} catch (e) {
9+
//bad json; do nothing
10+
return;
11+
}
12+
})();
13+
14+
15+
//Client controller:
16+
api.controller = function ($scope, $rootScope) {
17+
18+
var c = this,
19+
g_form = $scope.page.g_form,
20+
answerMap = c.data.decodedData;
21+
// return if the answermap is not a valid object
22+
if (!answerMap instanceof Object)
23+
return;
24+
// if we have g_form set values from the data parameter to form
25+
if (g_form) {
26+
fillAnswers();
27+
}
28+
//loops through the object with field name keys and corresponding values and sets values
29+
function fillAnswers() {
30+
for (key in answerMap) {
31+
if (!answerMap.hasOwnProperty(key) && !g_form.hasField(key))
32+
continue;
33+
g_form.setValue(key, answerMap[key]);
34+
}
35+
}
36+
//get gform from the spmodel
37+
$rootScope.$on('spModel.gForm.initialized', function (e, gFormInstance) {
38+
g_form = gFormInstance;
39+
fillAnswers();
40+
});
41+
};

0 commit comments

Comments
 (0)