The CreatorCon Call for Content is officially open! Get started here.

Auto populate a text field

servicenow14710
Tera Expert

Hello developers, Can anyone share the logic,  i need to auto populate a text field once if any user registers/submit an order(catalog form) for the first time. If another user registers then the auto populated number should be incremented by 1. Any help is appreciated . Thanks!

7 REPLIES 7

Jaspal Singh
Mega Patron
Mega Patron

Hi,

Is there any particular format or prefix (INC00.., PRB00...) or its merely numbers (1,2,3,etc.) without prefix. Also, since it is for catalog, it is for particular item or for all items?

Hi @Jaspal Singh , .Thanks for your response!   yes like PR001,PR002 etc., it is for particluar catalog item.

sumanta pal
Kilo Guru

Sure, you can achieve this by creating a business rule in ServiceNow. Here's a step-by-step guide:

1. Navigate to System Definition > Business Rules in ServiceNow.
2. Click on New to create a new business rule.
3. Give your business rule a name, for example, "Auto Increment Order Number".
4. Select the table you want this business rule to apply to. In your case, it would be the table that stores the catalog form submissions.
5. Set the "When to run" settings as follows:
- When: before
- Insert: true
6. In the "Advanced" tab, write a script to auto increment the order number. Here's a sample script:

javascript
(function executeRule(current, previous /*null when async*/) {
// Query the database to get the last order number
var gr = new GlideRecord(current.getTableName());
gr.orderByDesc('order_number');
gr.setLimit(1);
gr.query();

if (gr.next()) {
// If there's a previous order, increment the order number by 1
current.order_number = parseInt(gr.order_number, 10) + 1;
} else {
// If there's no previous order, set the order number to 1
current.order_number = 1;
}
})(current, previous);


7. Click on Submit to save the business rule.

Please note that you need to replace 'order_number' with the actual field name in your table that you want to auto increment. Also, make sure that the field type of your order number field is integer.

For asking ServiceNow-related questions try this :

For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.

Please visit : https://nowkb.com/home
Our Website :https://nowkb.com/home
Link - https://nowgpt.ai/

Hi @sumanta pal : Thanks for your response , i just needed an auto populated value of field which increments when another user registers.