How to change variable value if URL GET parameters changes

MalikB
Tera Contributor

If a id_logiciel parameter is provided in GET in the URL, if a logiciel matches this model number, it must be pre-selected, in the variable logiciel/extension/application( mandatory, changeable, type lookup set box from table logiciel and get value field ID & class

3 REPLIES 3

Mani A
Tera Guru

write onLoad client script

 

 


function getUrlParameter(name) {
var regex = new RegExp('[?&]' + name + '=([^&]*)');
var results = regex.exec(window.location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

// Get the id_logiciel parameter from the URL
var idLogiciel = getUrlParameter('id_logiciel');

if (idLogiciel) {
// Create a GlideAjax object to call a Script Include
var ga = new GlideAjax('GetLogicielRecord');
ga.addParam('sys_id', idLogiciel);
ga.getXMLAnswer(function(response) {
var record = JSON.parse(response);
if (record && record.sys_id) {
// Set the lookup field value
g_form.setValue('logiciel', record.sys_id);
}
});
}

SCRIPT INCLUDE SAMPLE:

 

var GetLogicielRecord = Class.create();
GetLogicielRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRecord: function() {
var sysId = this.getParameter('sys_id');
var gr = new GlideRecord('logiciel');
if (gr.get(sysId)) {
// Return the record in JSON format
return JSON.stringify({
sys_id: gr.sys_id.toString(),
// Include other fields as needed
});
}
return JSON.stringify({});
}
});

MalikB
Tera Contributor

Hi @Mani A thanks for you're answer I scripted the client script part like this to get the url params id_logiciel : 

function onLoad() {

//Type appropriate comment here, and begin script below

var url = top.location.href;

var value = new URLSearchParams(url).get('id_logiciel');

if (value) {

g_form.setValue('var_application_logiciel_extention_list_2', value);

}

}

And in the script includes I did this but im not sure if its correct : 

var LogicielHelper = Class.create();
LogicielHelper.prototype = {
initialize: function() {},

getTitleFromLogicielId: function(id_logiciel) {
var logicielGr = new GlideRecord('u_logiciel_application'); // Remplacez par le nom de votre table
if (logicielGr.get(id_logiciel)) {
return logicielGr.u_title;
}
return '';
},

type: 'LogicielHelper'
};

but you are requirement is get URL data and check it in different table , if matches then prefill it..

 

so don't setValue directly in variable directly ..call script include and do validation in your lookup tbl and return value to client side and set it.. please check my sample code and replace it with correct table, field names

 

please mark my answer as correct/helpful if you are ok with it