The CreatorCon Call for Content is officially open! Get started here.

Fetch state value from change request table to catalog item

Shalika
Tera Expert

I have a catalog item with 2 fields 'change request ID' and 'state of change request' . I have to fetch the state of change request based on change request ID. I have written onChange client script for this but it is not working properly.

 

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


var usr = g_form.getReference('change_request_id', callBack);

}

function callBack(usr)
{

g_form.setValue('state_of_change_request', usr.state);

}

It is fetching the value as follow -

Shalika_0-1677062408483.png

In the choice table for state field the value is set as 

Shalika_1-1677062597087.png

It is fetching the value, but I want the label value to be populated on the form. How can I do this?

 

1 ACCEPTED SOLUTION

Hi Shalika,

Check below code-for glideAjax

This is code for client script-

var ga = new GlideAjax('DataHelperAJAX');

ga.addParam('sysparm_name','getState'); getState

ga.addParam('sysparm_if','change_id');

ga.getXML(populateCampus);

function populateCampus(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");

g_form.setValue('campus',answer);

}

}

 

//--you have to create script include and function with name getState

//use this id in script include ,glide to change table ,get state value as-

write this code in script include function-

var id = this.getParameter('change_id');

var gr = new GlideRecord('change_request');

gr.addQuery('sys_id',id);

gr.query();

if(gr.next()){

var state =gr.getDisplayValue('state');

}

return state;

Thanks,

Manjusha 

 

View solution in original post

8 REPLIES 8

IAmAbhigyaan12
Giga Guru

function onChange(control, oldValue, newValue, isLoading) {

  if (isLoading) {

    return;

  }

 

  var changeRequest = g_form.getValue('change_request_id');

  if (changeRequest) {

    g_form.getReference('change_request_id', callBack, 'change_request');

  }

}

 

function callBack(reference) {

  if (reference) {

    g_form.setValue('state_of_change_request', reference.getValue('state'));

  }

}

 

You can check hope this helps.

You can give thumps up, if there is any issue can tag me.

 

Thanks

Abi

If my answer solved your issue, please mark my answer as Correct & hit like Helpful

Thanks
Abi

It is still fetching the value as 1,2 not fetching the label 

Create a new script include

var GetStateOfChangeRequest = Class.create();

 

GetStateOfChangeRequest.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    getState: function() {

        var gr = new GlideRecord('change_request');

        gr.addQuery('number', this.getParameter('sysparm_change_request_id'));

        gr.query();

        if (gr.next()) {

            return gr.state.getDisplayValue();

        }

        return '';

    },

 

    type: 'GetStateOfChangeRequest'

});

 

 

And on onChange function you can write this

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === '') {

        return;

    }

 

    var ga = new GlideAjax('GetStateOfChangeRequest');

    ga.addParam('sysparm_name', 'getState');

    ga.addParam('sysparm_change_request_id', newValue);

    ga.getXMLAnswer(function(answer) {

        g_form.setValue('state_of_change_request', answer);

    });

}

Hope this works

If my answer solved your issue, please mark my answer as Correct & hit like Helpful

Thanks
Abi

It is still not fetching the state values