Skip to content

Commit bbe7e86

Browse files
authored
Create Survey or form filler widget.js
1 parent 695d030 commit bbe7e86

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

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)