Get Sys_Id from URL
						
					
					
				
			
		
	
			
	
	
	
	
	
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
12-04-2024 05:20 AM
Hi,
I'm getting the worknotes from the ScTask. It is returning like
"04/12/2024 11:17:00 - Name (Work notes)
Link to SLA Definition in Dev:
https://dev.service-now.com/contract_sla_list.do?sysparm_query=sys_idIN8f559dcd85421e5c0b8a85980cbb3..."
Now, I want to get the Sys_Id from the URL in Server side script.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
12-04-2024 09:41 AM
Hi @Sriker ,
I have created a Br to capture the sys_id that has come in the URL present in the work_notes. Please check the script below:
(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here
    var journalGR = new GlideRecord("sys_journal_field");
    journalGR.addQuery("element_id", current.sys_id); // Match the sys_id of the record
    journalGR.addQuery("element", "work_notes"); // Ensure it's the work_notes field
    journalGR.orderByDesc("sys_created_on"); // Order by latest entries
    journalGR.query();
    if (journalGR.next()) {
        //gs.info("Work Note: " + journalGR.value.toString());
        var k = journalGR.value.toString();
        var urlRegex = /sysparm_query=sys_idIN([\w\d]+)/;
        var match = k.match(urlRegex);
        if (match && match[1]) {
            gs.info("Extracted sys_id: " + match[1]);
        } else {
            gs.info("No sys_id found in work notes.");
        }
    }
})(current, previous);
The conditions for this Br to run is "After" "update" and worknotes changes.
I have tried it with the worknotes that you gave and it gave me the correct value of the sys_id in the logs.
Please mark this answer as "Helpful" and "Correct" if it helped you in anyway
Thanks and Regards,
K. Sai Charan
Sr. ServiceNow Developer
Deloitte India
