Copy optional fields to child case client script

tindiz
Giga Guru

Hello ServiceNow experts!

I have created the below Client Script which is working as expected and this is because the 'Parent' field is on the Case form.

 

 

 

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_form.getValue('parent'); 

        // 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');
        }
    }
}

 

 

tindiz_0-1730351935333.png

 

However, this 'Parent' field is not originally on the form.

The Client Script is just working because I have added it.

If I remove this field the Client Script will not execute properly.

Please help on what to replace on the line: var parentId = g_form.getValue('parent');

So that it will still execute even if the Parent field is not on the form.

 

1 ACCEPTED SOLUTION

Hi @tindiz ,

 

One line of change you can do like below.

return parentGR.parent.short_description;

 

In both function just return "return parentGR.parent.short_description;" and from client script just pass current sys_id.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

6 REPLIES 6

Runjay Patel
Giga Sage

Hi @tindiz ,

 

You are using the parentId to get the parent sys_id of current record.

 

You can do 2 thing,

 

1. You can pass current sys_id of record and in script include do that gliderecord to get the parent record details.

2. You can create one display business rule and use g_scratchpad variable and return the parent sys_id. In this case you need to change only one line in client script. 

var parentId = g_scratchpad.parentSysId;

 In business rule you use code like.

g_scratchpad.parentSysId = cuurent.parent;

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Hi @Runjay Patel thank you for your response, here's my existing 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.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.description; // Return the parent's description
        }
        return ''; // Return an empty string if not found
    },

    type: 'GetParentFields'
});

 

May you please help me to modify it so I can do your 1st suggestion? ( You can pass current sys_id of record and in script include do that glidrecord to get the parent record details.)

Hi @tindiz ,

 

One line of change you can do like below.

return parentGR.parent.short_description;

 

In both function just return "return parentGR.parent.short_description;" and from client script just pass current sys_id.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Hi @Runjay Patel the confirmation message did appear but the short description and description did not get copy from parent to child after clicking OK, may you please help me to pinpoint which part did I miss?

 

So here's the updated 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'
});

 

 

 

Here's the update 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');
        }
    }
}

 

 

 

And the I created a Business Rule, when display:

 

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

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

})(current, previous);