Update variable value using SetValue

Eli Guttman
Tera Guru

Hi,

i'm trying to update requested item variables using script. i tried to do it using the setValue function

Seems like this option work

req.variables.first_name = 'MY NAME';

but this option does not work:

req.setValue('variables.first_name ','MY NAME);

req.update();

Anyone know the reason or how i should use the setValue function for variables?

12 REPLIES 12

Hi Eli,



Add some gs.log statements and check the log to see what you are getting.


Also, if the script is on server side, what kind of script is it? When does it run? (the gs.log can help us determine whether it is running at all)



harel


Hi,



the problem is with the " req.setValue(var_name,value); " statment itself.



do you mean like this gs.log(req.setValue(var_name,value)) ?



i basically running if from REST API script, but i took it out to fix script, just to test this issue.


In order to try and see if anything is actually running, I'd add a few statements to facilitate finding it in the log.



function updateVar (number,vaiable_name,value){


gs.log('Eli, running');


var var_name   = 'variables.' + vaiable_name;


var req = new GlideRecord('sc_req_item);


req.addQuery('number',number);


req.query();


if (req.next()){


req.setValue(var_name,value);


gs.log('Eli, var_name ' + var_name + ' value ' + value);


req.update();


}


}



It will allow you to see whether it is running and the values you are trying to populate.


I am also not sure about he var_name. Could be


var var_name = 'current.variables.' + vaiable_name;


Hopefully you are passing the functions' values somewhere.



harel


In JavaScript, you can access a property using the object.property syntax, OR object['property'] syntax.



Because the setValue method doesn't work with variables (apparently), then you should try the object['property'] syntax.



function updateVar (number,variable_name,value){


        gs.log('Eli, running');


        // var var_name   = 'variables.' + variable_name;


        var req = new GlideRecord('sc_req_item);


        req.addQuery('number',number);


        req.query();


       


        if (req.next()){


                  //req.setValue(var_name,value);


                  req.variables[variable_name] = value; // NEW


                  gs.log('Eli, variable_name ' + variable_name + ' value ' + value);


                  req.update();


        }


}


Lakshmaiah Dump
Giga Expert

While order the request we have cart and we can set the variables to cart and we performing action on the cart but not request item table. (Cart->REQ->RITM->TASK)


var itemId = getItem(request_item);


var cartId = GlideGuid.generate(null);


var cart = new Cart(cartId);


var req_item = cart.addItem(itemId);




for(var key in variables){


      cart.setVariable(req_item, variable_name, variable_value);


}


var rc = cart.placeOrder();i








But while updating the something on the request item (sc_req_item) we have to user the reqObject.Variables.VariableName = Variable_value (RITM)


var requestedItem = new GlideRecord('sc_req_item');


requestedItem.addQuery('number', ritmNumber);


requestedItem.query();


while(requestedItem.next()){


requestedItem.variables.short_description = short_description;


}


requestedItem.update();





Hit Like/Helpful as when felt


@Lakshmaiah