Code Example (Creating a new client)


The code below is a quick example of how to use the API to create a client, delivery account, delivery method, and order.

This code is assuming you are using VisualStudio.NET (visualstudio.com) and that you have added the API as a web reference using the namespace LeadExec.API.

//Create the API service object
var apiService = new LeadExec.API.Service();

//Create an array of fields for the new client
var clientFields = new[]
{
    //Company
    new ClientFieldType() {ClientFieldUID = 1, FieldValue = "ABC Company"},
    //First Name
    new ClientFieldType() {ClientFieldUID = 2, FieldValue = "John"},
    //Last Name
    new ClientFieldType() {ClientFieldUID = 3, FieldValue = "Doe"},
    //Email
    new ClientFieldType() {ClientFieldUID = 4, FieldValue = "jon.doe@email.com"},
    //Phone Number
    new ClientFieldType() {ClientFieldUID = 5, FieldValue = "7025551212"},
    //Address
    new ClientFieldType() {ClientFieldUID = 9, FieldValue = "1234 Main St."},
    //City
    new ClientFieldType() {ClientFieldUID = 10, FieldValue = "Las Vegas"},
    //State (ID of state, refer to state API)
    new ClientFieldType() {ClientFieldUID = 11, FieldValue = "29"},
    //Zip
    new ClientFieldType() {ClientFieldUID = 12, FieldValue = "89015"}
};

//Create the info object that will set the properties for the client
var clientInfo = new ClientType()
{
    ClientUID = -1,
    DateAdded = DateTime.Now,
    Notes = "New client from API test",
    Password = "[password]",
    Status = ClientStatus.New,
    TimeOffset = -8,
    TimeZoneName = "Pacific Standard Time",
    UserUID = 0,
    Username = "[username]",
    ResellerUID = 0
};

//Check to make sure that the new username is unique and not already in use
var result = apiService.isUsernameUnique("[APIKey]", "[username]");

//If not in use proceed with creating the new client
if (result)
{
    //Use the API service to add the new client
    var clientUid = apiService.AddEditClient("[APIKey]", clientInfo, clientFields);

    //Create a new delivery method for the client (be sure to do this before the delivery account)
    var deliveryMethod = new DeliveryType()
    {
        ClientUID = clientUid,
        Status = true,
        DateAdded = DateTime.Now,
        ClientDeliveryUID = -1,
        Type = 8,
        inTesting = false,
        LeadTypeUID = 1
    };

    //Use the API service to add the new delivery method to the client
    var deliveryUid = apiService.AddEditClientDelivery("[APIKey]", deliveryMethod);

    //Create a new delivery account for the client
    var deliveryAccount = new ClientAccountType()
    {
        Status = 12,
        ClientUID = clientUid,
        AccountType = 0,
        DayMax = -1,
        HourMax = -1,
        MonthMax = -1,
        Price = 25,
        DeliveryUID = deliveryUid,
        Description = "Delivery Account from API",
        //Setting to true to show an example of how to use orders
        useOrder = true,
        ClientAccountUID = -1,
        AutomationEnabled = true,
        Criteria = new[]
    {
        new ClientAccountCriteriaType()
        {
		ClientCriteriaUID = -1,
		Type = CriteriaType.FieldValue,
		Operator = CriteriaOperator.Equal,
		LeadFieldUID = 1,
		Value = "Test"
        }
    },
        DeliveryDelay = 0
    };

    //Use the API service to add the new delivery account to the client
    var deliveryAccountUid = apiService.AddEditClientAccount("[APIKey]", deliveryAccount);

    //Create a new order for the client and delivery account created
    var clientOrder = new ClientOrderType()
    {
        Status = OrderStatus.Open,
        ClientUID = clientUid,
        AutoCharge = false,
        ClientOrderUID = -1,
        //Setting the end date to DateTime.MinValue tells the system to not have an end date
        EndDate = DateTime.MinValue,
        Items = new[]
    {
        new OrderItemType()
        {
		ClientAccountUID = deliveryAccountUid,
		ClientOrderItemUID = -1,
		ClientOrderUID = -1,
		ConstrainQuantity = true,
		Price = 250,
		Qty = 250
        }
    },
        //This quantity should be the sum of all items
        Qty = 250,
        Renewal = OrderRenewal.No_Renewal,
        StartDate = DateTime.Now
    };

    //Use the API service to add the new order under the client
    var orderUid = apiService.AddEditClientOrder("[APIKey]", clientOrder);
}
else
{
    //If the username is already in use. Either change it, or prompt the user for a different username.
}