How to insert a related record in MRVS OnChange of variable

Kartik Magadum
Kilo Sage

When the variable 'Contract Number' changes, I want to populate the 'Asset' variable in MRVS.

Below I have provided the client script and script included for reference. 

 

Client Script:

 

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

    var ctnumber = newValue;
    var ga = new GlideAjax('Populate_contact_details');
    ga.addParam('sysparm_name', 'populate');
    ga.addParam('contact_name_is', ctnumber);
    ga.getXMLAnswer(getData);

    function getData(response) {
        var answer = JSON.parse(response);
        alert(answer.assetname); //getting an alert as required. 
        g_form.setValue('asset', answer.assetname);  // not setting a 'Asset' value. as i am getting info message from server side
    }
}

 

Script Include:

 

var Populate_contact_details = Class.create();
Populate_contact_details.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    populate: function() {
        var obj = {};
        var assetname = new GlideRecord('ast_contract');
        assetname.addQuery('sys_id', this.getParameter('contact_name_is'));
        assetname.query();
        if (assetname.next()) {
			gs.addInfoMessage('inside 1st if condition');
            var assets_covered = new GlideRecord('clm_m2m_contract_asset');
            assets_covered.addQuery('contract.vendor_contract', assetname.vendor_contract);
            assets_covered.query();
			gs.addInfoMessage('entering');
            if (assets_covered.next()) {
				gs.addInfoMessage('inside 2nd if condition');
				gs.addInfoMessage('Asset name is ' + assets_covered.asset.getDisplayValue());
                obj.assetname = assets_covered.asset.getValue();
                obj.dateadded = assets_covered.added;
                obj.dateremoved = assets_covered.removed;
            }
        }
		return JSON.stringify(obj);
    },
    type: 'Populate_contact_details'
});

 

Any assistance in resolving the issue would be greatly appreciated. 

Thanks and Regards

Kartik Magadum

 

 

5 REPLIES 5

Shubham Singh
Mega Guru

Hi @Kartik Magadum 

 

You have to pass the complete JSON object in string to set the value of MRVS.

Refer to the below thread for more info:

https://www.servicenow.com/community/developer-articles/update-value-in-multi-row-variable-set-mrvs/...

 

Thanks!

 

Mark it as correct and helpful if it works for you ✔️👍