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

Please ignore my last reply. It did work now! Thank you soooo much!!!!!!!!!!!!!

for some reason, I don't know if it's a system glitch but sometimes it work, sometimes not. If I try to create a child case from a child case, it doesn't work...