How to get value/string from URL after & symbol .

Neelavathi M
Tera Contributor

Hi All,

 

I have a requirement to populate the value from URL, actual value is(&sysparm_phase=6%20test%20&%20plan) '6 test & plan' i am able to get till '6 test' but '& paln ' is not populating it is trimming  off, so what needs to changed in client scritpt to get full value.

Quick help is appreciated.

 

11 REPLIES 11

Ahmmed Ali
Mega Sage

Hello @Neelavathi M 

 

Can you send current script you are using to get the value?

 

Thank you,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

you can refer below thread to get read URL parameters in client script: 

https://www.servicenow.com/community/csm-forum/get-url-parameters-from-client-script/m-p/389334 

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Juhi Poddar
Kilo Patron

Hello @Neelavathi M 

You can try this script:

function onLoad() {
    //Type appropriate comment here, and begin script below
    var url = top.location.href; // Current URL
    var urlParams = new URLSearchParams(url.split('?')[1]); // Extract query string
    // Decode the sysparm_phase parameter
    var sysparmPhase = urlParams.get('sysparm_phase');
    g_form.addInfoMessage('sysparmPhase' + sysparmPhase);
    if (sysparmPhase) {
        sysparmPhase = decodeURIComponent(sysparmPhase); // Decode special characters
        alert("sysparm_phase value:", sysparmPhase);

        // Populate the value into the desired field
        //g_form.setValue('your_field_name', sysparmPhase);
    }
}

  Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Hello @Neelavathi M 

In addition to my previous answer, I see that '6 test & plan' contains '&' which act as a delimiter.

When passing parameters via a URL in ServiceNow, special characters like & need to be encoded to avoid truncation or unexpected behavior. The & character is treated as a delimiter for query parameters in URLs. You need to ensure that the parameter value is properly URL-encoded and decoded to preserve its integrity.

Note: Update the url as (&sysparm_phase=6%20test%20%26%20plan). This will give the expected results as '6 test & plan'

Result:

JuhiPoddar_0-1735896058139.png

 

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar