How to populate new and existing record from UI Action in corresponding fields

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2023 07:58 AM - edited 08-28-2023 01:53 PM
Hello!
I have created a Create Demand UI Action from an application and am needing to populate the record number that the demand is being created from onto a field on the Demand. I also then need to populate the Demand number that is being created into a field on the record the Demand was created from. The UI Action as it is, is working, a Demand is created and all the fields I am taking data from the record to populate the demand or applying values only on the Demand is just as expected. I am just having some issues getting the two record numbers
Can I accomplish getting the two record numbers in the two fields I need within the UI Action itself or does this need to be handled afterwards? They are reference fields so they need to be able reference the existing record to populate on the demand and then reference the demand number after creation to populate on the existing record that triggered the demand. I believe I will need to do glide queries to look up the correct records as I am unable to get the sysID which is what the reference fields are needing I think so I can't hardcode them like a static value.
Field on the Demand = "Demand created from" (references the task table) this should populate with the record the demand created from.
Field on the Application = "Related Demand" (references the demand table) this should populate with the newly created demand.
Below is my code
function navigateToNewDemandCreation() {
var dialog = new GlideModalForm('Create Demand', 'dmn_demand'); //Provide dialog title and table name
dialog.setSysID("-1"); //Pass in sys_id as -1 to create new record
var sysparmQuery = "u_governance=" + g_form.getUniqueValue();
dialog.addParm("sysparm_query", sysparmQuery);
dialog.addParm('sysparm_form_only', 'true'); //Add or remove related list
dialog.setOnloadCallback(onLoadPreFillDemandForm);
dialog.setCompletionCallback(afterDemandFormSubmitted);
dialog.render(); //Open the dialog
}
function onLoadPreFillDemandForm(modalForm) {
var iframe = modalForm.$window.find("#dialog_frame")[0];
var iframeWindow = iframe.contentWindow ? iframe.contentWindow : iframe.contentDocument.defaultView;
var d_form = iframeWindow.g_form;
//Sets the values for fields on the New Demand Form from values on the EDGE form
d_form.setValue("short_description", g_form.getValue("short_description")); //Sets the value for short description on New Demand Form
d_form.setValue("u_requested_by", g_form.getValue("u_requested_by")); //Sets the value for requested by on New Demand Form
d_form.setValue("opened_by", g_form.getValue("opened_by")); //Sets the value for opened by on New Demand Form
d_form.setValue("u_applicability", g_form.getValue("u_applicibility")); //Sets the applicibility on New Demand Form
d_form.setValue("impacted_business_units", g_form.getValue("u_impacted_bus_unit")); //Sets the value for impacted business unit on New Demand Form
d_form.setValue("business_applications", g_form.getValue('u_bus_app')); //Sets the value for business applications on New Demand Form
d_form.setValue("u_primary_product", g_form.getValue("u_primary_product")); //Sets the value for primary product on New Demand Form
d_form.setValue("u_add_products", g_form.getValue("u_add_products")); //Sets the value for additional products on New Demand Form
d_form.setValue("u_demand_created_from", g_form.getValue("number"));
//Sets the value for business case on the New Demand From from the four fields and concatenates them
d_form.setValue("u_business_case", "<strong>Description:</strong> " + g_form.getValue("description") + " " + " <strong>Please describe the problem needing to be solved:</strong> " + g_form.getValue("u_problem_description") + " " + " <strong>Recommended Solution:</strong> " + g_form.getValue("u_recommended_solution") + " " + " <strong>Committee Meeting Notes:</strong> " + g_form.getValue("u_notes_from_meeting"));
//sets the default values on the New Demand form for values not on the edge form
d_form.setValue("u_initiative_type", "enterprise_operational"); //
d_form.setValue("category", "strategic"); //
d_form.setValue("type", "project");
d_form.setValue("u_mandated", "No");
d_form.setValue("u_project_class2", "3687451cdbf03d10ce8463a4059619ee"); //sysID of the EDGE Request Project Classification
d_form.setValue("u_planned", "unplanned"); //
d_form.setValue("u_fulfiller_group", "Enterprise");
d_form.setValue("state", 2); //Sets the State to Submitted
}
function afterDemandFormSubmitted(action_verb, sys_id, table, displayValue) {
if (action_verb === 'sysverb_insert') {
g_form.addInfoMessage('Demand created successfully.');
} else {
g_form.addErrorMessage('Demand not created.');
}
}
Any help would be greatly appreciated!
Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2023 10:00 AM
In my code on the line where I have (see below), this grabs the number and I can see it in the field within the modal but when I submit I get an error message of an invalid update and I think this is because its a reference field and I don't have the sysID. I have this commented out to get the Demand to submit but forgot it in the code snippet.
d_form.setValue("u_demand_created_from", g_form.getValue("number"));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2023 01:57 PM
Getting closer. I was able to do g_form.getUniqueValue for the line to set the Demand created from field and that is working. Now just need to get the newly created Demand number onto my other field.