<?xml version="1.0"?>
<AlvaoApplication xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ModelVersion="1">
  <Applications>
    <Application id="1">
      <Name>Ticket close - force</Name>
      <Scripts>
        <Script id="1">
          <Name>TicketClose</Name>
          <Code>using System;
using System.Data;
using Alvao.Global;
using Alvao.API.Common;
using Alvao.API.Common.Model.CustomApps;
using Alvao.Apps.API;
using Alvao.API.Common.Model;
using Alvao.API.Common.Model.Database;
using Alvao.API.SD.Model;
using Dapper;
using Alvao.Context;

public class TicketClose : IEntityCommand 
{
    public string Id {get; set;}
    public Entity Entity {get; set;}

    public TicketClose()
    {
        Id = "Ticket_close_force";
        Entity =  Entity.Request;
    }

    public EntityCommandShowResult Show(int entityId, int personId)
    {   
            int position = 1; //Position (1-4)
            string icon =  "AcceptMedium"; //Fabric icon
            string name =  "Close ticket - force"; //Command name 
            bool show =    Settings.TicketIds.Contains(entityId); // Set if command should show for person
            return new EntityCommandShowResult (show, name, icon, position);
    }

    public CommandResult Run(int entityId, int personId)
    {
            var systemPerson =  Alvao.API.Common.Person.GetSystem();
                        MessageType messageType = MessageType.None; // MessageType.None, MessageType.Info, MessageType.Warning, MessageType.Error
            string messageText = ""; //message that is displayed to the user after the command is executed
            string navigateToUrl = ""; //url that opens after executing the command
            foreach (int id in Settings.TicketIds)
            {
                try {
                    var ticket = Alvao.API.SD.Ticket.GetById(id);
                    if (Alvao.API.SD.TicketState.IsAdHocApproval(ticket.TicketStateId)) {
                                 string sql = @"select iHdTicketApprovalItemId from tHdTicketApprovalItem where liHdTicketApprovalItemHdTicketApprovalId = (select ApprovalId from tHdTicket where iHdTicketId = @id)";
                        using (var scope = AlvaoContext.GetConnectionScope())
                        {
                            var approvalItemIds = scope.Connection.Query&lt;int&gt;(sql, new {id});
                            Alvao.API.SD.Approval.CancelItems(id, approvalItemIds);
                        }
                 
                    } else {
                        Alvao.API.SD.Approval.Cancel(id);
                    }
                } catch(Exception e) {
                    messageType = MessageType.Info; // MessageType.None, MessageType.Info, MessageType.Warning, MessageType.Error
                    messageText = e.Message; //message that is displayed to the user after the command is executed
                    navigateToUrl = ""; //url that opens after executing the command
                   // return new CommandResult(messageType, messageText, navigateToUrl);
                }
                try {
                    HtmlTextModel message = new HtmlTextModel(null);
                    Alvao.API.SD.Ticket.Resolve(id, systemPerson.iPersonId, message, null, null, Alvao.API.SD.Ticket.ResolveFlags.IgnoreRights | Alvao.API.SD.Ticket.ResolveFlags.DontNotifyUser);
                } catch(Exception e) {
                    messageType = MessageType.Info; // MessageType.None, MessageType.Info, MessageType.Warning, MessageType.Error
                    messageText = e.Message; //message that is displayed to the user after the command is executed
                    navigateToUrl = ""; //url that opens after executing the command
                   // return new CommandResult(messageType, messageText, navigateToUrl);
                }
                try {
                    Alvao.API.SD.Ticket.Close(id, systemPerson.iPersonId, Alvao.API.SD.Ticket.CloseFlags.IgnoreRights);
                } catch(Exception e) {
                    messageType = MessageType.Info; // MessageType.None, MessageType.Info, MessageType.Warning, MessageType.Error
                    messageText = e.Message; //message that is displayed to the user after the command is executed
                    navigateToUrl = ""; //url that opens after executing the command
                  //  return new CommandResult(messageType, messageText, navigateToUrl);
                }
            }
        messageType = MessageType.None; // MessageType.None, MessageType.Info, MessageType.Warning, MessageType.Error
        messageText = ""; //message that is displayed to the user after the command is executed
        navigateToUrl = ""; //url that opens after executing the command
        return new CommandResult(messageType, messageText, navigateToUrl);
    }
}
</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
        <Script id="2">
          <Name>Settings</Name>
          <Code>using System.Collections.Generic;
public static class Settings
{
    public static readonly List&lt;int&gt; TicketIds = new List&lt;int&gt;() {
         14, 13, 12
    };
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>