$scope.server.update() / $scope.server.get() is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2023 03:30 AM
I am trying to populate catalog item form variables based on the already created request.
In $scope.data.variables I am able to get list of variable label and value. But for variable name I am trying to use $scope.server.update() /$scope.server.get() but it is not returing anything in response.
Can see variable backend name in data.variablename from server script. Please help how can i get variable name dynamically in client script to set variable values on onload. Let me know if any questions.
Client controller:
var requesteditem = new GlideRecord('sc_req_item');
requesteditem.addQuery('sys_id',data.ordereditem);
requesteditem.query();
if(requesteditem.next()){
data.variables = getVariables(requesteditem);
}
}
if (input && input.action == 'getvariablename') {
var variablenamequery = new GlideRecord('item_option_new');
variablenamequery.addQuery('question_text',input.variablequestion);
variablenamequery.addQuery('cat_item',data.sys_id);
variablenamequery.query();
if(variablenamequery.next()){
data.variablename= variablenamequery.name;
}
}
var variables;
if (serviceCatalogUtil.getVariablesForTask) {
variables = serviceCatalogUtil.getVariablesForTask(requesteditem, true);
} else {
variables = $sp.getVariablesArray();
}
return variables;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2023 09:56 AM
Hi @pkute ,
There's an issue with the asynchronous nature of your client script. The server-side call is asynchronous, and by the time it completes, the loop may have already finished executing. Here's a modified version of your client-side script that should work more effectively:
$scope.$on('spModel.gForm.initialized', function (e, gFormInstance) {
if (c.data.isreorder == 'true') {
var variables = $scope.data.variables;
var numVariables = variables.length;
var variableProcessed = 0;
for (var p = 0; p < numVariables; p++) {
var question = variables[p].label;
$scope.data.action = 'getvariablename';
$scope.data.variablequestion = variables[p].label;
$scope.server.update().then(function (resp) {
var questioname = resp.data.variablename;
console.log('m i here pooja questioname ' + questioname);
var value = variables[variableProcessed].value;
console.log('m i here pooja value ' + value);
if (questioname !== '') {
gFormInstance.setValue(questioname, value);
}
variableProcessed++;
// If all variables are processed, you can trigger further actions here.
if (variableProcessed === numVariables) {
// Do something when all variables are processed.
}
});
}
}
});
In this modified script:
We've introduced variableProcessed to keep track of the variables that have been processed.
Inside the then callback of $scope.server.update(), we update the variable values and increment variableProcessed.
We also added a check to see if all variables have been processed. Once all variables are processed, you can add further actions as needed.
This modified script should work more effectively for updating variables based on the backend name retrieved from the server.
Mark as Accepted solution & HIT helpful if it suffice your requirement
BR, Sohith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 06:32 AM
Thank you, Sohith!
I tried above solution but getting below error
js_includes_sp_libs.jsx?v=10-06-2023_1603&lp=Fri_Oct_20_13_31_57_PDT_2023&c=8_102:142 TypeError: Cannot read properties of undefined (reading 'variablename')
This is at line var questioname = resp.data.variablename;