how to get owner name from CMDB table

khaja
Giga Contributor

Hi,

i have a requirement, by using variable value i have check the condition in cmdb_ci_service table after that i will get owner of service.but it won't working any help me

when i check the same script in background script ti is working at that time i have checked by using service name i am get owner name when i check by using variable value i won't worked

var gr=current.variables.test

alert(gr);// i got variable value in alert

var app=new GlideRecord("cmdb_ci_service");

app.addQuery("name", gr);

app.query();

if(gr.next());

{

var gr=app.u_owner;

}

above variable is list-collector. any help me.

9 REPLIES 9

vinothkumar
Tera Guru

Hi Shaik,



You are trying to use the same gr variables twice(var gr=current.variables.test && var gr=app.u_owner;) . That might also be the reason.



Also alert will not work in Server side. Hope you are using a server side check, if so try to put log in the query and check what value you are receiving, hopefully it will work.



var gr=current.variables.test


alert(gr);// i got variable value in alert



var app=new GlideRecord("cmdb_ci_service");


app.addQuery("name", gr);


app.query();


if(gr.next());


{


var gr1=app.u_owner;


gs.log(gr1);



}


Robbie
Kilo Patron

Hi Shaik,


I see a few issues here. Are you trying to make this a client script? I'll point out the code issues below which is why your code isn't quite running as you expect, but to be honest I think we need to step back and ask a few process and logical questions as even if we fix the code, I'm not sure we will resolve the overall requirement.



On your form, should a user be able to select more than one "Business Service" (cmdb_ci_service)? Is that why you're using a list collector? If so, how do you intend to handle when multiple Business Services are selected which have different owners? How do you wish or need to display the Business Service Owner? If we're only dealing with one, its very easy, more than one and we have a few considerations from a catalog perspective. Please advise.



Hopefully, we're only requiring one? If only one business service service can be selected, I'd suggest using a reference field pointing at the correct table and simply using a getReference call to resolve this and set the value


(See this helpful wiki link - http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#getReference )



Here are your code issues if your trying to use this as a client script, but even if you correct these, the code wont work to resolve the requirement:



var gr=current.variables.test //you cant use current in a client script


alert(gr);// i got variable value in alert



var app=new GlideRecord("cmdb_ci_service");


app.addQuery("name", gr); //The list will return a sys_id, not the name. Change the quesry to reflect the "sys_id"


app.query();


if(gr.next()); //you need to be checking against 'app', not 'gr'


{


var gr=app.u_owner; //you have 2 references of var gr. Remove the 'var' on this line. Also, the column name you require for owned by the the cmdb_ci_service table is 'owned_by' not 'u_owner'


}



Please mark helpful and correct by clicking on your post link (how to get owner name from CMDB table ) to help close out open questions and help others with similar questions.



Thanks,


Robbie


khaja
Giga Contributor

thanks for your reply



i am using this code in server side



var gr=current.variables.test


gs.addInfoMeassge(gr);// i got variable value in alert



var app=new GlideRecord("cmdb_ci_service");


app.addQuery("name", gr);


app.query();


if(app.next());


{


var gr1 =app.u_owner; // it is custom filed


gs.addInfoMeassge(gr1);


}


Hi,



What logs you find for current.variable.test?



If it is a list collector it will provide the sys_id to you.



var gr=current.variables.test.toString();



gs.addInfoMeassge(gr);// i got variable value in alert



var app=new GlideRecord("cmdb_ci_service");


app.addQuery("sys_id", gr);


app.query();


if(app.next());


{


var gr1 =app.u_owner; // it is custom filed


gs.addInfoMeassge(gr1);


}



Instead of name use sys_id to get the value.




Thanks,


Jagarnath