Step 3: Creating Classes for External Entities

Now you will create classes for the external entities that you need to synchronize with the MYOB Acumatica system through the connector. For details about the classes, see Classes for External Entities.

Note:
The classes and the communication with the external system depend on the external system.

Creating Classes for External Entities

  1. In the Visual Studio project of the extension library, create a class with the name of the external entity that you want to process in the connector. In this example, you are creating the CustomerData class, shown in the code below. In this class, make sure that you have done the following:
    • Defined only the properties that correspond to the fields of the MYOB Acumatica entity.
    • Assigned the JsonProperty attributes to the properties. The names that are specified in these attributes must be the names of the properties of the external entity retrieved in JSON format through the external REST API.
    • Assigned the PX.Commerce.Core.CommerceDescription attributes to the class and its properties. The names that are specified in these attributes will be used as the names of the external objects and fields on the mapping and filtering tabs of the Entities (BC202000) form.
    Tip:
    You can see this code on GitHub.
    using System;
    using System.Collections.Generic;
    using PX.Commerce.Core;
    using Newtonsoft.Json;
    
    namespace WooCommerceTest
    {
        [CommerceDescription(WCCaptions.Customer)]
        public class CustomerData : BCAPIEntity, IWooEntity
        {
            [JsonProperty("id")]
            [CommerceDescription(WCCaptions.ID, FieldFilterStatus.Skipped, 
                FieldMappingStatus.Import)]
            public int? Id { get; set; }
    
            [JsonProperty("date_created")]
            public DateTime? DateCreatedUT { get; set; }
    
            [CommerceDescription(WCCaptions.DateCreatedUT)]
            [ShouldNotSerialize]
            public virtual DateTime? CreatedDateTime
            {
                get
                {
                    return DateCreatedUT != null ? (DateTime)DateCreatedUT.ToDate() : default;
                }
            }
    
            [JsonProperty("date_modified_gmt")]
            public DateTime? DateModified { get; set; }
    
            [CommerceDescription(WCCaptions.DateModifiedUT)]
            [ShouldNotSerialize]
            public virtual DateTime? ModifiedDateTime
            {
                get
                {
                    return DateModified != null ? (DateTime)DateModified.ToDate() : default;
                }
            }
    
            [JsonProperty("email")]
            [CommerceDescription(WCCaptions.Email, FieldFilterStatus.Skipped, 
                FieldMappingStatus.Import)]
            [ValidateRequired]
            public string Email { get; set; }
    
            [JsonProperty("first_name")]
            [CommerceDescription(WCCaptions.FirstName, FieldFilterStatus.Skipped, 
                FieldMappingStatus.Import)]
            [ValidateRequired]
            public string FirstName { get; set; }
    
            [JsonProperty("last_name")]
            [CommerceDescription(WCCaptions.LastName, FieldFilterStatus.Skipped, 
                FieldMappingStatus.Import)]
            [ValidateRequired()]
            public string LastName { get; set; }
    
            [JsonProperty("username")]
            [CommerceDescription(WCCaptions.UserName, FieldFilterStatus.Skipped, 
                FieldMappingStatus.Import)]
            public string Username { get; set; }
    
            [JsonProperty("billing")]
            public CustomerAddressData Billing { get; set; }
    
            [JsonProperty("shipping")]
            public CustomerAddressData Shipping { get; set; }
        }
    
        public interface IWooEntity
        {
            DateTime? DateCreatedUT { get; set; }
    
            DateTime? DateModified { get; set; }
    
        }
    }
    
  2. Repeat the previous instruction for each external entity that you need to use in your connector. For the WooCommerce connector, you also need the following classes:
    • CustomerAddressData, shown in the code below
      Tip:
      You can see this code on GitHub.
      using System;
      using System.ComponentModel;
      using PX.Commerce.Core;
      using Newtonsoft.Json;
      
      namespace WooCommerceTest
      {
          public class CustomerAddressData : BCAPIEntity, 
              IEquatable<CustomerAddressData>
          {
              [JsonProperty("customer_id")]
              [CommerceDescription(WCCaptions.CustomerId, 
                  FieldFilterStatus.Skipped, FieldMappingStatus.Import)]
              public virtual int? CustomerId { get; set; }
      
              [JsonProperty("first_name")]
              [CommerceDescription(WCCaptions.FirstName, FieldFilterStatus.Skipped, 
                  FieldMappingStatus.Import)]
              [ValidateRequired()]
              public virtual string FirstName { get; set; }
      
              [JsonProperty("last_name")]
              [CommerceDescription(WCCaptions.LastName, FieldFilterStatus.Skipped, 
                  FieldMappingStatus.Import)]
              public virtual string LastName { get; set; }
      
              [JsonProperty("company")]
              [CommerceDescription(WCCaptions.CompanyName, FieldFilterStatus.Skipped, 
                  FieldMappingStatus.Import)]
              public virtual string Company { get; set; }
      
              [JsonProperty("address_1")]
              [CommerceDescription(WCCaptions.AddressLine1, FieldFilterStatus.Skipped, 
                  FieldMappingStatus.Import)]
              [ValidateRequired(AutoDefault = true)]
              public string Address1 { get; set; }
      
              [JsonProperty("address_2")]
              [CommerceDescription(WCCaptions.AddressLine2, FieldFilterStatus.Skipped, 
                  FieldMappingStatus.Import)]
              public string Address2 { get; set; }
      
              [JsonProperty("city")]
              [CommerceDescription(WCCaptions.City, FieldFilterStatus.Skipped, 
                  FieldMappingStatus.Import)]
              [ValidateRequired(AutoDefault = true)]
              public virtual string City { get; set; }
      
              [JsonProperty("postcode")]
              [CommerceDescription(WCCaptions.PostalCode, FieldFilterStatus.Skipped, 
                  FieldMappingStatus.Import)]
              [ValidateRequired(AutoDefault = true)]
              public virtual string PostalCode { get; set; }
      
              [JsonProperty("country")]
              [CommerceDescription(WCCaptions.Country, FieldFilterStatus.Skipped, 
                  FieldMappingStatus.Import)]
              [ValidateRequired()]
              public virtual string Country { get; set; }
      
              [JsonProperty("state")]
              [Description(WCCaptions.State)]
              [ValidateRequired(AutoDefault = true)]
              public virtual string State { get; set; }
      
              [JsonProperty("phone")]
              [Description(WCCaptions.Phone)]
              [ValidateRequired(AutoDefault = true)]
              public virtual string Phone { get; set; }
      
              [JsonProperty("email")]
              [Description(WCCaptions.Email)]
              [ValidateRequired(AutoDefault = true)]
              public virtual string Email { get; set; }
      
              public bool Equals(CustomerAddressData newObject)
              {
                  var oldObject = this;
                  if (ReferenceEquals(newObject, oldObject)) return true;
                  if (newObject == null || oldObject == null) return false;
      
                  if (newObject.GetType() != oldObject.GetType()) return false;
      
                  var result = true;
      
                  foreach (var property in newObject.GetType().GetProperties())
                  {
                      var objValue = property.GetValue(newObject);
                      var anotherValue = property.GetValue(oldObject);
                      if (objValue == null || anotherValue == null)
                          result = objValue == anotherValue;
                      else if (!objValue.Equals(anotherValue)) result = false;
      
                      if (!result && !(property.Name == nameof(newObject.Phone) || 
                          property.Name == nameof(newObject.Email)))
                          return result;
                  }
      
                  return result;
              }
          }
      }
    • SystemStatusData and related entities, which are shown in the following code
      Tip:
      You can see this code on GitHub.
      using Newtonsoft.Json;
      
      namespace WooCommerceTest
      {
          public class SystemStatusData
          {
              [JsonProperty("environment")]
              public Environment Environment { get; set; }
      
              [JsonProperty("settings")]
              public Settings Settings { get; set; }
          }
      
          public class Settings
          {
              [JsonProperty("api_enabled")]
              public bool? ApiEnabled { get; set; }
      
              [JsonProperty("force_ssl")]
              public bool? ForceSsl { get; set; }
      
              [JsonProperty("currency")]
              public string Currency { get; set; }
      
              [JsonProperty("currency_symbol")]
              public string CurrencySymbol { get; set; }
      
              [JsonProperty("currency_position")]
              public string CurrencyPosition { get; set; }
      
              [JsonProperty("thousand_separator")]
              public string ThousandSeparator { get; set; }
      
              [JsonProperty("decimal_separator")]
              public string DecimalSeparator { get; set; }
      
              [JsonProperty("number_of_decimals")]
              public int? NumberOfDecimals { get; set; }
      
              [JsonProperty("geolocation_enabled")]
              public bool? GeolocationEnabled { get; set; }
      
              [JsonProperty("woocommerce_com_connected")]
              public string WoocommerceComConnected { get; set; }
          }
      
          public class Environment
          {
              [JsonProperty("home_url")]
              public string HomeUrl { get; set; }
      
              [JsonProperty("site_url")]
              public string SiteUrl { get; set; }
      
              [JsonProperty("version")]
              public string Version { get; set; }
      
              [JsonProperty("log_directory")]
              public string LogDirectory { get; set; }
      
              [JsonProperty("log_directory_writable")]
              public bool? LogDirectoryWritable { get; set; }
      
              [JsonProperty("wp_version")]
              public string WpVersion { get; set; }
      
              [JsonProperty("wp_multisite")]
              public bool? WpMultisite { get; set; }
      
              [JsonProperty("wp_memory_limit")]
              public int? WpMemoryLimit { get; set; }
      
              [JsonProperty("wp_debug_mode")]
              public bool? WpDebugMode { get; set; }
      
              [JsonProperty("wp_cron")]
              public bool? WpCron { get; set; }
      
              [JsonProperty("language")]
              public string Language { get; set; }
      
              [JsonProperty("external_object_cache")]
              public object ExternalObjectCache { get; set; }
      
              [JsonProperty("server_info")]
              public string ServerInfo { get; set; }
      
              [JsonProperty("php_version")]
              public string PhpVersion { get; set; }
      
              [JsonProperty("php_post_max_size")]
              public int? PhpPostMaxSize { get; set; }
      
              [JsonProperty("php_max_execution_time")]
              public int? PhpMaxExecutionTime { get; set; }
      
              [JsonProperty("php_max_input_vars")]
              public int? PhpMaxInputVars { get; set; }
      
              [JsonProperty("curl_version")]
              public string CurlVersion { get; set; }
      
              [JsonProperty("suhosin_installed")]
              public bool? SuhosinInstalled { get; set; }
      
              [JsonProperty("max_upload_size")]
              public int? MaxUploadSize { get; set; }
      
              [JsonProperty("mysql_version")]
              public string MysqlVersion { get; set; }
      
              [JsonProperty("mysql_version_string")]
              public string MysqlVersionString { get; set; }
      
              [JsonProperty("default_timezone")]
              public string DefaultTimezone { get; set; }
      
              [JsonProperty("fsockopen_or_curl_enabled")]
              public bool? FsockopenOrCurlEnabled { get; set; }
      
              [JsonProperty("soapclient_enabled")]
              public bool? SoapclientEnabled { get; set; }
      
              [JsonProperty("domdocument_enabled")]
              public bool? DomdocumentEnabled { get; set; }
      
              [JsonProperty("gzip_enabled")]
              public bool? GzipEnabled { get; set; }
      
              [JsonProperty("mbstring_enabled")]
              public bool? MbstringEnabled { get; set; }
      
              [JsonProperty("remote_post_successful")]
              public bool? RemotePostSuccessful { get; set; }
      
              [JsonProperty("remote_post_response")]
              public int RemotePostResponse { get; set; }
      
              [JsonProperty("remote_get_successful")]
              public bool? RemoteGetSuccessful { get; set; }
      
              [JsonProperty("remote_get_response")]
              public int? RemoteGetResponse { get; set; }
          }
      }
  3. Add the constants that you use for the descriptions of the entities and their fields to a class with the PXLocalizable attribute, as shown in the following code.
    Tip:
    You can see this code on GitHub.
    using PX.Common;
    
    namespace WooCommerceTest
    {
        [PXLocalizable]
        public static class WCCaptions
        {
            public const string AddressLine1 = "Address Line 1";
            public const string AddressLine2 = "Address Line 2";
            public const string City = "City";
            public const string CustomerId = "Customer ID";
            public const string FirstName = "First Name";
            public const string LastName = "Last Name";
            public const string CompanyName = "Company Name";
            public const string Country = "Country";
            public const string PostalCode = "Postal Code";
            public const string State = "State";
            public const string Phone = "Phone";
            public const string Email = "Email";
            public const string Customer = "Customer";
            public const string DateCreatedUT = "Date Created UT";
            public const string DateModifiedUT = "Date Modified UT";
            public const string ID = "ID";
            public const string UserName = "UserName";
        }
    }
  4. Build the project.