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

Animesh Das2
Mega Sage

Hi @keshav77,

 

Definitely the Problem table has a reference field referencing the incident record.

Write a BR on Incident and do gliderecord query on Problem table using the current.getUniqueValue(). If you find no records from the gliderecord query that means the related list is empty and abort the action of update using current.setAbortAction(true).

 

If this address your question, please don't forget to mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das

Runjay Patel
Giga Sage

Hi @keshav77 ,

 

You can write before BR.

RunjayPatel_0-1731597491231.png

RunjayPatel_1-1731597556869.png

 

Code:

var gr = new GlideRecord('problem');
    gr.addQuery('parent', current.sys_id);
    gr.query();
    if (!gr.next()) {
        gs.addErrorMessage('Please attache a problem to move state in-progress');
        current.setAbortAction(true);
    }

Output: 

RunjayPatel_2-1731597596661.png

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

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

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

 

 

 

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

 

this is not working for me. I am using in the same as you shared but it is not working some how