Script to compare variables

Ariomar de Deus
Tera Contributor

I have some variables in the catalog item and some variables that are in a MRVS (Minimum Reference Value), but I can't compare the variables because the variables outside the MRVS are coming back empty. Could someone help me with this script?

function onChange(control, oldValue, newValue, isLoading) {
 
    if (isLoading || newValue === oldValue) return;
 
    g_form.clearMessages();
 
    // Valores da API
    var tipo = g_form.getValue('api_tipo_pb');
    var classe = g_form.getValue('api_classe_pb');
    var subclasse = g_form.getValue('api_subclasse_pb');
    var sequencial = g_form.getValue('api_sequencial_pb');
 
    // Valores da assessoria
    var tipo_assessoria = g_form.getValue('tipo_pb');
    var classe_assessoria = g_form.getValue('classe_pb');
    var subclasse_assessoria = g_form.getValue('subclasse_pb');
    var sequencial_assessoria = g_form.getValue('sequencial_pb');
 
   
    var numContrato = g_form.getValue('numeroContrato');
   
    // alert('valor ' + numContrato);
    // alert('tipo ' + tipo);
    // alert('tipo assessoria' + tipo_assessoria);
 
       
        // Validação
    if (tipo != tipo_assessoria ||
        classe != classe_assessoria ||
        subclasse != subclasse_assessoria ||
        sequencial != sequencial_assessoria) {
 
        g_form.addErrorMessage('Item não pertence ao contrato selecionado. Favor verificar se PBMS pertence ao contrato selecionado.');
 
        // Limpa campos
        g_form.clearValue('api_tipo_pb');
        g_form.clearValue('api_classe_pb');
        g_form.clearValue('api_subclasse_pb');
        g_form.clearValue('api_sequencial_pb');
 
    } else {
 
        g_form.addInfoMessage('Tudo certo! Buscando dados do contrato...');
 
       
 
        //  Chamada ao Script Include
        var ga = new GlideAjax('x_rtb_5ll_0001110.store_verifica_contr');
        ga.addParam('sysparm_name', 'verificarContratos');
        ga.addParam('sysparm_contrato', numContrato);
 
        ga.getXMLAnswer(function(response) {
 
            var result = JSON.parse(response);
 
            if (result.status == 'valido') {
 
                // Preenche os campos
                g_form.setValue('nome_prefixo_contabil', result.numero_prefixo_contabil);
                g_form.setValue('nome_dependencia_contabil', result.dependencia_contabil);
                g_form.setValue('conta_contabil', result.conta_contabil);
                g_form.setValue('valor_por_equipamento', result.valor_por_equipamento);
 
                g_form.addInfoMessage('Dados do contrato carregados com sucesso!');
 
            } else {
 
                g_form.addErrorMessage(result.mesagem || result.status);
 
            }
 
        });
    }
}
 

 

 

 (variables outside the MRVS)

 tipo_assessoria, classe_assessoria, subclasse_assessoria and sequencial_assessoria

3 REPLIES 3

Naveen20
ServiceNow Employee

When your onChange client script runs inside an MRVS row, the g_form object refers to the MRVS row form, not the parent catalog item form. That's why tipo_assessoria, classe_assessoria, etc. come back empty — those variables don't exist in the MRVS row; they live on the parent catalog item.

The fix is to use g_form.getParentForm() to reach the parent catalog item's form and read those variables from there:

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

    if (isLoading || newValue === oldValue) return;

    g_form.clearMessages();

    // Get reference to the PARENT catalog item form
    var parentForm = g_form.getParentForm();

    // Valores da API (inside MRVS — use g_form)
    var tipo = g_form.getValue('api_tipo_pb');
    var classe = g_form.getValue('api_classe_pb');
    var subclasse = g_form.getValue('api_subclasse_pb');
    var sequencial = g_form.getValue('api_sequencial_pb');

    // Valores da assessoria (outside MRVS — use parentForm)
    var tipo_assessoria = parentForm.getValue('tipo_pb');
    var classe_assessoria = parentForm.getValue('classe_pb');
    var subclasse_assessoria = parentForm.getValue('subclasse_pb');
    var sequencial_assessoria = parentForm.getValue('sequencial_pb');

    var numContrato = parentForm.getValue('numeroContrato');

    // Validação
    if (tipo != tipo_assessoria ||
        classe != classe_assessoria ||
        subclasse != subclasse_assessoria ||
        sequencial != sequencial_assessoria) {

        parentForm.addErrorMessage('Item não pertence ao contrato selecionado. Favor verificar se PBMS pertence ao contrato selecionado.');

        // Limpa campos da MRVS
        g_form.clearValue('api_tipo_pb');
        g_form.clearValue('api_classe_pb');
        g_form.clearValue('api_subclasse_pb');
        g_form.clearValue('api_sequencial_pb');

    } else {

        parentForm.addInfoMessage('Tudo certo! Buscando dados do contrato...');

        var ga = new GlideAjax('x_rtb_5ll_0001110.store_verifica_contr');
        ga.addParam('sysparm_name', 'verificarContratos');
        ga.addParam('sysparm_contrato', numContrato);

        ga.getXMLAnswer(function(response) {
            var result = JSON.parse(response);

            if (result.status == 'valido') {
                parentForm.setValue('nome_prefixo_contabil', result.numero_prefixo_contabil);
                parentForm.setValue('nome_dependencia_contabil', result.dependencia_contabil);
                parentForm.setValue('conta_contabil', result.conta_contabil);
                parentForm.setValue('valor_por_equipamento', result.valor_por_equipamento);

                parentForm.addInfoMessage('Dados do contrato carregados com sucesso!');
            } else {
                parentForm.addErrorMessage(result.mesagem || result.status);
            }
        });
    }
}

The key changes are:

g_form.getParentForm() returns the parent catalog item's form object. Use it any time you need to read or write variables that live outside the MRVS — that includes getValue, setValue, addErrorMessage, and addInfoMessage calls that should appear on the main form rather than the MRVS row.

Keep using g_form (without parent) for the variables that actually belong to the MRVS row itself, like your api_tipo_pb fields.

Tanushree Maiti
Kilo Patron

Hi @Ariomar de Deus ,

 

(in English)

I second with Naveen.

When working with catalog items that use Multi-Row Variable Sets (MRVS) in ServiceNow, you may need to pass a value or update from a row in the MRVS to fields or variables on the parent record. Since MRVS scripts execute within an iframe, accessing or modifying the parent record requires using parent.g_form

 

Refer: ServiceNow Tips: Update Parent Record Variables from MRVS Selections

 

 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Ankur Bawiskar
Tera Patron

@Ariomar de Deus 

to access outside variable this is the syntax

g_service_catalog.parent.getValue('outsideVariableName')

So update as this in your script and it should work fine

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader