Set value of a variable based on query results

Harry Campbell2
Mega Guru

Hello All,

I know this will have a simple answer but I'm still learinng about querying & manipulating glide records so I cant quite get there.

I have written a query that checks if a user is an owner of a CI:

function onLoad() {

var gr = new GlideRecord('cmdb_ci');

var user = g_form.getValue('impacted_user');

gr.addQuery('owned_by', user);

gr.query();              

}      

So when the form loads, the script checks the value of the impacted_user variable and checks the cmdb_ci table to see if they are set as an owner of any CI's.

All I want to do, is set the value of the ci_owner_identified variable to yes if the user is identified as a CI owner.

Can anyone tell me how to do this?

Thanks

Harry

1 ACCEPTED SOLUTION

damianj_
Tera Contributor

Hello Harry,



I believe this is what you are looking for:


function onLoad() {  


      var gr = new GlideRecord('cmdb_ci');  


      var user = g_form.getValue('impacted_user');  


      gr.addQuery('owned_by', user);  


      gr.query(function(rec){


              if (rec.next()) {


                      g_form.setValue('ci_owner_identified','yes');


              }


      });                  


}



Nevertheless, I would highly recommend using GlideAjax instead due to performance impact of client-side GlideRecord.



More information:
http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices#Example:_Asynchronous_GlideA...


http://wiki.servicenow.com/index.php?title=Client_Side_GlideRecord



Dynamic default value might be yet another option but I do not have enough details to say if it would work in your case.



Regards,


Damian Jeszka


View solution in original post

5 REPLIES 5

Thank you for this, it works perfectly.



Will have a look at GlideAjax in the future.