How to copy a newly created record via UI button onto an existing record field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2024 11:32 AM
Hello,
I'm having issues placing a newly created record number into a field in an existing record. The functionality I'm looking for is similar to the "Create Problem" OOB UI button. The Create Problem button creates a new problem record and shows that newly created Problem record's number in the "Problem" field in the Incident form, under "Related Records" section.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2024 01:50 PM
Hi TerryCO3,
Populating a field and related list are two different processes. To populate a field, you need to have your script look up the relevant record and then put the appropriate value in that field. Related records are in separate tables. I suspect that you are creating the record in the separate table. You also need to go into Configure | Related Lists to define that relationship.
:{)
Helpful and Correct tags are appreciated and help others to find information faster
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2024 10:15 PM
Hello. Thanks for the response.
I've tried that:
The code shows a test string that I'm expecting to work as well, but to no avail. I'm thinking the code should be as simple as querying the table in question. Is it because I'm trying to query something too fast and it cannot gather something that is not available yet? I've done this in the past, but may've forgotten something. Any advice with this code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2024 10:59 AM
A few things stick out:
- The only value that you are assigning to the record that will go into u_test_table is the short description.
- Why are you assigning a value to test from current and then overwriting it?
- What are the fields in your current table and u_test_table that enable the linkage?
- At some point, you need a current.update() for the u_test value to be saved.
I've created a number of UI Actions to do similar to what you need. Here's the code I set up to create a problem from an incident.
gsftSubmit(null, g_form.getFormElement(), 'createProblem');
if (typeof window == 'undefined')
doCreateProblem();
}
function doCreateProblem() {
var mySysID = current.update;
var prob = new GlideRecord("problem");
var sysID;
prob.short_description = current.short_description;
prob.description = current.description;
prob.cmdb_ci = current.cmdb_ci;
prob.priority = current.priority;
prob.company = current.company;
prob.sys_domain = current.sys_domain;
prob.assignment_group = current.assignment_group;
prob.assigned_to = gs.getUserID();
prob.impact = current.impact;
prob.urgency = current.urgency;
prob.u_category = current.category;
prob.u_subcategory = current.subcategory;
prob.u_subsubcategory = current.u_subsubcategory;
sysID = prob.insert();
current.problem_id = sysID;
current.state = 3;
current.hold_reason = 3;
mySysID = current.update();
}
:{)
Helpful and Correct tags are appreciated and help others to find information faster
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2024 05:04 AM
Hi @TerryC03 ,
UI Action Script:
function createRelatedRecord() {
var ga = new GlideAjax('CreateRelatedRecord');
ga.addParam('sys_id', g_form.getUniqueValue());
ga.getXMLAnswer(function(response) {
var newRecordNumber = response.responseXML.documentElement.getAttribute('answer');
if (newRecordNumber) {
g_form.setValue('related_record_field', newRecordNumber); // Update with the appropriate field name
g_form.save();
}
});
}
Script Include:
var CreateRelatedRecord = Class.create();
CreateRelatedRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {
createRecord: function() {
var existingRecordSysId = this.getParameter('sys_id');
// Create the new record
var newRecord = new GlideRecord('target_table'); // Replace with the target table
newRecord.initialize();
// Set any necessary fields for the new record
newRecord.insert();
var newRecordNumber = newRecord.getValue('number');
// Update the existing record with the new record's number
var existingRecord = new GlideRecord('incident'); // Replace with the existing record's table
if (existingRecord.get(existingRecordSysId)) {
existingRecord.setValue('related_record_field', newRecordNumber); // Replace with the appropriate field name
existingRecord.update();
}
return newRecordNumber;
}
});
If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.
Thanks,
Amitoj Wadhera