MRVS deletion and MRVS Variable Mandatory

Anubhav24
Mega Sage
Mega Sage

Hi All,

 

Queries for MRVS:

1) Requirement is to make one variable in MRVS mandatory based on one checkbox on catalog item form :
Able to achieve this if I write a onLoad script(using g_service_catalog) on MRVS and make the field in MRVS as mandatory along with a UI Policy making the MRVS as mandatory but unable to achieve it onChange of one checkbox because the moment I will write a onChange Client Script on MRVS it will be unable to access the variable on catalog item form.

 

2) I need to register that if a row has been deleted from MRVS , if there is a deletion I need to make a checkbox true which indicates row deletion has been done and then collect all the deleted data and show it in multi line text field on catalog item form. From all the community threads it seems that I need to create a Macro and write a code based on session item storage. Need help on code if macro code needs to be written as well.

 

3)Also is below code considered under good coding practice:

this.cat_g_form = g_form;

 

I need guidance on above queries.

 

@Ankur Bawiskar  @Peter Bodelier  @Allen Andreas 

5 REPLIES 5

Dnyaneshwaree
Mega Sage

Hello @Anubhav24 ,

 

For 1St point i.e., on change script issue you can refer below code as an example and update it as per your req:

Script include:

 

var MRVSHelper = Class.create();
MRVSHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    updateMRVSFieldMandatory: function() {
        var mandatory = this.getParameter('sysparm_mandatory') == 'true';
        var itemSysId = this.getParameter('sysparm_item');
        
        // Fetch the MRVS variable set
        var item = new GlideRecord('sc_item_option_mtom');
        item.addQuery('request_item', itemSysId);
        item.query();
        
        while (item.next()) {
            var variableName = item.sc_item_option.getDisplayValue();
            
            // Update the mandatory state of the variable within the MRVS
            var variable = new GlideRecord('sc_item_option');
            if (variable.get(variableName)) {
                variable.setMandatory(mandatory);
                variable.update();
            }
        }

        return 'success';
    }
});

 

Client script:

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

// Fetch the MRVS field using its ID
var mrvs = g_form.getControl('<MRVS Variable Name>');

// Use a GlideAjax call to update the mandatory state of the MRVS field
var ga = new GlideAjax('MRVSHelper');
ga.addParam('sysparm_name', 'updateMRVSFieldMandatory');
ga.addParam('sysparm_mandatory', newValue);
ga.addParam('sysparm_item', g_form.getUniqueValue());
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'success') {
// Refresh the MRVS
g_form.refreshSlushbucket();
} else {
g_form.addErrorMessage('Failed to update MRVS field mandatory state.');
}
});
}

 

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

Hi @Dnyaneshwaree ,

Thanks a lot for your quick reply , but for 1st point do I really need to do GlideAjax , the checkbox is already present on the form and based on it's value I need to make variable mandatory in MRVS.

Dnyaneshwaree
Mega Sage

For the other variables we can directly access that from catalog client script and its values using get value But as per your issue I thought that you are not able to access variable because it is present in MRVS so, I have suggested to use script include to access it. I have not faced this type of issue so, I don't have exact idea for this but this is only my suggestion.

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

Brad Bowman
Kilo Patron
Kilo Patron

I don't understand why you would want or need a UI Policy if you have a working onLoad Catalog Client Script to make the MRVS variable mandatory.  g_service_catalog works to retrieve a Catalog Item variable value in an onChange script within the MRVS the same as onLoad.  Try disabling the UI Policy, and any other conflicting policies or scripts within the MRVS, then post the onChange script if you're still having trouble.