Appending text to the name field in a business rule

snow_beginner
Mega Guru

Hi,

I have a requirement to create a business rule on the cmdb_ci_server table. So when a new record is created or updated with certain conditions, a records needs to be created in another table called FNMS Agent table. However, the name needs to have "FNMS Agent on" appended to the Name value in the FNMS table. I have created the below below business rule, is it ok? or is there a different way to concatenate strings in business rules?

 

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

    // Add your code here
    var grFnms = new GlideRecord('u_cmdb_ci_fnms_agent');
    grFnms.addQuery('name', current.name);
    grFnms.query();
    if (!grFnms.next()) {
        grFnms.initialize();
        grFnms.name="FNMS Agent on "+current.name;
        grFnms.host_name=current.name;
        grFnms.u_hosted_on=current.sys_id;
        grFnms.install_status=20;
        grFnms.support_group=gs.getProperty('lbg.it.discovery.support_group.sysid');
        grFnms.insert();
    }

})(current, previous);
 
This is an After rule (insert and update)
2 ACCEPTED SOLUTIONS

siva krishna M2
Tera Guru

Hello @snow_beginner ,

 

Yes the concatenation is correct as name field is string if the field is not a string then we can convert the field to string using .toString()

grFnms.name="FNMS Agent on "+current.name.toString();

 

View solution in original post

Adrian Ubeda
Mega Sage
Mega Sage

Hello snow_beginner,

I think your code will work, using '+' it's the easiest way to concatenate strings. In addition, instead using '=' for setting values I would use gr.setValue() function. Here's a short example:

grFnms.setValue('name', "FNMS Agent on "+current.name);
grFnms.setValue('host_name', current.name);

Using this, you can create a for structure for iterating and then store key values in arrays for having a cleaner and structured code. But as I have mentioned, your code should work 😃

 

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

View solution in original post

3 REPLIES 3

siva krishna M2
Tera Guru

Hello @snow_beginner ,

 

Yes the concatenation is correct as name field is string if the field is not a string then we can convert the field to string using .toString()

grFnms.name="FNMS Agent on "+current.name.toString();

 

Adrian Ubeda
Mega Sage
Mega Sage

Hello snow_beginner,

I think your code will work, using '+' it's the easiest way to concatenate strings. In addition, instead using '=' for setting values I would use gr.setValue() function. Here's a short example:

grFnms.setValue('name', "FNMS Agent on "+current.name);
grFnms.setValue('host_name', current.name);

Using this, you can create a for structure for iterating and then store key values in arrays for having a cleaner and structured code. But as I have mentioned, your code should work 😃

 

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

SunilKumar_P
Giga Sage

Hi @snow_beginner, concatenation, (+) operator  is correct in your script.

 

Regards,

Sunil