title: Creating Customer Location
body: <p>My requirement is to create a customer location part of downloading order from 3rd party shopping cart.</p>
  
  <p>I have tried this below code and It is not saving any location and also not raising any error.</p>
  
  <pre><code>private static void CreateCustomerLocation(Customer cust, string locationcode, OrderDTO ord, OrderDownloadActivityEntry grp)
          {
              try
              {
                  LocationMaint graph = CustomerLocationMaint.CreateInstance&lt;CustomerLocationMaint&gt;();
                  SelectedLocation loc = new SelectedLocation();
                  loc.BAccountID = cust.BAccountID;
                  loc.LocationCD = locationcode;
                  loc.Descr = ord.CustomerLocationName;
                  loc.IsContactSameAsMain = false;
                  loc.IsAddressSameAsMain = false;
                  graph.Location.Insert(loc);
  
                  Contact contact = new Contact();
                  contact.Attention = ord.OrderCustomerContactName;
                  contact.Phone1 = ord.OrderCustomerContactPhone;
                  contact.DisplayName = ord.CustomerLocationName;
                  contact.LastName = ord.OrderCustomerContactName;
                  contact = graph.Contact.Update(contact);
                  Address address = new Address();
                  address.AddressLine1 = ord.OrderShippingLocationAddress1;
                  address.AddressLine2 = ord.OrderShippingLocationAddress2;
                  address.City = ord.OrderShippingLocationCity;
                  address.State = ord.OrderShippingLocationState;
                  address.PostalCode = ord.OrderShippingLocationZip;
                  address.CountryID = "US";
  
                  contact = graph.Contact.Update(contact);
                  address = graph.Address.Update(address);
                  loc.DefAddressID = address.AddressID;
                  loc.DefContactID = contact.ContactID;
                  graph.Location.Update(loc);
                  graph.Save.Press();
              }
              catch(Exception e)
              {
                  grp.AddLogData(SessionID, "Create Location", "Create Location falied", null, null, e.StackTrace);
              }
          }
  </code></pre>
  
  <p>I am not able to figure out where i am making mistake. any suggestion for this issue?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I have tried the following code and I am getting the following error</p>
  
  <p><strong>CARAccountLocationID' cannot be empty.</strong></p>
  
  <pre><code>private static void CreateCustomerLocation(Customer cust, string locationcode, OrderDTO ord, OrderDownloadActivityEntry grp)
          {
              try
              {
                  LocationMaint graph = PXGraph.CreateInstance&lt;CustomerLocationMaint&gt;();
                  graph.BusinessAccount.Current = PXSelect&lt;BAccount, Where&lt;BAccount.bAccountID, Equal&lt;Required&lt;BAccount.bAccountID&gt;&gt;&gt;&gt;.Select(graph, cust.BAccountID);
                  var newLocation = (Location)graph.Location.Cache.CreateInstance();
                  var locType = LocTypeList.CustomerLoc;
                  newLocation.LocType = locType;
                  graph.Location.Insert(newLocation);
  
                  var loc = (Location)graph.Location.Cache.CreateCopy(graph.Location.Current);
  
  
  
  
                  Contact contact = graph.Contact.Cache.CreateCopy(graph.Contact.Current) as Contact;
                  contact.Attention = ord.OrderCustomerContactName;
                  contact.Phone1 = ord.OrderCustomerContactPhone;
                  contact.DisplayName = ord.CustomerLocationName;
                  contact.LastName = ord.OrderCustomerContactName;
                  contact = graph.Contact.Update(contact);
                  Address address = graph.Address.Cache.CreateCopy(graph.Address.Current) as Address;
                  address.AddressLine1 = ord.OrderShippingLocationAddress1;
                  address.AddressLine2 = ord.OrderShippingLocationAddress2;
                  address.City = ord.OrderShippingLocationCity;
                  address.State = ord.OrderShippingLocationState;
                  address.PostalCode = ord.OrderShippingLocationZip;
                  address.CountryID = "US";
                  contact = graph.Contact.Update(contact);
                  address = graph.Address.Update(address);
                  contact.DefAddressID = address.AddressID;
                  loc.IsAddressSameAsMain = false;
                  loc.IsContactSameAsMain = false;
                  loc.IsAPAccountSameAsMain = true;
                  loc.IsAPPaymentInfoSameAsMain = true;
                  loc.IsARAccountSameAsMain = true;
                  loc.LocationCD = locationcode;
                  loc.Descr = ord.CustomerLocationName;
                  loc = graph.Location.Update(loc);
                  loc.BAccountID = cust.BAccountID;
                  graph.Location.Cache.RaiseFieldUpdated&lt;Location.isARAccountSameAsMain&gt;(loc, null);
                  if (loc.CARAccountLocationID == null)
                      loc.CARAccountLocationID = cust.DefLocationID;
                  graph.Location.Update(loc);
                  graph.Save.Press();
              }
              catch(Exception e)
              {
                  grp.AddLogData(SessionID, "Create Location", "Create Location falied", null, null, e.StackTrace);
              }
          }
  </code></pre>