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

Deepa Srivastav
Kilo Sage

You can add this condition.


While(gr.next())


{gr.ci_owner_identified='Yes';


gr.update();


}



Using Glide record is not recommended in client side, instead use GlideAjax .



http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0



Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.


Greg42
Mega Guru

Hi Harry,



Just to note the way you're using GlideRecord will work synchronously - which is sometimes useful when needed.


If you need to use data from the server in the client on form load why not use g_scratchpad, later you will probably need GlideAjax if you want some information accessed dynamically.



Have a read:


https://community.servicenow.com/thread/222774


Tip: Server Side Functions and Client Scripts




Cheers



Greg


harishdasari
Tera Guru

Hi Harry,



here in this scenario you can use Reference qualifier instead of client script.



http://wiki.servicenow.com/index.php?title=Reference_Qualifiers#gsc.tab=0



Using this business rule you can also achieve this,



Business rule



var gr = new GlideRecord('cmdb_ci');


gr.addQuery('cmdb_ci', gs.getUserID());


gr.query();


if(gr.next())


{


gr.cmdb_ci = current.cmdb_ci;


gr.owned_by = current.owned_by;


var user = gs.getUserName();


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


gs.addInfoMessage('current user is owner of this affetected ci' + user);


}


else


{


gs.addInfoMessage('user is not owner');


}




Thanks


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