MB26
ServiceNow Employee
Options
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
‎04-25-2011
11:24 AM
I was asked recently to create a new app that would manage the shipment of hardware. One of the requirements was to allow the user to Create AND Update the record via a record producer. Normally record producers, as the name suggests, creates records. However I needed to figure out a smooth way to update an existing record, in particular ONLY add or remove pieces of hardware on the existing record from within the record producer.
When it comes down to it, the real magic to make this process work is contained in two lines of code.
//This will prevent the record producer from creating a new record.
current.setAbortAction(true);
//This will redirect you to the record specified.
//Need to add the record table, and the record sys_id.
producer.redirect = 'u_recordtable.do?sys_id=' + record.sys_id;
These two lines of code will need to be incorporated in a script contained in the "Script" field of the record producer.
Building Out My Example:
•In the record producer you will need some type of field called "Existing Record" (existing_record). Or something of the sort to contain Yes/No.
•Selecting "YES", by UI Policy, will display a reference field where the user could select an existing record on the referenced table.
•In my case I specifically wanted the user to ONLY modify the CIs associated with the record. So I created a "List Collector" in my record producer.
•I then used a Catalog Client script to populate all additional variables with the data from the existing record when the existing_record was "Yes". Here is an example of that onChange Client script.
function onChange(control, oldValue, newValue, isLoading) {
//Yes-No variable drop down.
var existing = g_form.getValue('existing_record');
if (existing == 'Yes'){
// variable field to the referenced record.
var rec = g_form.getReference('ref_variable_field_name');
g_form.setValue('producer-field', rec.somedata);
//How I populated the right side of the List Collector from the existing record.
var configitems = gel('configuration_items_select_1');
$('configuration_items_select_1').options.remove(0);
var gr = new GlideRecord("some_ci_table");
gr.addQuery("ci_field", rec.sys_id);
gr.query();
while (gr.next()) {
var opt = document.createElement('option');
opt.text = gr.name;
opt.value = gr.sys_id;
opt.title = "";
$('configuration_items_select_1').options.add(opt);
}
}
}
Once you have populated your record producer, you will need to work on the "Submit" part of the Record Producer script. The framework to accomplish this will look something like this. All of this code will be contained in the "Script" field of the record producer.
if (producer.existing_record == 'Yes'){
//Do whatever code in here to update the referenced record.
//Remember that your onChange Client script will have been triggered,
//based on the existing_record being yes, to populate any fields you want.
//Once your code is done, use our magic two lines of code.
//This will prevent the record producer from creating a new record.
current.setAbortAction(true);
//This will redirect you to the record specified.
//Need to add the record table, and the record sys_id.
producer.redirect = 'u_recordtable.do?sys_id=' + record.sys_id;
}
else {
//In here would be any processing to happen when the Record producer will actually Complete.
}
If anything, this example got me thinking out-of-the-box about the many different ways you could use a record producer. It is a great front-end to back-end interaction for the user.
Enjoy the out-of-box thoughts....
<script></script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22945975-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
- 8,684 Views
3 Comments
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.