get Sys_ID from Service portal URL

yogeshpoyyara
Mega Expert

I am trying to get the Sys_Id from the URL and then fetch the related records from a table.

I tried, data.sys_id = $sp.getParameter("sys_id"); in the server script ; but it doesn't fetch the sys_id from the URL.

Please assist.

Regards,

Yogesh PH

1 ACCEPTED SOLUTION

Yogesh,



You want to use addQuery, as opposed to addEncodedQuery when looking up just the sys_id.



If you log ' $sp.getParameter("sys_id");', what does it return?




Thanks.


View solution in original post

10 REPLIES 10

Hi Yogesh,



The syntax for addQuery is rec.addQuery('<field_name>', '<operator>', '<value>');. The operator is optional and will default to equals if not present.



You are missing the field name in your query.



Also, it is Best Practice to use an if instead of a while if you only expect one record to be return. This will stop a potential bad query from running on multiple records, which is especially helpful when using a delete function.



Read more about addQuery, and GlideRecord syntax on the Wiki at http://wiki.servicenow.com/index.php?title=GlideRecord#addQuery




Thanks.


HI paulw(acorio),



the rec.deleteRecord(); function doesn't do anything in client script. I tried calling GlideAjax to delete but still no go. Do you know if we can run delete and update functions from service portal?


Yes, those functions work but only in the Server Script section.


How can i call a server function from client?


chiragbagdai
Mega Expert

Hi Yogesha,



You can use   client controller to get URL parameters and send that to server side (submit request), please find below example



//Client controller


function ($rootScope, $scope, snRecordWatcher, spUtil, $location, $uibModal, cabrillo, $timeout,$window) {


  var c = this;


  c.data.table = $location.search().table;


  c.data.rec_sys_id = $location.search().sys_id;


 


  c.server.get( {action_name : 'validate', table: c.data.table, rec_sys_id : c.data.rec_sys_id } ).then(function(r) {



            c.data.btn_visible = r.data.btn_visible; // r.data.btn_visible retrieve data from server controller



  });


}



// Server controller


(function() {


 


  if (input) {


            if(input.action_name == 'validate') {


                  gs.log(input.table);                          


                  gs.log(input.rec_sys_id);


                  //validation


                  data.btn_visible = true;


            }


      }


});





Regards,


Chirag



@Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.