Here's how to use a Create Record Data Resource in scripts in UIB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2025 12:43 PM
First you need to add the resource.
- Click the +
- Choose Data Resource
- Click Create Record
- Click Add.
Under Data Resources you now have a "Create Record 1". Congrats.
But you want to use it in a Client Script, because scripting always feels better.
Here's the code:
api.data.create_record_1.execute({
table: 'INSERT_TABLE_NAME_HERE',
templateFields: 'FIELD_NAME=VALUE^FIELD_NAME_2=VALUE_2',
useSetDisplayValue: null,
});
// NOTE: the ^ after VALUE. This allows you to chain field names and values together to set all the fields you want to set on your new record.
BUT WAIT THERE'S MORE
Because you're a savvy ServiceNow Developer, you want to make sure you get the record details for the new record you created.
- Go to your Create Record 1 Data Resource and click it.
- On the modal that opens, find "Events" and click it.
- Click the "+ Add event mapping" button
- Choose "Operation Succeeded"
- Click the "+ Add event handler"
This is where it will get a bit tricky
The information you want is from the event. In the below example, I'm just setting a client state param to the sys_id of the newly created record. Obviously, you can do whatever you want with it. OR you could use the console.log statement under the long dot walk to see all the other available data returned from this operation.
function evaluateEvent({api, event}) {
const new_user_id = event.payload.data.output.data.GlideRecord_Mutation.insert_sys_user.sys_id.value;
//console.log(JSON.stringify(event.payload.data, null, 4))
return {
propName: 'new_user_id',
value: new_user_id,
};
}
// Things to note about the above code
// After GlideRecord_Mutation is "insert_YOUR_TABLE_NAME"
// If you are having a hard time getting the dot walk to work, use the commented out code under "const new_user_id" and find the dot walk path in your dev console, in browser.
- 1,739 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2025 08:48 PM
Hi @Paul Sisson ,
- Data Resources: You correctly highlighted the ease of creating Data Resources for server-side actions like record creation.
- Client Script Integration: Using api.data.create_record_1.execute() within a Client Script allows for seamless interaction with the Data Resource.
- templateFields: The templateFields parameter is crucial for setting field values during record creation. The ^ separator is correctly explained for chaining multiple field assignments.
- Event Handling (Operation Succeeded): You've accurately described how to capture the newly created record's details using the "Operation Succeeded" event.
- event.payload.data.output.data.GlideRecord_Mutation: This is the core dot-walk to access the returned data. You've correctly pointed out the importance of replacing insert_sys_user with the appropriate table name.
- Debugging with console.log: The advice to use console.log(JSON.stringify(event.payload.data, null, 4)) for debugging and exploring the event payload is excellent.
- Client State Parameter: Using client state parameters to store the returned sys_id demonstrates a practical application of the event data.
Enhancements and Considerations:
- Error Handling: While you've covered the "Operation Succeeded" event, it's essential to also handle the "Operation Failed" event. This allows you to provide user feedback or take corrective actions in case of errors.
- Within the event mapping, create an event handler for "Operation Failed".
- Within the evaluateEvent function for the failed event, you can use the event.payload.error object to get error messages and codes.
- Asynchronous Operations: Remember that api.data.create_record_1.execute() is asynchronous. If you need to perform subsequent actions based on the created record, ensure that those actions are executed after the event handler has processed the result.
- Data Type Considerations: When setting templateFields, be mindful of data types. For example, reference fields require sys_id values, and date/time fields need to be in the correct format.
- Security: When using Data Resources and Client Scripts, always consider security implications. Ensure that users have the necessary permissions to create records in the specified table.
- Use of templateFields vs setting fields after the creation: In some situations, it may be better to create the record with minimal data, and then use a seperate glide record update to fill in the rest of the data. This can be helpful when dealing with complex logic, or when needing to run server side business rules against the record before all fields are set.
- User Feedback: Consider providing user feedback during and after the record creation process. Use g_form.addInfoMessage() or g_form.addErrorMessage() to communicate with the user.
- Variable naming: While the provided code is functional, using more descriptive variable names (e.g. newRecordSysId instead of new_user_id) enhances code readability.
- Testing: Thoroughly test your Client Script and Data Resource in various scenarios to ensure that it behaves as expected.
Example of Error Handling:
// In the "Operation Failed" event handler: function evaluateFailedEvent({api, event}) { console.error("Record creation failed:", event.payload.error); g_form.addErrorMessage("Failed to create record. Please check the console for details."); return null; // Or return an object if needed }
By incorporating these enhancements, you can create more robust and user-friendly ServiceNow solutions using Data Resources and Client Scripts.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 06:36 AM
g_form isn't accessible in UIB scripts. If it is, I'd love to know how to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2025 01:10 PM
for UIB client scripts they moved it all to
api.data.record.<Methods are Here>
Its documented here in what I find a confusing way.
https://www.servicenow.com/docs/bundle/xanadu-api-reference/page/app-store/dev_portal/API_reference/...
Here is an example from a Select this search result button I built that sets values on the form.
function handler({
api,
event,
helpers,
imports
}) {
var fields = {
"member_id": "MEMBER_ID",
"member_first_name": "MEMBER_FIRST_NAME",
"member_last_name": "MEMBER_LAST_NAME",
"member_date_of_birth": "DATE_OF_BIRTH",
"dob": "DATE_OF_BIRTH",
"member_address_1": "ADDR_LINE1",
"member_address_2": "ADDR_LINE2",
"member_city": "CITY",
"member_state": "STATE_SYS_ID",
"member_zip": "ZIPCODE"
};
for (var field in fields) {
api.data.record.setFieldValue({
fieldName: field,
value: event.context.item.value[fields[field]]
});
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 08:47 PM
Be honest... did you run my post through ChatGPT and then copy and paste the response? This looks EXACTLY like a ChatGPT post. Especially the "Enhancements and Considerations" part.
No hate if that's what you did, but be warned brother, ChatGPT does NOT know ServiceNow as well as it thinks it does. I've used it many times for help with ServiceNow and it has had at least one or two flaws in it's assessment every time.