How to get a parameter from the URL - Catalog Client Script



      The mission was to get a sys_id from the URL, query a record and return a value to the front-end.

      In the front-end we have a Record Producer (RP) with a String field.

      On the onLoad event, we want to populate the field in order to show the value retrieved from the back-end.

      The scenario is defined so let's go to the step by step process.


1. Create a Util class in the back-end

      Our Utils class will be a Script Include that receives a sys_id and returns a String. 


var KMXOUtils = Class.create();
KMXOUtils.prototype = {
    initialize: function() {
    },
  /*
  * Receives a um sys_id and returns a Task table field value
  *
  * @param {String} - taskId
  * @return {Object}
  */
  getTaskNumber: function(taskId)
  {
      if (taskId != "" && taskId != null && taskId != undefined) {
        var grTask = new GlideRecord('x_770214_consultor_rwd_activity');
        if (grTask.get(taskId)) {
          
          var number = grTask.getValue('number');
          
          var obj = {};
          obj["number"] = number;
          
          return obj;
        } else {
          return {};
        }
      }
  },

    type: 'KMXOUtils'
};

2. Create a class to provide access for the Front-End


      2.1) To provide access in this class, the parameter Client callable should be True (checked)


var UtilsAjax = Class.create();
UtilsAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

  getTaskNumber: function() {
    var taskId = this.getParameter('sysparm_task_id');
    gs.debug('=== Camacho UtilsAjax = Received the sys_id ' + taskId);
    return JSON.stringify(new KMXOUtils().getTaskNumber(taskId));
    
  },
    type: 'UtilsAjax'
});  

3. I'll suppose that you have a String field called task_number in your RP


      3.1) Create a Catalog Client Script (Type: OnLoad) to get the URL parameter and call the back-end class:


function onLoad() {
   
   var taskId = getParameterValue("taskid");
  
   if (taskId != "" && taskId != null && taskId != undefined) {
     console.log('=== CAMACHO Task id: ' + taskId);
     
     var gaGetTaskNumber = new GlideAjax('UtilsAjax');
     gaGetTaskNumber.addParam('sysparm_name', 'getTaskNumber');
     gaGetTaskNumber.addParam('sysparm_task_id', taskId);
     gaGetTaskNumber.getXMLAnswer(setmyFormValue);
   }
}

function setmyFormValue(answer) {
  
  //console.log('=== CAMACHO Entered setmyFormValue');
  if (answer) {
    var obj = JSON.parse(answer);
    var numero = obj.number.toString();
    console.log(numero);
    
    g_form.setValue('task_number', numero);
    
  }
}

function getParameterValue(name) {
  var url = top.location.href;
  var value = new URLSearchParams(url).get(name);
  if (value) {
    return value;
  } else {
    return null;
  }
}


Thank you.

Comentários

Postagens mais visitadas deste blog

PROJETO DIY Videogame retrô com Raspberry Pi

Dashboard do ServiceNow sem header em tela cheia

When the student is ready the teacher will appear