Custom Request Commands
Creating a new command
In the folder where ALVAO Service Desk WebService is installed, copy the CodeExtension_Template.cs file in the App_Code folder and rename it according to the functionality of the custom command.
In the newly created file, enter a definition of the new command into the bordered code block by implementing the ICommand interface and set the values for the id, name, and position properties in the command class constructor.
- id – command number (unique)
- name – command name displayed in the applications
- position – position at which the command will be displayed in the applications
- 1 = on the first place of the request main menu
- 2 = on the last place of the request main menu
- 3 = on the first place of a menu nested in the request main menu
- 4 = on the last place of a menu nested in the request main menu
Example:
… class Command1 : ICommand { CommandDesc commandDesc;
public CommandDesc CommandDesc { get { return commandDesc; } set { commandDesc = value; } }
public Command1() { int id = 1; string name = "Calculate total estimated work"; int position = 1;
commandDesc = new CommandDesc(id, name, position); } } …
Defining conditions for displaying the command
In the created command class, implement the Show method from the ICommand interface. Input parameters are ticketId (request number) and personId (id of a person who should see the command).
Output is then a truth value representing whether the command should display to the user on the entered request, or not.
Tip: By calling this method in the Run(…) method you can check before running the query, whether the conditions for its displaying changed in the period between displaying and running the query, or not.
Example:
… class Command1: ICommand { … public bool Show(SqlConnection Con, int ticketId, int personId) { bool show = false;
// loading the required data about request from the database SqlCommand Cmd; Cmd = new SqlCommand(@"SELECT liHdTicketHdSectionId, sHdTicketStateNotice, liHdTicketSolverPersonId FROM tHdTicket WHERE liHdTicketSolverPersonId IS NOT NULL AND iHdTicketId = @iHdticketId", Con); Cmd.Parameters.Add("@iHdTicketId", SqlDbType.Int).Value = ticketId;
SqlDataReader ticketReader = Cmd.ExecuteReader();
int sectionId; // id of the service where the request is string state; // request status int solverId; // request solver id
if (ticketReader.Read()) { sectionId = int.Parse((ticketReader["liHdTicketHdSectionId"]).ToString()); state = (ticketReader["sHdTicketStateNotice"]).ToString(); solverId = int.Parse((ticketReader["liHdTicketSolverPersonId"]).ToString()); ticketReader.Close(); } else { ticketReader.Close(); return show; }
// checking whether the request is in the requested service, in the Solution status, and the solver is the user who the request should be displayed to, or not if (sectionId == 1 && state == "Solution"&& solverId == personId) show = true;
return show; }
// only copy this part of the code public List<Tuple<int, bool>> Show(SqlConnection Con, List<int> ticketsIds, int personId) { throw new NotImplementedException(); } … } …
Defining actions performed by the command
In the created command class, implement the Run method from the ICommand interface. Input parameters are ticketId (request number) and personId (id of a person who is running the command).
The output is a truth value based on the success of execution, a text which can be displayed in the message for the user, and a URL to open in a browser.
Example:
… class Command1: ICommand { … public Tuple<bool, string, string> Run(SqlConnection Con, int ticketId, int personId) { bool complete = false; string message = ""; string url = "";
// Check, whether the conditions for running/displaying the command changed in the period between displaying and running it, or not (can be performed anywhere in this Run method).
if (!Show(Con, ticketId, personId)) { message = "Command can not be performed as the conditions for its displaying has not been met."; complete = false; return Tuple.Create(complete, message, url); }
complete = true; SqlTransaction transaction = Con.BeginTransaction(); try { HelpdeskWebService sdws = new HelpdeskWebService();
// loading values from the “SW costs” and “HW costs” custom items string strCostSW = sdws.ReadColumn(ticketId, "tHdTicketCust", "costSW"); int costSW = string.IsNullOrEmpty(strCostSW) ? 0 : int.Parse(strCostSW); string strCostHW = sdws.ReadColumn(ticketId, "tHdTicketCust", "costHW"); int costHW = string.IsNullOrEmpty(strCostHW) ? 0 : int.Parse(strCostHW); int costTotal = costSW + costHW;
// writing the sum of costSW and costHW into the Total costs custom item sdws.WriteColumn(ticketId, "tHdTicketCust", "costTotal", costTotal.ToString());
transaction.Commit();
return Tuple.Create(complete, message, url); } catch (Exception ex) { message = "Error when writing the value."; complete = false; url = ""; transaction.Rollback(); return Tuple.Create(complete, message, url); } } … } …
Tip: If the custom command should open another page from ALVAO WebApp, use the GetServiceDeskPortalUrl() method from the Service Desk Web Service interface to find the root URL of WebApp. E.g.: string waUrl = ""; using(HelpdeskWebService ws = new HelpdeskWebService()) { waUrl = ws.GetServiceDeskPortalUrl(); } if (waUrl.Length == 0) throw new Exception("WebApp address not set.");
Did not find what you were looking for? Ask our technical support team.
|