Not able to Query a table successfully

sanjeev28 k
Giga Expert

Hi,

 

I am taking the request no. from a request item (sc_req_item) table based on some conditions and then querying that request no. into request table (sc_request) and then assigning a static value of a particular user to the field of requested_for'. But Its not getting into req.next() loop. and req.query is giving a "undefined " response which I checked in logs. Please Help.

 

 

var req_no= current.request;                   // getting a request no. from sc_req_item table

 

var   req = new GlideRecord('sc_request');

 

req.addQuery('sys_id', req_no);

req.query();                   // showing   as   'undefined' response in logs

if(req.next()){               // showing   as   'false' response in logs

req.requested_for= '01f3aba9141c390048f5dab33584817c';     // Assigning a static user sys_id to requested_for field

req.update();

1 ACCEPTED SOLUTION

Hi Kalai, i got my answer, and thanks for your kind support,


I just removed req.update(); from your code. and used only the below code. And now I am getting only single request.




var req= current.request.getRefRecord();


req.requested_for= '01f3aba9141c390048f5dab33584817c';



Thanks kalai and pradeep


View solution in original post

30 REPLIES 30

Hi pradeep,


I tried in   https://sandbox.service-now.com


and selected order guide Request Developer Project Equipment and taken   ritm 'Development Laptop' as a condition in business rule(xyz) and assigining requested_for to a user (xyz). But it dint worked. Plz check and provide me some suggestion


Hi Sanjeev,



Please go through the below link on how to avoid hard-coding


https://www.youtube.com/watch?v=Yvn0zqCaXr8&index=14&list=PLCOmiTb5WX3ouggdJ4ty2dVUSrki9uYr1


Jake Gillespie
Mega Guru

Hi Sanjeev,



The script you've posted should work, however it's against best practice to hard-code sys_id's into any script (e.g req.requested_for= '01f3aba9141c390048f5dab33584817c';). Check that this sys_id is a valid user record. If the req.next() loop is not being entered, it's because it's returning false, and this would mean no records were found in the query. I would suggest converting the req_no variable to a string first (e.g var req_no= current.request.toString();). Try the query again and see how you go. An alternative to using .addQuery() and .query() when you're expecting a single result, is to use .get(). See wiki. This allows you to get a specific record by using a unique key value (like sys_id).



As long as you're executing a req.update() instead of a req.insert(), this should only update the req (Request) record, and definitely not create another record. If you're finding another record is being created, it is likely being triggered as a side-effect of performing an update on the Request. In Eureka, the Request table has about 31 Business Rules running on it. The business rule "sc request events" has a reference to when the "Requested For" field value changes. When this happens, it fires an event. I'm not sure what actions this is triggering, but it could be the reason a new record is created when the "Requested For" value changes.



Regards,


Jake


Hi Jake,



I have used a unique request_no (sys_id) in the place of req_no for querying request table and it worked perfectly fine, but i forgot how I achieve that. Now I am not getting that also. can you please check on the below instance and provide me suggestion.



I tried in   https://sandbox.service-now.com


and selected order guide Request Developer Project Equipment and taken   ritm 'Development Laptop' as a condition in business rule(xyz) and assigining requested_for to a user (xyz). But it dint worked.


Hello Sanjeev,



Check the below demo link on which it is working fine.


https://demo007.service-now.com/login.do


Username and password :admin