Script to create a new record from a business rule

Marta G
Mega Guru

Hello All

i am trying to create a business rule, onbefore, with condition

user type is student. 

I thought under actions i could have selected something like ''create a new record'' but i can't, and from reading online it seems i can only do it via scripting. 

 

Does anyone know what am i supposed to write for this to happen? 

 

The idea is, if user type is student, create a new record in the csm_consumer table and link the two

 

thanks to anyone who can help!

3 ACCEPTED SOLUTIONS

Adrian Ubeda
Mega Sage
Mega Sage

Hello @Marta G , 

Check this link where a new record it's created from business rule: https://www.servicenow.com/community/developer-forum/create-a-business-rule-to-insert-a-record-when-...

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

View solution in original post

Community Alums
Not applicable

Hi @Marta G ,

 

Please create before BR and add below script this will help you 

 

var gr = new GlideRecord('tableName'); // Give your table name here
gr.initialize();
gr.short_description = current.short_description
gr.insert();

 

 

For Example you want to create new incident when user is created so you have to create BR on user table and glideRecord on incident table 

 

var gr = new GlideRecord('incindet'); // Give your table name here
gr.initialize();
gr.short_description = "New User inserted name - " + current.first_name + current.last_name 
gr.insert();

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

View solution in original post

Robbie
Kilo Patron
Kilo Patron

Hi @Marta G,

 

Whilst others have guided you on the creation of a record via a Business Rule which is correct in concept, a best practice tip would be to avoid using the variable 'gr'. It is important to understand a "gr" defined in one business rule can clobber another "gr" defined in some other script, which happens to be running as part of the same update or transaction.

 

You should name your variables uniquely as well as wrap your scripts in a function to avoid your script clobbering another as shown below:

 

function createIncident() {
    var createIncidentGr = new GlideRecord('incident');
    createIncidentGr.initialize();
    createIncidentGr.short_description = 'Example record Creation'; // or grab an existing value such as current.field_name
    createIncidentGr.insert();
}
createIncident(); // runs the above function

 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

 

SN Developer and Best Practice article on this subject:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0713029#:~:text=When%20using%...

View solution in original post

3 REPLIES 3

Adrian Ubeda
Mega Sage
Mega Sage

Hello @Marta G , 

Check this link where a new record it's created from business rule: https://www.servicenow.com/community/developer-forum/create-a-business-rule-to-insert-a-record-when-...

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

Community Alums
Not applicable

Hi @Marta G ,

 

Please create before BR and add below script this will help you 

 

var gr = new GlideRecord('tableName'); // Give your table name here
gr.initialize();
gr.short_description = current.short_description
gr.insert();

 

 

For Example you want to create new incident when user is created so you have to create BR on user table and glideRecord on incident table 

 

var gr = new GlideRecord('incindet'); // Give your table name here
gr.initialize();
gr.short_description = "New User inserted name - " + current.first_name + current.last_name 
gr.insert();

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

Robbie
Kilo Patron
Kilo Patron

Hi @Marta G,

 

Whilst others have guided you on the creation of a record via a Business Rule which is correct in concept, a best practice tip would be to avoid using the variable 'gr'. It is important to understand a "gr" defined in one business rule can clobber another "gr" defined in some other script, which happens to be running as part of the same update or transaction.

 

You should name your variables uniquely as well as wrap your scripts in a function to avoid your script clobbering another as shown below:

 

function createIncident() {
    var createIncidentGr = new GlideRecord('incident');
    createIncidentGr.initialize();
    createIncidentGr.short_description = 'Example record Creation'; // or grab an existing value such as current.field_name
    createIncidentGr.insert();
}
createIncident(); // runs the above function

 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

 

SN Developer and Best Practice article on this subject:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0713029#:~:text=When%20using%...