Getting values from URL not working in client script

MaharshiC
Tera Contributor

Hi,

 I have created a Related list action and I am using client side scripting in that and I want to have the sys_id from the URL - /x/iem/tqa-workspace/record/x_iem_tqa_work_request/cf0f63f31b6f96103df95314604bcbb9/params/selected-... . From the given URL I want to dynamically get the work request sys_id and for that I am writing the given script but the button is not working.

 

 

function onClick() {

var a = window.location.href;
var str = a.split('x_iem_tqa_work_request/');

    var index1 = url.indexOf("x_iem_tqa_work_request/");
    index1 = index1 + 23;
    //var index2 = url.indexOf("/params");
    var substr = url.substr(index1, 32);	
var num = g_form.getValue('work_request_number');
var instanceName = g_form.getValue('instance_name');	
var url='https://emcitizendev.service-now.com/x/iem/tqa-workspace/record/x_iem_tqa_work_request/'+substr+'/params/selected-tab-index/1/sub/record/x_iem_tqa_work_request_items/-1_uid_5/params/query/work_request_number%3D'+substr+'/extra-params/query%2Fwork_request_number%3Dcf0f63f31b6f96103df95314604bcbb9%2FparentTable%2Fx_iem_tqa_work_request%2FparentRecordSysId%2Fcf0f63f31b6f96103df95314604bcbb9';
var win = top.window.open(url, '_self');
	win.focus();
 }

 

 

 

MaharshiC_0-1739899240276.png

 

 

 

2 REPLIES 2

maheshkhatal
Mega Sage

Hello @MaharshiC , you have misplaced the variable 'a' and 'url' you overlooked I guess. Also for sysId calculation you can use the regEx, see the changes in the script given below:

function onClick() {
    // Get the current URL
    var currentUrl = window.location.href;

    // Extract the sys_id from the URL
    var sysId = extractSysIdFromUrl(currentUrl);

    if (sysId) {
        // Construct the new URL dynamically
        var newUrl = 'https://emcitizendev.service-now.com/x/iem/tqa-workspace/record/x_iem_tqa_work_request/' + sysId + '/params/selected-tab-index/1/sub/record/x_iem_tqa_work_request_items/-1_uid_5/params/query/work_request_number%3D' + sysId + '/extra-params/query%2Fwork_request_number%3D' + sysId + '%2FparentTable%2Fx_iem_tqa_work_request%2FparentRecordSysId%2F' + sysId;

        // Open the new URL in the same window
        window.location.href = newUrl;
    } else {
        alert('Unable to extract sys_id from the URL.');
    }
}

// Helper function to extract sys_id from the URL
function extractSysIdFromUrl(url) {
    var sysId = '';
    var startIndex = url.indexOf('x_iem_tqa_work_request/');

    if (startIndex !== -1) {
        startIndex += 'x_iem_tqa_work_request/'.length;
        var endIndex = url.indexOf('/', startIndex);

        if (endIndex !== -1) {
            sysId = url.substring(startIndex, endIndex);
        } else {
            sysId = url.substring(startIndex);
        }
    }

    // Validate the sys_id (32-character alphanumeric string)
    if (sysId.length === 32 && /^[a-f0-9]{32}$/i.test(sysId)) {
        return sysId;
    } else {
        return null;
    }
}

 

My response helped you get better insights please mark my response as helpful.

Thank you,

Mahesh.

Hi @maheshkhatal ,

 

Thanks for you reply.

For some reason even this code is not running it is only returning value for the first alert but whenever i try to get the top url it doesnot return anything. The second and third alert is not showing. I added your code but that didnot work so i just tried to get the top URL that is also not working

 

function onClick() {
alert('This is a simple alert message!');
var a = window.location.href;
alert('newThis is a simple alert message!');
alert('abc'+a);