- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 07:07 AM
Hi, I have a field named Template which has value
"short_description=Short Description for the Change^description=Longer Description^implementation_plan=Step 1 Step 2^backout_plan=Step 1 Step 2^test_plan=Step 1 Step 2^assignment_group=f956ff23878a8910b2bab996cebb3587^justification=Testing needed^risk_impact_analysis=Risk and impact analyzed^cmdb_ci=7f1a146d87541150dfd7468acebb355b^u_ci_test_lead=247f044b878a05109f887487cebb3572^u_environment_type=Production^EQ.
I want to get cmdb_ci=7f1a146d87541150dfd7468acebb355b so that I can pass into another variable.
Regards
Suman P.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 07:30 AM - edited 03-09-2023 07:37 AM
Hi @Community Alums ,
You can use regular expressions to extract the value of the cmdb_ci field from the string.
Here's an example code
var template = "short_description=Short Description for the Change^description=Longer Description^implementation_plan=Step 1 Step 2^backout_plan=Step 1 Step 2^test_plan=Step 1 Step 2^assignment_group=f956ff23878a8910b2bab996cebb3587^justification=Testing needed^risk_impact_analysis=Risk and impact analyzed^cmdb_ci=7f1a146d87541150dfd7468acebb355b^u_ci_test_lead=247f044b878a05109f887487cebb3572^u_environment_type=Production^EQ";
var regex = /cmdb_ci=([^\^]+)/;
var match = regex.exec(template);
if (match) {
var cmdb_ci = match[1];
gs.info('cmdb_ci: ' + cmdb_ci);
} else {
gs.info('cmdb_ci field not found');
}
Regards,
Shravan
Please mark as helpful and correct answer, if this helps you
Shravan
Please mark this as helpful and correct answer, if this helps you

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 06:45 PM
Hello,
What field is the onChange client script running on that would be the newValue value and that you're passing in your script include. You can't use g_form in your script include...I think you meant to use: userGR.getValue("template"); but you'd only use that after the if or while.next not before you step into the GlideRecord result(s).
Additionally, in your script include, you're using a while loop, but then using "return" in the loop, thus the script include will just return back right away. Do you actually need a while loop? Or an if (userGR.next()) { instead?
The regex would then need to go after the while or if next as well:
var regex = /cmdb_ci=([^\^]+)/;
var match = regex.exec(template_Std);
The cmdb_ci sys_id value from the template would be associated to match[1]...so you'd want to return that
return match[1];
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 07:30 AM
Hi,
Can you give a bit more information as that isn't a variable value, it's a variable/field with a value. So you're wanting to extract that entire segment: "cmdb_ci=7f1a146d87541150dfd7468acebb355b" to then be used where? Such as adding it to the query here in association with another variable/field?
Please give more information so we can help. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 06:45 PM
Hello,
What field is the onChange client script running on that would be the newValue value and that you're passing in your script include. You can't use g_form in your script include...I think you meant to use: userGR.getValue("template"); but you'd only use that after the if or while.next not before you step into the GlideRecord result(s).
Additionally, in your script include, you're using a while loop, but then using "return" in the loop, thus the script include will just return back right away. Do you actually need a while loop? Or an if (userGR.next()) { instead?
The regex would then need to go after the while or if next as well:
var regex = /cmdb_ci=([^\^]+)/;
var match = regex.exec(template_Std);
The cmdb_ci sys_id value from the template would be associated to match[1]...so you'd want to return that
return match[1];
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 06:30 AM - edited 03-10-2023 06:41 AM
Hi @Community Alums
As it seems you've used most of what I've mentioned here for my corrections and you now have like 3 threads you've made all covering the same thing, it is probably best to close this thread out.
If I've helped guide you correctly in this thread, please also consider marking it as "Accept Solution".
Thanks and take care! 🙂
I can review your other threads, but having 3 going at the same time isn't appropriate.
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2023 08:29 AM
Hi @Community Alums
I believe my other reply above helped guide you towards a solution as well, correct?
If so, please consider marking that reply as "Accept Solution".
Just so you're aware, you can mark more than 1 reply as "Accept Solution".
Thanks! 🙂
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!