UI Action on Problem Task table that will copy fields to the Problem.

MayrelCerero
Tera Expert

Hi Everyone, I am creating my first UI Action button that is on the problem task form named 'Copy to Problem'. With that button, when the user clicks on it it would then copy the fields from the problem task to what I believe is considered the parent Problem. Here are the mapping that were provided to me along with some instructions: 

Create a new UI Action (a button labeled "Copy to Problem") on the PTASK form. This will copy fields from the PTASK to the parent PRB, using the following mappings:

PRB Problem Statement | PTASK Short Description
PRB Description | PTASK Description
PRB Analysis Information > Workaround | PTASK Analysis Information > Workaround
PRB Analysis Information > Cause Notes | PTASK Analysis Information > Cause notes
PRB Analysis Information > Fix notes | PTASK Analysis Information > Proposed fix notes
PRB Summary > Summary | PTASK Analysis Information | Summary
PRB Analysis Information > Contributing factors | PTASK Analysis Information > Contributing factors

 

And here is my script: 

function copyToProblem() {
    var ptaskRecord = g_form.getTableName();
    gs.log("MC log - Table name: " + ptaskRecord);
    var ptaskSysID = g_form.getUniqueValue();
    gs.log("MC log - Task Sys ID: " + ptaskSysID);
    var problemSysID = g_form.getValue('parent');
    gs.log("MC log - Problem Sys ID: " + problemSysID);

    var ptaskFields = {
        'Short description': 'short_description',
        'Description': 'description',
        'Workaround': 'workaround',
        'Cause notes': 'cause_notes',
        'Proposed fix notes': 'fix_notes',
        'Summary': 'u_summary',
        'Contributing factors': 'u_contributing_factors'
    };

    var problemFields = {
        'Problem statement': 'short_description',
        'Description': 'description',
        'Workaround': 'workaround',
        'Cause notes': 'cause_notes',
        'Fix notes': 'fix_notes',
        'Summary': 'u_summary',
        'Contributing factors': 'u_contributing_factors'
    };

    var ptaskGR = new GlideRecord(ptaskRecord);
    if (ptaskGR.get(ptaskSysID)) {
        var problemGR = new GlideRecord('problem');
        if (problemGR.get(problemSysID)) {
            for (var ptField in ptaskFields) {
                var prbField = ptaskFields[ptField];
                if (ptaskGR.isValidField(ptField) && problemGR.isValidField(problemFields[prbField])) {
                    problemGR.setValue(problemFields[prbField], ptaskGR.getValue(ptField));
                }
            }
            problemGR.update();
            g_form.addInfoMessage('Fields copied successfully.');
        } else {
            g_form.addErrorMessage('Parent problem record not found.');
        }
    } else {
        g_form.addErrorMessage('PTASK record not found.');
    }
}
The issues that I'm running to is that nothing is being copied to the problem form from the problem task, and I see two 'Copy to Parent' buttons on the problem task form, but one of them when I click it on it nothing happens, but the other one, when I click on it prompts me to create a new problem task. I would appreciate any help I can receive from you. Thank you! 🙂
1 ACCEPTED SOLUTION

Appanna M
Tera Guru

Hello @MayrelCerero ,

Try the below logic inside your UI Action script, It should works fine. I have tested it in my PDI working fine.

Just map your fields accordingly as per your requirement. 

function copyToProblem() {
    gsftSubmit(null, g_form.getFormElement(), 'copy_to_problem');
}

if (typeof window == 'undefined')
    updateData();

function updateData() {
var probsysid = current.problem;
var problemTask = current.number;
gs.info("ProbSysID:" + probsysid + ":ProblemTask:" + problemTask +":ProbTaskSD:"+current.short_description);
var problem = new GlideRecord('problem');
problem.addQuery('sys_id', probsysid);
problem.query();
    if (problem.next()) {
        problem.short_description = current.short_description;
        problem.description = current.description;
        problem.workaround = current.workaround; // replace your fields
        problem.summary = current.summary;// replace your fields
        problem.update();
    }
    action.setRedirectURL(current);
}

Below are the screenshots:

Appanna_0-1711739249458.pngAppanna_1-1711739312080.pngAppanna_2-1711739412450.pngAppanna_3-1711739446410.png

 

 

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.

 

View solution in original post

3 REPLIES 3

Appanna M
Tera Guru

Hello @MayrelCerero ,

Try the below logic inside your UI Action script, It should works fine. I have tested it in my PDI working fine.

Just map your fields accordingly as per your requirement. 

function copyToProblem() {
    gsftSubmit(null, g_form.getFormElement(), 'copy_to_problem');
}

if (typeof window == 'undefined')
    updateData();

function updateData() {
var probsysid = current.problem;
var problemTask = current.number;
gs.info("ProbSysID:" + probsysid + ":ProblemTask:" + problemTask +":ProbTaskSD:"+current.short_description);
var problem = new GlideRecord('problem');
problem.addQuery('sys_id', probsysid);
problem.query();
    if (problem.next()) {
        problem.short_description = current.short_description;
        problem.description = current.description;
        problem.workaround = current.workaround; // replace your fields
        problem.summary = current.summary;// replace your fields
        problem.update();
    }
    action.setRedirectURL(current);
}

Below are the screenshots:

Appanna_0-1711739249458.pngAppanna_1-1711739312080.pngAppanna_2-1711739412450.pngAppanna_3-1711739446410.png

 

 

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.

 

Thank you so so much for your help! Your script fixed the issues I ran into before. I appreciate it! ðŸ˜€

Thank you. Please Mark my Answer as Helpful.