Confirm copy optional fields from parent to child case

tindiz
Giga Guru

I have created a Client Script where the short description and description fields are optional to be copied when a child case is created from a parent. However, it is not executing as expected, I am getting "No valid parent Case specified". Please help in determining which part went wrong:

 

 

function onLoad() {
    // Check if this is a new record
    if (g_form.isNewRecord()) {
        var parentID = g_form.getValue('parent'); // Get the Parent ID
		console.log("Parent ID:", parentID);
        
        if (parentID) {
            // Ask for short description
            var copyShortDesc = confirm("Would you like to copy the Short description from the Parent Case?");
            
            if (copyShortDesc) {
                var parentGr = new GlideRecord('sn_customerservice_case');
                if (parentGr.get(parentID)) {
                    g_form.setValue('short_description', parentGr.short_description);
                } else {
                    alert("Parent record not found.");
                }
            }

            // Ask for description
            var copyDesc = confirm("Would you like to copy the Description from the Parent Case?");
            
            if (copyDesc) {
                var parentGr = new GlideRecord('sn_customerservice_case');
                if (parentGr.get(parentID)) {
                    g_form.setValue('description', parentGr.description);
                } else {
                    alert("Parent record not found.");
                }
            }
        } else {
            alert("No Parent Case specified.");
        }
    }
}

 

4 REPLIES 4

Tai Vu
Kilo Patron
Kilo Patron

Hi @tindiz 

Using GlideRecord in a Client Script is generally not recommended due to their performance impact.

Ref: Minimize Server Lookups

 

It’s better to consider alternative approaches for generating child cases from parent cases with copied fields. Could you share more details on how the child case creation currently works in your instance? That way, we can help find the best solution.

 

Cheers,

Tai Vu

Hi @Tai Vu so the requirement is that, when we click on the 'New' UI action button on the Child Cases related list on the Case record, when the form opens there should be a pop-up message asking if short description and description from the parent case will be copied over to the child case. If we click Yes or OK then it will be copied and if we click No or cancel then it will not.

PrashantLearnIT
Giga Sage

Hi @tindiz 

 

The first thing using glide record in the client script is not recommended, try to use the script include here to fulfill your requirements.

 

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

Murthy Ch
Giga Sage

Hello @tindiz 

What you can do is when we hit the UI action from related list. The parent sys_id will be append in the url.

So firstly perform the validation and check if the parent sys_id is present or not. If yes get that sys_id and get the case details using server side script (perform GlideAjax call) and populate the details based on the user response.

 

Thanks,
Murthy