How to hide a specific row from MRVS based on condition

caiooliveira
Tera Expert

I was looking for some article that could help me, but I didn't find anyone about this situation. Is there a way to hide a specific row from MRVS based on some condition? 

 

As example:

caiooliveira_0-1706292013145.png

To hide the second row based on the value from the 'Item' and 'Exame' fields? I was trying to do it through Client Script On load, but I could just access the values from MRVS and do the verification based on the field's values at the background script and store the index from rows that I don't want to hide:

 

 

var itemID = 'b0be6d351b3bb51028d0a6cae54bcb38';
var ritmGR = new GlideRecord('x_ibmfs_csc_v2_instrucao_geral_form');

// Array to store the indices of instances that meet the criteria
var indicesEncontrados = [];

if (ritmGR.get(itemID)) {
    var mrvs = ritmGR.variables.instrucao_geral;

    // Check if mrvs.u_item_ig is an array before attempting to access the length property
    if (Array.isArray(mrvs.u_item_ig)) {
        var qtdItens = mrvs.u_item_ig.length;
        
        // Array of sys_ids for comparison
        var sysIdsDesejados = [
            '25ec099b1bb3701028d0a6cae54bcbac',
            'd57505ef1bb3b01028d0a6cae54bcbca',
            'b20685af1bb3b01028d0a6cae54bcb30',
            'f7e7c5a31bf3b01028d0a6cae54bcb93',
            '1da7c5631bf3b01028d0a6cae54bcbbf'
        ];

        // IIterate over the elements of the array and compare them with desired sysIds
        for (var i = 0; i < qtdItens; i++) {
            var itemAtual = mrvs.u_item_ig[i];

            // Check if the current element is present in the desired sysIds
            if (sysIdsDesejados[i] == itemAtual) {

                // Call the function to compare
                var sys_id_exame = mrvs.u_exame[i];
                var check = checaSeValorExiste(sys_id_exame);

                if (check == true) {
                    // Store the index value of that row in the mrvs
                    indicesEncontrados.push(i);
                    gs.info('Adicionando índice ' + [i] + ' ao array indicesEncontrados.');
                }

            }
        }
    }
}

    function checaSeValorExiste(sysId_exame) {
   
        // function to compare the value from u_exame
        var gr = new GlideRecord('x_ibmfs_csc_v2_sigla');
        gr.addEncodedQuery('sys_id='+ sysId_exame);
        gr.query();

        if (gr.next()) {
            var name = gr.getValue('name');

            var grPar = new GlideRecord('u_portfolio_px_pardini');
            grPar.setLimit(1);
            grPar.addQuery('u_prod_sl_exame', name);
            grPar.query();

            // se a pesquisa na tabela pardini conter, define a resposta do ajax como true
            if (grPar.next()) {

                return true;
            }
			// do contrário, se não conter, false
			return false;
        }
    }

 

This script returns the index from rows that I don't want to hide.

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

In a client script you can get the value of a MRVS using g_form.getValue('mrvs_internal_name') then parse the JSON to iterate the rows, but there is not a way to hide a row or column(s) from the MRVS displayed value, so you would have to (temporarily) delete the row if your conditions are met (then add it back after the form is submitted or whatever).  Another crazy workaround would be to copy the retained or other row(s) to a similar MRVS, hiding the one that contains the objectionable data. 

caiooliveira
Tera Expert

Yes, it worked. I captured the value of MRVS in a client Script onLoad, and parsed it into JSON, thus allowing me to access its properties. With this, I created a new object and replaced the original object, only on the client side. I had to use the scratchpad to store the value of the original object and through another client script, onChange, I could capture the original value of MRVS and overwrite it again, using a choice field to control the flow.

 

Client Script onLoad:

 

    // MRVS capture
    var mrvs = g_form.getValue('mrvs_name');
    
    // Storing MRVS in scratchpad
    g_scratchpad.originalMRVS = mrvs;
    
    // Converting MRVS to an array of objects
    var objects = JSON.parse(mrvs);
    
    // Looping through the MRVS objects
    objects.forEach(function(object) {
        // Accessing attributes of each object
        var id = object.id;
        var name = object.name;
    });
}

The new object needs to have the same properties as the original object and needs to be an array of objects. If the newly created object has the same properties and is an array of objects, simply do the following to overwrite the original MRVS:

// arrObj has the same properties as the original object and is an array of objects.
var newObj = JSON.stringify(arrObj);
g_form.setValue('mrvs_name', newObj);

 

It is important to note that if there is any operation that saves the record, the original object will be overwritten by the new one, and this can be detrimental to data consistency.

 

To work around this, in the Client Script onChange, I captured the value of the original object coming from the scratchpad, processed the data, overwrote it again from the value of the field created to control the flow, and hid the value of MRVS from the user:

function onChange() {
    var fieldControlFlow = g_form.getValue('field_control_flow');

    if (fieldControlFlow == 'u_completed') {
        var objOriginal = g_scratchpad.originalMRVS;
        if (objOriginal) {

                var objOrJSON = JSON.parse(objOriginal);
                var objOrStr = JSON.stringify(objOrJSON);

                g_form.setValue('mrvs_name', objOrStr);
                g_form.setVisible('mrvs_name', false);
            } else {
            //
        }
    }
}

This way, in practical terms, it is possible to "hide" rows of the MRVS and save the record without losing the original data.

 

I know that perhaps this isn't the most elegant method to solve this problem, but It worked for me and I would be happy if someone could bring any other approach to the subject.