Coping Change Request along with all visible variables attached to it/

mattmg
Tera Guru

Hi friendly crowd,

 

I have thought nut to crack, and I hope someone might help me with it

 

We have a change request from where we have  many additional questions that are attached to the main change request from as a variables.

 

I also have a Copy Change button on the back end form, to help submitting changes based on the previous same change. However I came across the problem, when the form is submitted via record producer and have variables attached, I can't copy these variables if I need to use Copy button, It there a way to duplicate the variables and reattach them at the new record?

 

Many thanks for meaningful answers

1 ACCEPTED SOLUTION

mattmg
Tera Guru

Managed to do it without the use of Catalog UI policies, but with some exclusions, this is my current UI Action to Copy Change (with variables)

 

function OnCopyChangeClick() {
    try {
        // Get the Sys ID of the source change request
        var srcSysId = g_form.getUniqueValue();
        if (!srcSysId) {
            g_form.addErrorMessage('Source record not found.');
            return;
        }

        // Create a new change request record
        var newChange = new GlideRecord('change_request');
        newChange.initialize();
        
        // Copy relevant fields from the current form using g_form
        newChange.short_description = g_form.getValue('short_description') || 'Copy of Change Request';
        newChange.category = g_form.getValue('category') || '';
        newChange.priority = g_form.getValue('priority') || '';
        newChange.risk = g_form.getValue('risk') || '';
        newChange.impact = g_form.getValue('impact') || '';
        newChange.type = g_form.getValue('type') || '';
        newChange.u_location = g_form.getValue('u_location') || '';
        newChange.assignment_group = g_form.getValue('assignment_group') || '';
        newChange.assigned_to = g_form.getValue('assigned_to') || '';
        newChange.description = g_form.getValue('description') || '';
        newChange.justification = g_form.getValue('justification') || '';
        newChange.change_plan = g_form.getValue('change_plan') || '';
        newChange.risk_impact_analysis = g_form.getValue('risk_impact_analysis') || '';
        newChange.backout_plan = g_form.getValue('backout_plan') || '';
        newChange.test_plan = g_form.getValue('test_plan') || '';

        // Insert the new change request record
        var newSysId = newChange.insert();
        if (!newSysId) {
            g_form.addErrorMessage('Failed to create new change request record.');
            return;
        }

        // Get the question answers from the original change request
        var qaGR = new GlideRecord('question_answer');
        qaGR.addQuery('table_name', 'change_request');
        qaGR.addQuery('table_sys_id', srcSysId);
        qaGR.query();

        // If there are no question answers to copy, return
        if (!qaGR.hasNext()) {
            g_form.addErrorMessage('No question answers found to copy.');
            return;
        }

        // List of variables to exclude from copying
        var excludedVariables = [
            'additional_questions', 'initial_note', 'u_important_info', 'note', 'risk', 'impact',
            'justification', 'change_plan', 'backout_plan', 'risk_impact_analysis', 'test_plan',
            'u_implementer_s_contact_details', 'u_services_affected', 'user_has_itil_role',
            'description', 'u_network_monitoring_pause_details', 'type', 'change_request_created_by'
        ];

        // Process each question answer and insert it into the new change request
        var copiedVariables = [];
        while (qaGR.next()) {
            var questionGR = new GlideRecord('question');
            if (questionGR.get(qaGR.question)) {
                // Only copy variables that are not excluded and have a non-empty value
                if (excludedVariables.indexOf(questionGR.name) === -1 && qaGR.value && qaGR.value.trim() !== '') {
                    var newQA = new GlideRecord('question_answer');
                    newQA.initialize();
                    newQA.table_name = 'change_request';
                    newQA.table_sys_id = newSysId;
                    newQA.question = qaGR.question;
                    newQA.value = qaGR.value;
                    newQA.insert();

                    // Store the copied variable for the message
                    copiedVariables.push(questionGR.name);
                }
            }
        }

        // Generate the URL for the new change request
        var newRecordUrl = '/change_request.do?sys_id=' + newSysId;

        // Prepare the success message with a link to the new record
        var message = 'Change request copied successfully. <a href="' + newRecordUrl + '" target="_self">Click here</a> to view the new Change Request.<br>';
       
        // Show the success message
        g_form.addInfoMessage(message);

    } catch (e) {
        // Handle any errors that occur during the process
        g_form.addErrorMessage('An unexpected error occurred: ' + e.message);
        gs.error('Error in OnCopyChangeClick: ' + e.message);
    }
}

 

Two things I did not manage to do it, as it was breaking script, sort the added variables a-z, and redirect from to anew record at the end. Instead I decided to put link to a new CR in the message o top of the form, when coping is completed

View solution in original post

5 REPLIES 5

This is great and smart at the same time - Thank you so much for sharing this solution with the entire world 🙂


Hope that helps!