Sharepoint - Approve listitem task from button with javascript

How to approve workflow task item using JSOM

The following example demonstrates how to approve task item using JSOM

function approveTask(listTitle,itemId, success,error){
    var ctx = SP.ClientContext.get_current();
    var list = ctx.get_web().get_lists().getByTitle(listTitle);
    var item = list.getItemById(itemId);
    item.set_item('Completed',true);
    item.set_item('PercentComplete',1);
    item.set_item('Status','Approved');
    item.set_item('WorkflowOutcome','Approved');
    item.update();
    ctx.executeQueryAsync(
      function() {
          success();
      },
      error
    );
}

Example

approveTask('Workflow Tasks',1,function(){
    console.log('Task has been approved');
},
function(sender,args){
    console.log(args.get_message());
});

Tags:

Workflow

Tasks