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.

replace string

User370547
Giga Contributor

Hello

I'm still new to service now.   I would like to replace blanks on attachment. 

I have created a business rule for insert into sys_attachment.

###############################################################################

(function executeRule(current, previous /*null when async*/) {

    var v_file_name = current.file_name;
    
    v_file_name.replace(/\s+/g, '_');
    current.file_name = v_file_name + 'xxxx';

})(current, previous);

###############################################################################

Result:   The filename is extended by "xxxx" (was just for debugging)   but the blanks are still there. 

############################  Script Background ###################################

var v_file_name = 'file name with blanks';
gs.log(v_file_name.replace(/\s+/g, '_'));

###############################################################################

Here I get the expected result. 

Sincerely Detlef

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

Try this code.

(function executeRule(current, previous /*null when async*/) {
    var v_file_name = current.file_name;
    v_file_name = v_file_name.replaceAll(" ","_");
    current.file_name = v_file_name + 'xxxx';
})(current, previous);

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

View solution in original post

3 REPLIES 3

Adrian Ubeda
Mega Sage

Hi, 

Try this: v_file_name.replace(/\s/g, '');

 

If it was helpful, please give positive feedback.

Thanks, 

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

asifnoor
Kilo Patron

Hi,

Try this code.

(function executeRule(current, previous /*null when async*/) {
    var v_file_name = current.file_name;
    v_file_name = v_file_name.replaceAll(" ","_");
    current.file_name = v_file_name + 'xxxx';
})(current, previous);

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

User370547
Giga Contributor

Hello

I found the solution:   escape the escape character !

 

(function executeRule(current, previous /*null when async*/) {

    var v_file_name = current.file_name;
    
    var v_r = new SNC.Regex('/\\\s+/');
    var v_replaced = v_r.replaceAll(v_file_name, '_');
    current.file_name = v_replaced;

})(current, previous);