$scope.server.update() / $scope.server.get() is not working

pkute
Tera Contributor

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:

$scope.$on('spModel.gForm.initialized',function(e, gFormInstance){if(c.data.isreorder=='true'){
var variables  = {};
variables= $scope.data.variables; 
for (var p=0;p<variables.length;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[p].value;
console.log('m i here pooja value '+value);
if(name != '')
{
         g_form.setValue(questioname,value);
}
}
}});
 
Server Script:
 
if (data.ordereditem){
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;
}
}
 
function getVariables(requesteditem) {
var variables;
if (serviceCatalogUtil.getVariablesForTask) {
variables = serviceCatalogUtil.getVariablesForTask(requesteditem, true);
} else {
variables = $sp.getVariablesArray();
}
return variables;
}
 
 
 
 
 
2 REPLIES 2

Sohithanjan G
Kilo Sage
Kilo Sage

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:

  1. We've introduced variableProcessed to keep track of the variables that have been processed.

  2. Inside the then callback of $scope.server.update(), we update the variable values and increment variableProcessed.

  3. 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

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

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;

 

pkute_0-1699453952302.png