Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Widget not working correctly

Ariomar de Deus
Tera Contributor

I have a widget that displays a button when my variable is true, but this button isn't appearing even when my condition is true.
The variable in question is trigger_evolution_survey, and it's of type true/false.

I've provided my HTML body template and the server script. Could you help me with this problem?

 

Body HTML Template

<div class="panel panel-default" ng-if="data.trigger_evolution_survey">
<div class="panel-body">
<a class="btn btn-primary btn-block" ng-click="redirectSurvey()">Resultado pesquisa de satisfação</a>
</div>
</div>
 
<div ng-if="c.showPopup" class="modal fade in">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Sem avaliação</h5>
</div>
<div class="modal-body">
<p>Fale sem avaliação do solicitante.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" ng-click="closeModal()">Fechar</button>
</div> 
</div>
</div>
</div>

 

 

Server Script

(function() {

    data.table = $sp.getParameter('table');

    data.sys_id = $sp.getParameter('sys_id');

    data.complete = true;

    data.urlSurvey = '';

    data.trigger_evolution_survey = false; // novo campo
 
    var task = new GlideRecord(data.table);

    if (task.get(data.sys_id)) {

        data.trigger_evolution_survey = task.getValue('trigger_evolution_survey') == 'true'; // converte para booleano

    }
 
    var instance = new GlideRecord('asmt_assessment_instance');

    instance.addQuery('trigger_id', data.sys_id);

    instance.query();
 
    if (instance.next()) {

        if (instance.getValue('state') != 'complete')

            return data.complete = false;
 
        data.urlSurvey = "/bbstore?id=survey_result&ans=" + instance.getValue('sys_id');

    }

})();

 
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Ariomar de Deus 

try this

HTML:

<div class="panel panel-default" ng-if="data.trigger_evolution_survey === true || data.trigger_evolution_survey === 'true'">
    <div class="panel-body">
        <a class="btn btn-primary btn-block" ng-click="redirectSurvey()">Resultado pesquisa de satisfação</a>
    </div>
</div>

<div ng-if="c.showPopup" class="modal fade in">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Sem avaliação</h5>
            </div>
            <div class="modal-body">
                <p>Fale sem avaliação do solicitante.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" ng-click="closeModal()">Fechar</button>
            </div> 
        </div>
    </div>
</div>

Server:

(function() {
    data.table = $sp.getParameter('table');
    data.sys_id = $sp.getParameter('sys_id');
    data.complete = true;
    data.urlSurvey = '';
    data.trigger_evolution_survey = false; // novo campo

    var task = new GlideRecord(data.table);
    if (task.get(data.sys_id)) {
        data.trigger_evolution_survey = task.getValue('trigger_evolution_survey') === 'true'; // converte para booleano
    }

    var instance = new GlideRecord('asmt_assessment_instance');
    instance.addQuery('trigger_id', data.sys_id);
    instance.query();

    if (instance.next()) {
        if (instance.getValue('state') != 'complete') {
            return data.complete = false;
        }
        data.urlSurvey = "/bbstore?id=survey_result&ans=" + instance.getValue('sys_id');
    }
})();

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Nawal Singh
Tera Guru

Hi @Ariomar de Deus ,

Please review below code - 

 

<!-- Show button only when trigger_evolution_survey = true -->
<div class="panel panel-default" ng-if="data.trigger_evolution_survey">
    <div class="panel-body">
        <a class="btn btn-primary btn-block" ng-click="redirectSurvey()">
            Resultado pesquisa de satisfação
        </a>
    </div>
</div>

<!-- Popup Modal -->
<div ng-if="c.showPopup" class="modal fade in">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <h5 class="modal-title">Sem avaliação</h5>
            </div>

            <div class="modal-body">
                <p>Fale sem avaliação do solicitante.</p>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-primary" ng-click="closeModal()">Fechar</button>
            </div>

        </div>
    </div>
</div>

 

and server script- 

 

(function() {

    data.table = $sp.getParameter('table');
    data.sys_id = $sp.getParameter('sys_id');

    data.complete = true;
    data.urlSurvey = '';
    data.trigger_evolution_survey = false;

    // Load the task record
    var task = new GlideRecord(data.table);
    if (task.get(data.sys_id)) {

        // Boolean fields in ServiceNow return "1" or "0"
        // getBooleanValue() converts this automatically
        data.trigger_evolution_survey = task.getBooleanValue('trigger_evolution_survey');
    }

    // Find survey instance
    var instance = new GlideRecord('asmt_assessment_instance');
    instance.addQuery('trigger_id', data.sys_id);
    instance.query();

    if (instance.next()) {

        // If survey is not complete → do not show the result button
        if (instance.getValue('state') != 'complete') {
            data.complete = false;
            return;
        }

        // Build link to survey result
        data.urlSurvey = "/bbstore?id=survey_result&ans=" + instance.getValue('sys_id');
    }

})();

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh