How to search if there is an existing request to a specific catalog item with a specific user?

ariesmanlangit
Kilo Expert

Hi,

        I am a beginner developer and I would like to ask if there is a way on how I will find out if a user has requested a certain catalog item in the requested items table. Here's my code:

//glide to request table using the sys_id of the user

var detectReq = new GlideRecord('sc_request');

detectReq.addQuery('requested_for', user);

detectReq.query();

//detect if the user has request in the sc_request table

if(detectReq.hasNext()){

//This part is not I'm sure if working. I used loop and scan each retrieved requests from sc_request if there are matching requests

// on the sc_req_item table.

        while(detectReq.next()){

                  var detectReqItem = new GlideRecord('sc_req_item');

//filter using the sys_id of the catalog item and the sys_id of the request from the glide above

                  detectReqItem.addQuery('cat_item', catalogSysID);

                  detectReqItem.addQuery('request',detectReq.sys_id);

                  detectReqItem.query();

//if there are matching requests which has the catalog item, the result will be true.

                  if(detectReqItem.hasNext()){

                            errorMessage = "Previous Request Detected. You cannot order this service again.";

                            result='true';

                  }

                  else

                            result='false';

        }

}

I tried to use addJoinQuery() but I'm not familiar with the method. Please enlight me. I appreciate your help.

Thank you very much,

Ariestotle

2 REPLIES 2

ariesmanlangit
Kilo Expert

Hi,



        I studied on how joinquery works, used it and found the answer. here's the code for reference of the others:



var detectReq = new GlideRecord('sc_request');


var detectReqItem = detectReq.addJoinQuery('sc_req_item');


detectReq.addQuery('requested_for',user);


detectReqItem.addCondition('cat_item',catalogSysID);


detectReq.query();


        if(detectReq.hasNext()){


                  errorMessage="Previous request detected. You cannot order this request again";


                  result='true';


        }


        else


                  result='false';



Thank you


Why not just search the sc_req_item table like so?



var sc_req_item = new GlideRecord('sc_req_item');


sc_req_item.addQuery('request.requested_for', user);


sc_req_item.addQuery('cat_item', item);


sc_req_item.query();


if(sc_req_item.hasNext()){


  errorMsg = "Previous Request Detected. You cannot order this service again.";


result = true;


} else {


result = false;


}