How to create record based on some filed in Record producer

neehas
Tera Expert

Suppose there is a field on a record produces type. If type is "Incident", an Incident should be created. If type is "Problem", then a Problem should be created. How will you achieve this?

2 ACCEPTED SOLUTIONS

Abbas_5
Tera Sage
Tera Sage

Hello @neehas,

 

To create a record in ServiceNow based on a field within a Record Producer, you'll need to use scripting to conditionally populate the record's fields based on the value of that specific fieldSpecifically, you'll use the current and producer objects within a script to access and modify the new record being created. 
 
Here's a breakdown of the process:
  1. 1. Define the Field:
    Create a field (variable) in your Record Producer that will dictate the type or table of the record to be created. This could be a drop-down, a radio button, or another field that allows the user to select the desired type. 
     
  2. 2. Add a Script:
    Go to the "What it will contain" section of your record producer and add a script to the "Script" field. 
     
  3. 3. Conditional Logic:
    Within the script, use conditional statements (like if/else or switch) to check the value of the field you defined in step 1. 
     
  4. 4. Record Creation:
    Depending on the field's value, use the current object to set the appropriate values for the new record's fields. For example, if the field indicates an "Incident," you might set the table_name to "incident" and other incident-specific fields. 
     
  5. 5. Example:
     
 
// Check the value of the "Type" field (assuming it's named "type")if (producer.type == "Incident") {    current.table_name = "incident"; // Set the table for the record    // Set other Incident-specific fields as needed    current.short_description = "Generated Incident";    current.caller_id = current.user_id; // Example: Set caller to current user} else if (producer.type == "Problem") {    current.table_name = "problem";    // Set other Problem-specific fields as needed    current.short_description = "Generated Problem";    current.caller_id = current.user_id;}// ... other conditions for different record types
  1. Test: Order the record producer from the Service Catalogue to verify that the correct record type is created based on the selected field. 

If this is helpful, please hit the thumbs up button and accept the correct solution by referring to this solution in the future; it will be helpful to them.

 

Thanks & Regards,

Abbas Shaik

View solution in original post

2 REPLIES 2

Abbas_5
Tera Sage
Tera Sage

Hello @neehas,

 

To create a record in ServiceNow based on a field within a Record Producer, you'll need to use scripting to conditionally populate the record's fields based on the value of that specific fieldSpecifically, you'll use the current and producer objects within a script to access and modify the new record being created. 
 
Here's a breakdown of the process:
  1. 1. Define the Field:
    Create a field (variable) in your Record Producer that will dictate the type or table of the record to be created. This could be a drop-down, a radio button, or another field that allows the user to select the desired type. 
     
  2. 2. Add a Script:
    Go to the "What it will contain" section of your record producer and add a script to the "Script" field. 
     
  3. 3. Conditional Logic:
    Within the script, use conditional statements (like if/else or switch) to check the value of the field you defined in step 1. 
     
  4. 4. Record Creation:
    Depending on the field's value, use the current object to set the appropriate values for the new record's fields. For example, if the field indicates an "Incident," you might set the table_name to "incident" and other incident-specific fields. 
     
  5. 5. Example:
     
 
// Check the value of the "Type" field (assuming it's named "type")if (producer.type == "Incident") {    current.table_name = "incident"; // Set the table for the record    // Set other Incident-specific fields as needed    current.short_description = "Generated Incident";    current.caller_id = current.user_id; // Example: Set caller to current user} else if (producer.type == "Problem") {    current.table_name = "problem";    // Set other Problem-specific fields as needed    current.short_description = "Generated Problem";    current.caller_id = current.user_id;}// ... other conditions for different record types
  1. Test: Order the record producer from the Service Catalogue to verify that the correct record type is created based on the selected field. 

If this is helpful, please hit the thumbs up button and accept the correct solution by referring to this solution in the future; it will be helpful to them.

 

Thanks & Regards,

Abbas Shaik

Thank you @Abbas_5