issue in widget code

Prithvi Ramesh1
Mega Sage

Server Side code - 

 

 

g_approval_form_request = true;

data.show_all_approvals = options.show_all_approvals == 'true';


//we get only a max number of elements to avoid to have a big list of it
var maxNumberOfItemsInTheList = parseInt(options.max_number_of_elements_shown_on_the_list);
//set 10 if maxnumber is undefined, empty or negative value
maxNumberOfItemsInTheList = maxNumberOfItemsInTheList > 0 ? maxNumberOfItemsInTheList : 50;

var initRow = 0;
var lastRow = maxNumberOfItemsInTheList || 50;
var currentPage = 0; //0 is the first page

//console.log(input);

if (input) {
    // update pagination
    currentPage = input.pagination.currentPage || 0;
    initRow = (currentPage * maxNumberOfItemsInTheList);
    lastRow = initRow + maxNumberOfItemsInTheList;

    if (input.op == 'approved' || input.op == 'rejected') {
        console.log('Comment has been updated. ' + input.op);
        var app = new GlideRecord("sysapproval_approver");
        if (app.get(input.target)) {
            console.log('Comment has been updated. ' + input.target);
            var isMine = gs.hasRole("approval_admin") || (gs.hasRole("approver_user, itil") && isApprovalMine(app));
            if (isMine) {
                if (input.comment)
                    app.comments = input.comment;
                app.state = input.op;
                app.update();
                console.log('Comment has been updated.');
            }
        }
    }
}

data.ViewApprovalPageMsg = gs.getMessage("View approval page");
data.esignature = {
    username: gs.getUserName(),
    userSysId: gs.getUserID(),
    e_sig_required: GlidePluginManager.isRegistered('com.glide.e_signature_approvals')
};

var esigRequiredMap = {};
if (data.esignature.e_sig_required) {
    var esigRegistryGR = new GlideRecord("e_signature_registry");
    esigRegistryGR.addQuery("enabled", "true");
    esigRegistryGR.query();
    while (esigRegistryGR.next()) {
        esigRequiredMap[esigRegistryGR.getValue("table_name")] = true;
    }
}

var gr = new GlideRecord('sysapproval_approver');
gr.initialize();
gr.chooseWindow(initRow, lastRow);
var selectedOption = input.view === undefined ?
    'requested' : input.view; //Contains the type of approval selected
var approvalFor = input.search_text // contains approval number

data.myApprovals = getMyApprovals();
gr.addQuery("approver", "IN", data.myApprovals);
if (!((input || {}).showAllApprovals)) {
    gr.addQuery('state', selectedOption);
    gr.addQuery('sysapproval.number', approvalFor);
} else {
    gr.addQuery('state', 'requested').addOrCondition('state', 'rejected').addOrCondition('state', 'approved').addOrCondition('state', 'canceled');
}
gr.orderByDesc("sys_created_on");
gr.query();

var rowCount = gr.getRowCount();
var approvals = [];
var ids = [];
var source_tables = [];





//Filter CODE BELOW

var localInput = input; //to safeguard pullution of "input" via BR or other scripts

var alsoRequest = false;

data.empty_message = gs.getMessage(options.empty_message || "You do not have any approvals");
data.no_match = gs.getMessage(options.no_match || "Search didn't match any requests");

data.filterMsg = gs.getMessage("Search your approvals");




//Filter code ABOVE




while (gr.next()) {

    var task = getRecordBeingApproved(gr);
    if (!task.isValidRecord())
        continue;

    ids.push(gr.getUniqueValue());
    var t = {};
    t.number = task.getDisplayValue();
    t.short_description = task.short_description.toString();
    if (gr.getValue("approver") != gs.getUserID())
        t.approver = gr.approver.getDisplayValue();
    if (task.isValidField("opened_by") && !task.opened_by.nil())
        t.opened_by = task.opened_by.getDisplayValue();

    // requestor >> opener
    if (task.isValidField("requested_by") && !task.requested_by.nil())
        t.opened_by = task.requested_by.getDisplayValue();

    t.requested_for = task.requested_for.getDisplayValue();
    t.start_date = task.start_date.getDisplayValue();
    t.end_date = task.end_date.getDisplayValue();
    t.quantity = task.quantity.getDisplayValue();
    t.table = task.getLabel();
    if (task.getValue("price") > 0)
        t.price = task.getDisplayValue("price");

    if (task.getValue("recurring_price") > 0)
        t.recurring_price = task.getDisplayValue("recurring_price");

    t.recurring_frequency = task.getDisplayValue("recurring_frequency");





    var items = [];
    var idx = 0;
    var itemsGR = new GlideRecord("sc_req_item");
    itemsGR.addQuery("request", task.sys_id);
    itemsGR.query();
    if (itemsGR.getRowCount() > 1)
        t.short_description = itemsGR.getRowCount() + " requested items";

    while (itemsGR.next()) {
        var item = {};
        item.short_description = itemsGR.short_description.toString();
        if (itemsGR.getValue("price") > 0)
            item.price = itemsGR.getDisplayValue("price");
        if (itemsGR.getValue("recurring_price") > 0) {
            item.recurring_price = itemsGR.getDisplayValue("recurring_price");
            item.recurring_frequency = itemsGR.getDisplayValue("recurring_frequency");
        }
        if (itemsGR.getRowCount() == 1) {
            item.variables = new GlobalServiceCatalogUtil().getVariablesForTask(itemsGR, true);
            t.short_description = itemsGR.short_description.toString();
        }

        items[idx] = item;
        idx++;
    }

    var j = {};
    j.sys_id = gr.getUniqueValue();
    j.table = gr.getRecordClassName();
    j.approval_source_table = gr.getValue("source_table");
    if (!j.approval_source_table)
        j.approval_source_table = gr.sysapproval.sys_class_name + "";
    j.requireEsigApproval = esigRequiredMap[j.approval_source_table];
    j.task = t;
    if (task)
        j.variables = new GlobalServiceCatalogUtil().getVariablesForTask(task, true);

    j.items = items;
    j.state = gr.getValue("state");
    j.stateLabel = gr.state.getDisplayValue();
    approvals.push(j);

}
data.ids = ids;
data.approvals = approvals;
data.showApprovals = gs.getUser().hasRole('approver_user ,itil');
// for pagination
data.pagination = {};
data.pagination.hasNext = (approvals.length == (parseInt(lastRow) - parseInt(initRow)) && lastRow < rowCount);
data.pagination.hasPrevious = parseInt(initRow) > 0;
data.pagination.from = parseInt(initRow + 1);
data.pagination.to = parseInt(lastRow) < parseInt(rowCount) ? parseInt(lastRow) : parseInt(rowCount);
data.pagination.of = parseInt(rowCount);
data.pagination.showPagination = data.pagination.hasPrevious || data.pagination.hasNext;
data.pagination.currentPage = data.pagination.from > data.pagination.to ? currentPage - 1 : currentPage;
delete g_approval_form_request;

function getRecordBeingApproved(gr) {
    if (!gr.sysapproval.nil())
        return gr.sysapproval.getRefRecord();

    return gr.document_id.getRefRecord();
}

 

 

Client Controller -

 

 

function($scope, spUtil, spUIActionsExecuter, spModal, $uibModal) {

    var ESIGNATURE = {
        TYPE: "form",
        APPROVE_SYS: "cbfe291147220100ba13a5554ee4904d",
        REJECT_SYS: "580f711147220100ba13a5554ee4904b",
        INDEXPAGE_SYS: "46cc384147532100ba13a5554ee49009"
    };

    var c = this;
    $scope.showAllApprovals = false
    $scope.toggleShowAll = function() {
        $scope.showAllApprovals = !$scope.showAllApprovals
        c.server.get({
            showAllApprovals: true
        }).then(function(response) {
            console.log('comments  ' + JSON.stringify(response.data));
            $scope.data = response.data;

        })
    }

    for (var i = c.data.approvals.length - 1; i >= 0; i--) {
        if (c.data.approvals[i].state == 'approved') {
            c.data.approvals.splice(i, 1);
        }
    }

    if ($scope.page.sys_id != ESIGNATURE.INDEXPAGE_SYS) {
        get();
    }

    if ($scope.options.portal == true || $scope.options.portal == 'true') {
        $scope.contentColClass = "col-xs-12";
        $scope.options.portal = true;
    } else {
        $scope.options.portal = false;
        $scope.contentColClass = "col-sm-8";
    }

    $scope.data.op = "";
    spUtil.recordWatch($scope, "sysapproval_approver", "state=requested^approverIN" + $scope.data.myApprovals.toString(), function(data) {

        //	 console.log(data);
        // don't double-roundtrip if update came from record just approved/rejected
        if (data.data.sys_id != $scope.data.target)
            spUtil.update($scope);
    });

    //filter Code Below

    c.viewFilter = 'requested';
    c.search = function() {
        c.server.get({
            action: 'search',
            search_text: c.filterText,
            view: c.viewFilter
        }).then(function(response) {
            c.data = response.data;
        });
    }

    c.checkEnter = function(event) {
        if (event.which === 13)
            c.search();
    }

    //c.viewFilter = 'requested';
    c.changeView = function() {
        c.server.get({
            action: 'change',
            search_text: c.filterText,
            view: c.viewFilter
        }).then(function(response) {
            c.data = response.data;
        });
    }


    //filter code Above 

    function get() {
        spUtil.update($scope);
    }

    c.showQuestion = function(id, esigRequired, state) {
        c.esigRequired = esigRequired;
        c.approvalId = id;
        c.data.isRejected = '';
        c.data.newState = state;
        if (state == 'rejected') c.data.isRejected = 'true';
        c.modalInstance = $uibModal.open({
            template: '<div class="modal-content" ><div class="modal-header ng-scope" ng-style="options.headerStyle"> <button type="button" class="close" ng-click="c.closeModal()">&times;</button>  <h1 class="modal-title h4 ng-binding" ng-if="c.data.isRejected">{{::c.options.rejection_title}} </h1> <h1 class="modal-title h4 ng-binding" ng-if="!c.data.isRejected">{{::c.options.approval_title}} </h1></div> <form name="form.xpForm" ng-submit="submit()" class="ng-pristine ng-scope ng-invalid ng-invalid-required" role="form"> <div class="form-group" style="margin: 25px;"> <label for="com" ng-if="c.data.isRejected"> <span class="fa fa-asterisk mandatory" style="padding-right: .25em" aria-label="Required "></span> </label> <input id="com"  class="form-control ng-pristine ng-scope ng-empty ng-invalid  ng-invalid-required ng-touched" type="input"  ng-model="comment" ng-required="c.data.isRejected" autofocus> </div> </form>	<div class="panel-footer text-right"> <button class="btn btn-primary" ng-click="c.closeModal()">${Cancel}</button>	<button ng-disabled="form.xpForm.$invalid"  sp-focus-if="button.focus"  class="btn btn-default btn-primary" ng-if="c.data.isRejected" ng-click="c.reject(c.approvalId, c.esigRequired, comment)" >OK</button> <button ng-disabled="form.xpForm.$invalid"  sp-focus-if="button.focus"  class="btn btn-default btn-primary" ng-if="!c.data.isRejected"  ng-click="c.approve(c.approvalId, c.esigRequired, comment)" >OK</button></div></div>',
            scope: $scope
        });
    }

    c.closeModal = function() {
        c.modalInstance.close();
    }

    c.approve = function(id, esigRequired, comment) {
        var requestParams = {
            username: $scope.data.esignature.username,
            userSysId: $scope.data.esignature.userSysId
        };

        c.data.op = "approved";
        c.data.comment = comment;
        console.log("Approval Comment: " + comment);
        console.log("Approval sys_id: " + id); // Log sys_id
        if ($scope.data.esignature.e_sig_required && esigRequired) {
            spUIActionsExecuter.executeFormAction(ESIGNATURE.APPROVE_SYS, "sysapproval_approver", id, [], "", requestParams).then(function(response) {});
        } else {
            if (c.options.approval_comment) c.closeModal();
            $scope.data.op = "approved";
            $scope.data.target = id;
            get();
        }
    }



    c.reject = function(id, esigRequired, comment) {
        var requestParams = {
            username: $scope.data.esignature.username,
            userSysId: $scope.data.esignature.userSysId
        };
        $scope.data.target = id;
console.log($scope.data.target);
        c.data.op = "rejected";
        c.data.comment = comment;
        console.log("Approval Comment: " + comment);
        console.log("Approval sys_id: " + id); // Log sys_id
        if ($scope.data.esignature.e_sig_required && esigRequired) {
            spUIActionsExecuter.executeFormAction(ESIGNATURE.REJECT_SYS, "sysapproval_approver", id, [], "", requestParams).then(function(response) {});
        } else {
            if (c.options.approval_comment) c.closeModal();
            $scope.data.op = "rejected";
            $scope.data.target = id;
            get();
        }
    }

    $scope.filterByApproved = function($event) {
        if ($scope.data.showApproved) get();
        for (var i = c.data.approvals.length - 1; i >= 0; i--) {
            if (!$scope.data.showApproved && c.data.approvals[i].state == 'approved') {
                c.data.approvals.splice(i, 1);
            }
        }
    }

    // pagination
    $scope.previousPage = function() {
        if ($scope.data.pagination.currentPage > 1)
            $scope.data.pagination.currentPage = $scope.data.pagination.currentPage - 1;
        else
            $scope.data.pagination.currentPage = 0;

        get();
    }

    $scope.nextPage = function() {
        $scope.data.pagination.currentPage = $scope.data.pagination.currentPage + 1;
        get();
    }
}

 

 

Whenever i reject the change then after filing the e - signature then the change is rejected but the comments is not visible in the native view.

0 REPLIES 0