Comment for rejection is saving twice in service portal

Gab
Tera Expert

Hi, the comment is saving two times whenever i try to reject an approval in service portal. Below is the details of Approval info widget (cloned).

Server script: 

(function() {
data.actionPreventedMsg = gs.getMessage("Update failed");
var gr = $sp.getRecord();
if (gr == null || !gr.isValid()) {
data.isValid = false;
return;
}

data.isValid = true;
data.isMine = isApprovalMine(gr);

var approverDisplay = gr.approver.getDisplayValue();
if (!data.isMine && approverDisplay)
data.approverDisplay = approverDisplay;

if (approverDisplay) {
data.approvedMsg = gs.getMessage("Approved by {0}", approverDisplay);
data.rejectedMsg = gs.getMessage("Rejected by {0}", approverDisplay);
} else {
data.approvedMsg = gs.getMessage("Approved");
data.rejectedMsg = gs.getMessage("Rejected");
}
if (input && input.op && data.isMine) {
gr.state = input.op;
if (gr.state == "rejected") {
gr.comments = input.comments; //Custom code (DFCT0010123)
}
data.updateID = gr.update();
if (GlideStringUtil.nil(data.updateID)) {
// update failed so fetch again for correct values
gr = $sp.getRecord();
}
data.op = "";
}

var fields = $sp.getFields(gr, 'state,sys_created_on');

if (gr.sys_mod_count > 0)
fields.push($sp.getField(gr, 'sys_updated_on'));

data.fields = fields;
data.state = gr.state.toString();
data.sys_updated_on = gr.sys_updated_on.toString();
data.sys_id = gr.getUniqueValue();
data.table = gr.getTableName();
data.label = getRecordBeingApproved(gr).getLabel();
data.esignature = {
username: gs.getUserName(),
userSysId: gs.getUserID(),
e_sig_required: checkESig(gr)
};
function checkESig(approvalGR) {
var esigRegistryGR = new GlideRecord("e_signature_registry");
if (!esigRegistryGR.isValid())
return false;

var table = approvalGR.getValue("source_table");
if (!table)
table = approvalGR.sysapproval.sys_class_name;
if (!table)
return false;

esigRegistryGR.addQuery("enabled", "true");
esigRegistryGR.addQuery("table_name", table);
esigRegistryGR.query();
return esigRegistryGR.hasNext();
}

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

return gr.document_id.getRefRecord();
}
})();

 

Client:

function ($scope, spUIActionsExecuter, spUtil, spModal) {
var c = this;

var ESIGNATURE = {
"approved": "cbfe291147220100ba13a5554ee4904d",
"rejected": "580f711147220100ba13a5554ee4904b"
};

spUtil.recordWatch($scope, "sysapproval_approver", "state=requested^sys_id="+ c.data.sys_id);

c.action = function(state) {
if(c.data.esignature.e_sig_required) {
var requestParams = {
username: c.data.esignature.username,
userSysId: c.data.esignature.userSysId
};
spUIActionsExecuter.executeFormAction(ESIGNATURE[state], "sysapproval_approver" , c.data.sys_id, [] , "", requestParams).then(function(response) {
});
} else {
if (state == "rejected") {
spModal.prompt("Provide a reason for rejecting this approval").then(function(newComments) {
c.data.comments = newComments;
c.data.op = state;
c.server.update().then(function() {
if (!c.data.updateID) // update failed
spUtil.addErrorMessage(c.data.actionPreventedMsg);
else
c.data.state = state;
});
});
}
else
{
c.data.op = state;
c.server.update().then(function() {
if (!c.data.updateID) // update failed
spUtil.addErrorMessage(c.data.actionPreventedMsg);
else
c.data.state = state;
});
}
}
}
}

 

Screenshots:

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

Subrahmanyam2
Giga Guru

Hi Gab,

Remove the business rules if you implemented any of them to avoid the duplicate updates for this issue.

 

The issue seems to be happening because of the record watcher observing the approval record.

You may comment the below line in client script and validate.

spUtil.recordWatch($scope, "sysapproval_approver", "state=requested^sys_id=" + c.data.sys_id);

 

Alternatively, you can keep record watcher and change the server side and client side code as mentioned below.

Widget Server Side Script:

(function() {
    data.actionPreventedMsg = gs.getMessage("Update failed");
    var gr = $sp.getRecord();
    if (gr == null || !gr.isValid()) {
        data.isValid = false;
        return;
    }

    data.isValid = true;
    data.isMine = isApprovalMine(gr);

    var approverDisplay = gr.approver.getDisplayValue();
    if (!data.isMine && approverDisplay)
        data.approverDisplay = approverDisplay;

    if (approverDisplay) {
        data.approvedMsg = gs.getMessage("Approved by {0}", approverDisplay);
        data.rejectedMsg = gs.getMessage("Rejected by {0}", approverDisplay);
    } else {
        data.approvedMsg = gs.getMessage("Approved");
        data.rejectedMsg = gs.getMessage("Rejected");
    }
    if (input && input.op && data.isMine && gr.state != "rejected") {
        gr.state = input.op;
        if (gr.state == "rejected") {
            gr.comments = input.comments; //Custom code (DFCT0010123)
        }
        data.updateID = gr.update();
        if (GlideStringUtil.nil(data.updateID)) {
            // update failed so fetch again for correct values
            gr = $sp.getRecord();
        }
        data.op = "";
    }

    var fields = $sp.getFields(gr, 'state,sys_created_on');

    if (gr.sys_mod_count > 0)
        fields.push($sp.getField(gr, 'sys_updated_on'));

    data.fields = fields;
    data.state = gr.state.toString();
    data.sys_updated_on = gr.sys_updated_on.toString();
    data.sys_id = gr.getUniqueValue();
    data.table = gr.getTableName();
    data.label = getRecordBeingApproved(gr).getLabel();
    data.esignature = {
        username: gs.getUserName(),
        userSysId: gs.getUserID(),
        e_sig_required: checkESig(gr)
    };

    function checkESig(approvalGR) {
        var esigRegistryGR = new GlideRecord("e_signature_registry");
        if (!esigRegistryGR.isValid())
            return false;

        var table = approvalGR.getValue("source_table");
        if (!table)
            table = approvalGR.sysapproval.sys_class_name;
        if (!table)
            return false;

        esigRegistryGR.addQuery("enabled", "true");
        esigRegistryGR.addQuery("table_name", table);
        esigRegistryGR.query();
        return esigRegistryGR.hasNext();
    }

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

        return gr.document_id.getRefRecord();
    }
})();

 

And client side script as below:

function($scope, spUIActionsExecuter, spUtil, spModal) {
    var c = this;

    var ESIGNATURE = {
        "approved": "cbfe291147220100ba13a5554ee4904d",
        "rejected": "580f711147220100ba13a5554ee4904b"
    };

    spUtil.recordWatch($scope, "sysapproval_approver", "state=requested^sys_id=" + c.data.sys_id);
    c.approvalInProgress = false;
    c.action = function(state) {
        if (c.data.esignature.e_sig_required) {
            var requestParams = {
                username: c.data.esignature.username,
                userSysId: c.data.esignature.userSysId
            };
            spUIActionsExecuter.executeFormAction(ESIGNATURE[state], "sysapproval_approver", c.data.sys_id, [], "", requestParams).then(function(response) {});
        } else {
            if (state == "rejected") {
                c.approvalInProgress = true;
                spModal.prompt("Provide a reason for rejecting this approval").then(function(newComments) {
                    c.data.comments = newComments;
                    c.data.op = state;
                    c.approvalInProgress = false;
                    c.server.update().then(function() {
                        if (!c.data.updateID) // update failed
                            spUtil.addErrorMessage(c.data.actionPreventedMsg);
                        else
                            c.data.state = state;
                    });
                });
            } else {
                c.approvalInProgress = true;
                c.data.op = state;
                c.server.update().then(function() {
                    c.approvalInProgress = false;
                    if (!c.data.updateID) // update failed
                        spUtil.addErrorMessage(c.data.actionPreventedMsg);
                    else
                        c.data.state = state;
                });
            }
        }
    }
}

 

 

In the client side code, I added c.approvalInProgress assignment lines, because using this OOB widget is making sure that, if the end user clicked on the Approve or Reject button till the time server processes and responds, system is not letting user click on the buttons again. So it is trying to avoid duplicate actions.

Due to this reason, I added them again in the client script code.

 

Hope this helps!

Please mark this as correct solution if this works for you.

 

Thanks and regards,

Subrahmanyam Satti

View solution in original post

11 REPLIES 11

Community Alums
Not applicable

Hi gab,

Have you checked before insert/update business rule?

You can set the condition on BR:

Additional comment changes

State -Is one of- other than rejected

Hi, what should i do in action tab?

Community Alums
Not applicable

Nothing in action Tab.

No Additional Comments in the table, I tried the Comments field but it's not working.

find_real_file.png

find_real_file.png