Programatically create demo data with fixed Number values from scoped app
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 04:19 PM - edited 11-07-2023 05:27 PM
My requirement is to create a set of demo data (like incidents) for my scoped app as though the records were created X number of days ago. Therefore, I am programmatically setting sys_id, Number, and dates.
I am able to set sys_id and dates, but not set the Number value unless I turn off workflows/BRs, which is only working from Global scope, not from my app scope. I'd prefer to let workflows/BRs run anyway, so my issue is not that setWorkflow fails in scope, but that I can't set the Number from within scope. Any thoughts?
Code sample:
var gr = new GlideRecord('incident');
gr.initialize();
gr.setNewGuidValue('12345678901234567890123456789012'); // WORKS
gr.autoSysFields(false); // WORKS
gr.setValue('sys_updated_on', '2020-01-01 00:00:00'); // WORKS
gr.setWorkflow(false); // ERROR IN APP SCOPE
gr.number = 'INC0011196'; // IGNORED
gr.caller_id = gs.getUserID(); // WORKS
gr.insert(); // WORKS, BUT WITH AUTO-GENERATED NUMBER 😩
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 05:34 PM
What about writing a script that creates them via the Table API?
I ran a quick test with the API Explorer including the "sysparm_suppress_auto_sys_field" parameter as true and it allows writing the sys dates and GUID with specific data.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2023 09:58 AM
Can you post your script? Please see additional details and code added to original post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2023 04:23 PM
Below is a fix script that I tested with
var myDemoRecordList = [{
"number": "MYREC000001",
"sys_id": "12345678901234567890123456789001",
"sys_updated_on": "2020-01-01 00:00:00",
"caller_id": "6816f79cc0a8016401c5a33be04be441"
},
{
"number": "MYREC000002",
"sys_id": "12345678901234567890123456789002",
"sys_updated_on": "2020-01-01 00:00:00",
"caller_id": "6816f79cc0a8016401c5a33be04be441"
},
{
"number": "MYREC000003",
"sys_id": "12345678901234567890123456789003",
"sys_updated_on": "2020-01-01 00:00:00",
"caller_id": "6816f79cc0a8016401c5a33be04be441"
}
]
myDemoRecordList.forEach(createDemoIncident)
function createDemoIncident(details) {
try {
//get the endpoint of the platform instance
var instanceURL = gs.getProperty('glide.servlet.uri', "");
//manually setup a Rest message call
var ws = new sn_ws.RESTMessageV2();
//set the endpoint appending the path for the table api for creating incidents
ws.setEndpoint(instanceURL + 'api/now/table/incident');
//set the sysautofield to false same as doing gr.autoSysFields(false)
ws.setQueryParameter('sysparm_suppress_auto_sys_field', 'true');
ws.setHttpMethod('POST');
//Set up credentials (basic auth) on this table sys_auth_profile_basic and passed sys_id of that record here
ws.setAuthenticationProfile('basic', '3c162b65976a31101f62d0900153af33');
//This API only supports single insertion hence the loop. Passing each record (detail) as the request body
ws.setRequestBody(JSON.stringify(details));
var result = ws.execute();
var status = result.getStatusCode();
var respBody = result.getBody();
if(status == 200)
gs.info(respBody)
} catch (ex) {
gs.info(JSON.stringify(ex))
}
}
Results:
You can see the Number is what I set and the Updated field see how it's before the actual opened field. The date doesn't match because the profile time isn't set but from the next image of XML you'll see the date I put and the sys_id
XML
So this should work. However, as always, I tend to over think things. This could also be done with a simple data source and transform map.
Hopefully this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 05:35 PM
For #1, seems you need to post your script. If you are running it in Scripts - Background, set the scope to that of the table you want to create records in. You may have trouble with setting value for the 'sys_' fields. But then there is this:
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0689588
on including demo data in a scoped application.