Copy optional fields to child case

tindiz
Giga Guru

I've developed a Script Include, Client Script, and Business Rule aimed at prompting a confirmation message when creating a child case from the Child Case related list on a parent case. This message asks whether you want to copy the short description and description fields from the parent to the child case. The parent field isn’t included on the form, which is why the scripts are set up this way.

However, the scripts aren't working as intended. When I create a child case from the parent, the confirmation message does appear. If I save that child case and then enter a new short description and description, and attempt to create another child case from it, the confirmation message does show up. But when I click OK, the descriptions copied over are from the first case (the original parent of the previously saved child case), not the updated descriptions from the child case.

Could you please help me resolve this issue? Thank you!

 

Script Include:

 

 

var GetParentFields = Class.create();
GetParentFields.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getShortDescription: function() {
        var parentId = this.getParameter('sysparm_parent_id');
        var parentGR = new GlideRecord('sn_customerservice_case');
        if (parentGR.get(parentId)) {
            return parentGR.parent.short_description; // Return the parent's short description
        }
        return ''; // Return an empty string if not found
    },

    getDescription: function() {
        var parentId = this.getParameter('sysparm_parent_id');
        var parentGR = new GlideRecord('sn_customerservice_case');
        if (parentGR.get(parentId)) {
            return parentGR.parent.description; // Return the parent's description
        }
        return ''; // Return an empty string if not found
    },

    type: 'GetParentFields'
});

 

 

Client Script:

 

 

function onLoad() {
    
    // Check if the form is a new record
    if (g_form.isNewRecord()) {
        // Clear the confirmation flags for new cases
        sessionStorage.removeItem('confirmationShown');
        sessionStorage.removeItem('descriptionConfirmationShown');

        var parentId = g_scratchpad.parentSysId;

        // Show confirmation for short description if not already shown
        var confirmationShown = sessionStorage.getItem('confirmationShown');
        if (parentId && !confirmationShown) {
            var answer = confirm("Do you want to copy the short description from the parent case?");
            if (answer) {
                var ga = new GlideAjax('GetParentFields');
                ga.addParam('sysparm_name', 'getShortDescription');
                ga.addParam('sysparm_parent_id', parentId);
                ga.getXMLAnswer(function(response) {
                    if (response) {
                        g_form.setValue('short_description', response);
                    }
                });
            }
            sessionStorage.setItem('confirmationShown', 'true');
        }

        // Show confirmation for description if not already shown
        var descriptionConfirmationShown = sessionStorage.getItem('descriptionConfirmationShown');
        if (parentId && !descriptionConfirmationShown) {
            var descriptionAnswer = confirm("Do you want to copy the description from the parent case?");
            if (descriptionAnswer) {
                var gaDesc = new GlideAjax('GetParentFields');
                gaDesc.addParam('sysparm_name', 'getDescription');
                gaDesc.addParam('sysparm_parent_id', parentId);
                gaDesc.getXMLAnswer(function(response) {
                    if (response) {
                        g_form.setValue('description', response);
                    }
                });
            }
            sessionStorage.setItem('descriptionConfirmationShown', 'true');
        }
    }
}

 

 

Business Rule: When to run: Display

 

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	g_scratchpad.parentSysId = current.parent;

})(current, previous);

 

 

 

 

2 REPLIES 2

Community Alums
Not applicable

Hi @tindiz ,

I tried your problem please check below script

 

Script Include 

 

getShortDescriptionAndDescription: function() {
    var data = {};
    var parentId = this.getParameter('sysparm_parent_id');
    var parentGR = new GlideRecord('sn_customerservice_case');
    if (parentGR.get(parentId)) {
        data.shortDesc = parentGR.parent.short_description + "";
        data.desc = parentGR.parent.description + "";
        return JSON.stringify(data); // Return the parent's short description and Description
    }
    return ''; // Return an empty string if not found
},

 

 

Client Scirpt

function onLoad() {
    
    // Check if the form is a new record
    if (g_form.isNewRecord()) {
        // Clear the confirmation flags for new cases
        sessionStorage.removeItem('confirmationShown');
        sessionStorage.removeItem('descriptionConfirmationShown');

        var parentId = g_scratchpad.parentSysId;

        // Show confirmation for short description if not already shown
        var confirmationShown = sessionStorage.getItem('confirmationShown');
        if (parentId && !confirmationShown) {
            var answer = confirm("Do you want to copy the short description from the parent case?");
            if (answer) {
                var ga = new GlideAjax('GetParentFields');
                ga.addParam('sysparm_name', 'getShortDescriptionAndDescription');
                ga.addParam('sysparm_parent_id', parentId);
                ga.getXMLAnswer(function(response) {
                    if (response) {
						var res = JSON.parse(response);
                        g_form.setValue('short_description', res.shortDesc);
						g_form.setValue('description', res.desc);
                    }
                });
            }
            sessionStorage.setItem('confirmationShown', 'true');
        }

    }
}

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

Hi @Community Alums thank you so much for your help, so I tried it but the behaviour is just the same. Created a child case from the parent, the confirmation message appeared, clicked ok but the fields did not copy. Saved the child case. Now, from the child case I created a child case, confirmation message appeared, clicked OK, the fields got copied.