Related list

keshav77
Tera Contributor

In Incident form there is related list name-problem if the related list is empty I can not change the state of incident form new to inprogrees. if it is not empty then I can move the state. how I can achieve this..

1 ACCEPTED SOLUTION

Ashish Parab
Mega Sage

@keshav77 

 

Try below onChange Client script and script include -

 

onChange client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === oldValue) {
        return;
    }

    if (newValue == 2) {

        // Call the Script Include
        var ga = new GlideAjax('CheckRelatedProblems');
        ga.addParam('sysparm_name', 'hasRelatedProblems');
        ga.addParam('sysparm_sys_id', g_form.getUniqueValue());

        ga.getXMLAnswer(function(response) {

            if (response == "false") {
                g_form.addErrorMessage("Cannot change the state to In Progress because there are no related Problems.");
                g_form.setValue('state', oldValue); // Revert the state to its previous value
            }
        });
    }
}

 

Script Include

var CheckRelatedProblems = Class.create();
CheckRelatedProblems.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    hasRelatedProblems: function() { // Changed method name to match sysparm_name

        var incidentId = this.getParameter('sysparm_sys_id');

        var problemGR = new GlideRecord('problem');
        problemGR.addQuery('parent', incidentId); // Adjusted field if needed
        problemGR.query();

        // Return result as "true" or "false" as a string
        var result = problemGR.hasNext() ? "true" : "false";
        return result;
    },

    type: 'CheckRelatedProblems'
});

 

Output - 

ashish_parab_0-1731597841759.png

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish

 

View solution in original post

6 REPLIES 6

ok now I get it

 

@keshav77 ,

Is it working for you? Let me know if you require anything else.

 

Please mark as "Accepted Solution" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish