Extract part of the variable value information so it can be used in other variable

Community Alums
Not applicable

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.

2 ACCEPTED SOLUTIONS

Sai Shravan
Mega Sage

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');
}

 

 

SaiShravan_0-1678375796361.png

 

Regards,

Shravan 

Please mark as helpful and correct answer, if this helps you 

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

View solution in original post

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!

View solution in original post

6 REPLIES 6

Sai Shravan
Mega Sage

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');
}

 

 

SaiShravan_0-1678375796361.png

 

Regards,

Shravan 

Please mark as helpful and correct answer, if this helps you 

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Community Alums
Not applicable

Hi @Sai Shravan and All,

I am trying to put the value into another variable using onChange client script. Kindly help me in correcting it.

 

Glide Ajax

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax("onChangeStChCIClass");
    ga.addParam("sysparm_name", "onChangeStChCIFunction");
    ga.addParam("sysid_key", newValue);
    ga.getXML(callBackFunction);

    function callBackFunction(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue("configuration_item", answer);

    }

}

 

 

Script Include

 

 

var onChangeStChCIClass = Class.create();
onChangeStChCIClass.prototype = Object.extendsObject(AbstractAjaxProcessor, {

            onChangeStChCIFunction: function() {
                var keyClientScript = this.getParameter("sysid_key");
                var userGR = new GlideRecord("std_change_template");
                userGR.addQuery("sys_id", keyClientScript);
                userGR.query();
                var template_Std = g_form.getValue("template");
                //template looks like this
                //                 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 = 7 f1a146d87541150dfd7468acebb355b ^ u_ci_test_lead = 247 f044b878a05109f887487cebb3572 ^ u_environment_type = Production ^ EQ
                var regex = /cmdb_ci=([^\^]+)/;
                var match = regex.exec(template_Std); //getting the value of cmdb_ci
                while (userGR.next()) {
                    var getVal = userGR.getValue(match);
                    return getVal;
				}
                },

                type: 'onChangeStChCIClass'
            });

 

 

Regards

Suman P.