RangerMSP Ticket Charge Data Import

The C# code below will insert a Account record, Ticket record and Charge record into a RangerMSP database. A Charge record must have a Ticket Record ID or Contract Record ID included in the data for the Charge record. Similar for a Ticket Record, which cannot be inserted with the Account Record ID being included. 

Therefore is the Account record and the Ticket record isn’t already in the RangerMSP database, then those must be inserted first.

string Address = "http://localhost:11111/RangerMSP";
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;
ChannelFactory<RangerMSP.IAccount> factory = new ChannelFactory<RangerMSP.IAccount>(binding, Address);
RangerMSP.IAccount accountChannel = factory.CreateChannel();
RangerMSP.AccountRecord accountRecord = new RangerMSP.AccountRecord();
accountRecord.CompanyName = "Customer"; // Put your customer namer here
int accountInsert = accountChannel.Insert(accountRecord);
RangerMSP.Select select = new RangerMSP.Select
    {
        Link = "and",
        RecordLimit = 100,
        Criterias = new RangerMSP.Criteria[1]
        {
            new RangerMSP.Criteria
            {
                Field = "",
                Operation = "opEqual",
                Value = ""
            }
        }
    };
RangerMSP.AccountRecord[] accountRecords = null;
select.Criterias[0].Field = "CompanyName";
select.Criterias[0].Value = textBox1.Text;
accountRecords = accountChannel.Select(select);
string acctRECID = accountRecords[0].AccountREC_ID;            

ChannelFactory<RangerMSP.ITicket> ticketFactory = new ChannelFactory<RangerMSP.ITicket>(binding, Address);
RangerMSP.ITicket ticketChannel = ticketFactory.CreateChannel();
RangerMSP.TicketRecord ticketRecord = new RangerMSP.TicketRecord();
ticketRecord.AccountREC_ID = acctRECID;
ticketRecord.Description = "Problem to fix"; // Description for your ticket
int ticketInsert = ticketChannel.Insert(ticketRecord);

ChannelFactory<RangerMSP.ICharge> chargeFactory = new ChannelFactory<RangerMSP.ICharge>(binding, Address);
RangerMSP.ICharge chargeChannel = chargeFactory.CreateChannel();
RangerMSP.ChargeRecord chargeRecord = new RangerMSP.ChargeRecord();
select = new RangerMSP.Select
    {
        Link = "and",
        RecordLimit = 100,
        Criterias = new RangerMSP.Criteria[2]
        {
            new RangerMSP.Criteria
            {
                Field = "",
                Operation = "opEqual",
                Value = ""
            },
            new RangerMSP.Criteria
            {
                Field = "",
                Operation = "opEqual",
                Value = ""
            }
        }
    };
RangerMSP.TicketRecord[] ticketRecords = null;
select.Criterias[0].Field = "Description";
select.Criterias[0].Value = ticketRecord.Description;
select.Criterias[1].Field = "AccountREC_ID";
select.Criterias[1].Value = acctRECID;
ticketRecords = ticketChannel.Select(select);
string ticketRECID = ticketRecords[0].TicketREC_ID;
chargeRecord.ChargedItem = "Labour-34XD0"; // The unique RangerMSP item code that your charge is for
chargeRecord.TicketREC_ID = ticketRECID;
chargeRecord.Units_Hours = Convert.ToString(5);
int chargeInsert = chargeChannel.Insert(chargeRecord);