UI action to create a new record and transfer values

優大上
Tera Contributor

I have a question about UI actions.

_1-1729133162351.png

_3-1729133744160.png

 

I am currently trying to create a new record in the `本番作業申請`table from an incident record. I am also creating a UI action to attach the incident record number value to the <関連障害管理番号> of the `本番作業申請`new record, but I am having trouble because I don't know how to script.

 

_4-1729133849651.png

 

I'm sorry, but could someone help me?

1 REPLY 1

Sanjay191
Tera Sage

Hello @優大上 

Please Refer the below script logic and apply changes according to your recuirement.

try {
// Step 1: Create a new Incident record
var incidentGR = new GlideRecord('incident');
incidentGR.initialize();

// Set values for the Incident fields
incidentGR.short_description = 'This is a sample incident created by script';
incidentGR.caller_id = 'sys_id_of_caller'; // Replace with actual caller sys_id or variable
incidentGR.urgency = 2; // Set urgency (1 - High, 2 - Medium, 3 - Low)

// Insert the Incident record
var incidentSysID = incidentGR.insert();

// Check if Incident was created successfully
if (incidentSysID) {
gs.addInfoMessage('Incident created successfully with number: ' + incidentGR.number);

// Step 2: Create a new Problem record associated with the Incident
var problemGR = new GlideRecord('problem');
problemGR.initialize();

// Set values for the Problem fields
problemGR.short_description = 'Problem related to incident: ' + incidentGR.number;
problemGR.incident = incidentSysID; // Associating the problem with the incident
problemGR.description = 'This problem was created based on the incident record.';

// Insert the Problem record
var problemSysID = problemGR.insert();

// Check if Problem was created successfully
if (problemSysID) {
gs.addInfoMessage('Problem created successfully with number: ' + problemGR.number);

// Establish a reference between Problem and Incident
incidentGR.problem_id = problemSysID;
incidentGR.update();
} else {
gs.addErrorMessage('Failed to create a Problem record.');
}
} else {
gs.addErrorMessage('Failed to create an Incident record.');
}
} catch (error) {
gs.error('Error while creating Incident and Problem records: ' + error.message);
gs.addErrorMessage('An error occurred. Please check the logs.');
}

Please refer to the existing logic without directly copying and pasting it. Apply the necessary changes so that it can create an incident and then a problem that will be linked to the incident record. In your case, the table name is 'u_production_work_application_table' instead of 'problem,' so make adjustments accordingly.

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You