Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to Split Mulitline Variable type by using Business rule

Arun91
Tera Contributor

Hi,

I have a catalog item and variable type is multiline text. I have to comma separate the values to insert in another table. How we can achieve this.

2 REPLIES 2

SAI VENKATESH
Kilo Patron
Kilo Patron

Hi @Arun91 

 

You can try the below script:

before Insert business rule and change the field name as per your requirement

 

(function executeRule(current, previous /*null when async*/ ) {
        var requestItemSysId = current.sys_id; // Replace with the actual sys_id
        var commaSeperatedValues = [];
        var multiLineText = current.variables.special_accommodations.toString();
        if (multiLineText) {
            var valuesArray = multiLineText.split(' ');
            for (j = 0; j < valuesArray.length; j++) {
                var newRecord = new GlideRecord('incident');
                newRecord.initialize();
                newRecord.caller_id = '77ad8176731313005754660c4cf6a7de';
                newRecord.short_description = valuesArray[j]; /
                newRecord.insert();
            }


        }

})(current, previous);

 

Regards,

Sai Venkatesh

Bert_c1
Kilo Patron

Seems the desire here is to split based on a new line character. Some logic for that follows:

 

// find a journal entry with multi-line text
var sjf = new GlideRecord('sys_journal_field');
sjf.addQuery('sys_id', '04554d105301121063141ab0a0490ee6');
sjf.query();
while (sjf.next()) {
	gs.info("Value: " + sjf.value);
	// replace all new lines with a comma
	var newText = sjf.value.replaceAll('\n', ',');
	gs.info('newText = ' + newText);
}
// now convert newText to an array
var newLines = newText.split(',');
for (i = 0; i < newLines.length; i++ ) {
	gs.info("line " + i + " = " + newLines[i]);
}

// now create one or more records in another table using the newLines array values.
// ...

I have no idea what table a business rule would be defined on. I see variable values for sc_req_item records, but I have no idea what table they are stored in. If the desired 'field' is a variable, I see an OOB BR defined on the sc_req_item table named "Start Workflow" that accesses the variables.