RangerMSP Contact by Date

This example uses a TimeSpan of two hours to determine the date two hours prior to the runtime of the code. The purpose is to identify the Contacts created during the last two hour. 

Alternatively if it is preferred to use a fixed date, then on Line 37 change the value to be equal to a string. The syntax required of the date field in RangerMSP is in the comment on Line 37. 

The output of this code snippet displays a table. If the Account doesn’t have a company name, then the contact is considered to be a residential customer. 

This example is written using .NET Core C#

.NET Core C#
using System;
using System.ServiceModel;

namespace DemoRangerMSPContact_Core
{
    class Program
    {
        static void Main(string[] args)
        {
            // A contact is stored in the Account table. The difference between an Account and a Contact is determined by the AccountType column
            // Account Type "Account"   = an Account
            // Account Type "*"         = a Contact
            RangerMSPService.AccountClient accountClient = new RangerMSPService.AccountClient();
            (accountClient.ChannelFactory.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue;
            (accountClient.ChannelFactory.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue;
            (accountClient.ChannelFactory.Endpoint.Binding as BasicHttpBinding).ReceiveTimeout = new TimeSpan(0, 10, 0);
            RangerMSPService.Select select = new RangerMSPService.Select
            {
                Link = "and",
                RecordLimit = 0,
                Criterias = new RangerMSPService.Criteria[1]
                {
                        new RangerMSPService.Criteria
                        {
                            Field = "",
                            Operation = "opGreaterThanOrEqual",
                            Value = ""
                        }
                }
            };

            DateTime currentTime = DateTime.Now;
            TimeSpan sinceLastRun = new TimeSpan(2, 0, 0);
            DateTime searchTime = currentTime.Subtract(sinceLastRun);
            Console.WriteLine(searchTime);
            select.Criterias[0].Field = "CreationDate";
            select.Criterias[0].Value = searchTime.ToString(); // "01/09/2018  11:40 AM";
            select.RecordLimit = 100;                    // Define max number of records to retrieve. 
            Console.WriteLine(String.Format("{0,20} {1,20} {2,20} {3,20}","CompanyName","AccountType","Contact","CreationDate"));
            RangerMSPService.AccountRecord[] accountRecord = accountClient.SelectAsync(select).Result;
            foreach (RangerMSPService.AccountRecord record in accountRecord)
            {
                string companyName = record.CompanyName;
                if (companyName == "")
                {
                    companyName = "RESIDENTAL";
                }
                string creationDate = record.CreationDate;
                string contact = record.Contact;
                string acctType = record.AccountType;
                DateTime parsedDate = DateTime.Parse(creationDate);
                Console.WriteLine(String.Format("{0,20} {1,20} {2,20} {3,20}", companyName, acctType, contact, parsedDate));
            }
            Console.WriteLine("Press enter to continue.");
            Console.ReadLine();
        }
    }
}