Set Value on MVRS from the catalog form variable

Kaustubh k
Tera Contributor

Hello All,

 

Thanks for looking into the ask,

 

We have a catalog variable : Applications(list collector)

 

We have a mvrs: which has Applications(reference) field.

 

We need to create and set, each row for the applications selected on the Catalog form variable Application(list collector) on to the MVRS.

 

 

Any feedback on the same will be highly appreciable.

Thanks

 

12 REPLIES 12

Bhavani Shankar
Tera Guru

Hi @Kaustubh k ,

 

This can be achieved through catalog client scripts.

 

Can I know what is the structure of your MRVS, and give me a brief sense of how / on which variable(s) you want the values selected from list collector to be mapped. So that I can help further.

 

Regards,

Bhavani Shankar

Regards,
Bhavani Shankar
Linked In

Thanks @Bhavani Shankar  for looking in to the ask,

 

There is a field list collectors which allows multiple selections for Application on the catalog form;

Select the application:(ABC,EFG..)

 

We have a MVRS;

With fields;

Application name    Application Owner

1.ABC                       XYZ

2.EFG                        QRS

 

The ask was to make the field on MVRS auto populate with the values on the Catalog form Variable(list collector).

 

eg: for Applications: ABC and EFG of the List collector we need to have two entries on the MVRS created as shown above.

 

hope this clarifies the asks.

 

Thanks

 

 

 

Community Alums
Not applicable

Hi @Kaustubh k ,

 

You need to create a Script include and client script to handle this-

Script include- 

var MVRSRecordCreator = Class.create();
MVRSRecordCreator.prototype = {
    initialize: function() {},

    createRecords: function(applications) {
        var appList = applications.split(',');
        for (var i = 0; i < appList.length; i++) {
            var app = appList[i];
            var mvrs = new GlideRecord('sc_multi_row_question_answer'); //Replace if different table
            mvrs.initialize();
            mvrs.setValue('application_name', app); // Replace 'application_name' with your MVRS field name
            mvrs.setValue('application_owner', 'default_owner'); // Set the default owner or get this value dynamically
            mvrs.insert();
        }
        return 'success';
    },

    type: 'MVRSRecordCreator'
};

 

Calatog Client Script-

function onSubmit() {
    var applicationList = g_form.getValue('applications'); //your list collector variable name
    if (applicationList) {
        var ga = new GlideAjax('MVRSRecordCreator');
        ga.addParam('sysparm_name', 'createRecords');
        ga.addParam('sysparm_applications', applicationList);
        ga.getXMLAnswer(function(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            if (answer !== 'success') {
                g_form.addErrorMessage('Failed to create MVRS records.');
                return false;
            }
        });
    }
    return true;
}

Note- script include should be client callable.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

Hello @Community Alums ,

 

Thanks for replying ,

 

The code is not working and SI is not getting triggered on Change and there is no records created in sc_multi_row_question_answer.

 

Updated the SI and catalog client script created on catalog item and type on change of application variable(list collector).

 

if you can please have a review once and let me know for any issues here tried some approaches but no luck on this.

 

as Below

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   var applicationList = g_form.getValue('select_the_application');
   alert(applicationList);
   //your list collector variable nameif (applicationList) {
    var ga = new GlideAjax('MVRSRecordCreator');        
    ga.addParam('sysparm_name', 'createRecords');        
    ga.addParam('applications', applicationList);        
    ga.getXMLAnswer(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer !== 'success') {                
            g_form.addErrorMessage('Failed to create MVRS records.');
            return false;
            }
            });
            return true;

   //Type appropriate comment here, and begin script below
   
}

 

 

And the related script include client callable;

 

var MVRSRecordCreator = Class.create();
MVRSRecordCreator.prototype = {
    initialize: function() {},

    createRecords: function() {
        var appl =this.getParameter('applications');
        gs.info ("appl");
        var appList = appl.split(',');
        for (var i = 0; i < appList.length; i++) {
            var app = appList[i];
            var mvrs = new GlideRecord('sc_multi_row_question_answer');
            mvrs.initialize();
            mvrs.setValue('application', app);
            //mvrs.setValue('application_owner', 'default_owner'); // Set the default owner or get this value dynamically
            mvrs.insert();
        }
        return 'success';
    },

    type: 'MVRSRecordCreator'

 

 

Thanks