Skip to main content

My custom field isn’t persisting to the database here: I’ve checked both the bAccount and customergroupid and neither of them are null. This is inside a method that gets called in a persist override. 
 

 

Everything you’ve shown looks fine. We’ll need more information, though.

Does bAccount hold the correct record? What graph are you extending?


Hi Djanogo,
Yes, bAccount holds the record for the one displayed on screen. I’m extending customermaint in the graph I’m working on. For the DAC extension I’m extending BAccount.
 

I got it to work by using persist action but I feel like this isn’t the proper way to do it.
 

 


It appears that when it gets to the end of the persist method all the fields I’ve updated are erased.


    public void Persist(PersistDelegate baseMethod)
{
BAccount currentBAccount = Base.BAccount.Current;
Contact contactBefore = PXSelect<Contact,
Where<Contact.contactID, Equal<Required<Contact.contactID>>>>.Select(Base, currentBAccount.PrimaryContactID);
//ValidateConsignment(currentBAccount);
if (currentBAccount == null) //to handle deleting customer from customer screen
{
baseMethod();
return;

}
// Retrieve the order lines
var bAccountExt = PXCache<BAccount>.GetExtension<BAccountExt>(currentBAccount);

if (PXCache<BAccount>.GetExtension<BAccountExt>(currentBAccount).UsrFromMedusa == true) //To handle customer created from medusa
{
// Reset the flag to avoid future loops
Base.Caches[typeof(BAccount)].SetValueExt<SOOrderExt.usrFromMedusa>(currentBAccount, false);
Base.Caches[typeof(BAccount)].Update(currentBAccount);
Base.Caches[typeof(BAccount)].Persist(PXDBOperation.Update);
baseMethod();

Customer medusaCreatedCustomer = PXSelect<Customer,
Where<Customer.bAccountID, Equal<Required<Customer.bAccountID>>>>.Select(Base, currentBAccount.BAccountID);

Contact medusaCreatedContact = PXSelect<Contact,
Where<Contact.contactID, Equal<Required<Contact.contactID>>>>.Select(Base, medusaCreatedCustomer.PrimaryContactID);
v4.EmailOrder.SendEmailSignUp(medusaCreatedContact, currentBAccount.AcctCD, "User created");
//Skip the API call to Medusa
return;
}

var customer_added = Base.Caches[typeof(Customer)].Inserted //Gets customers added in cache
.Cast<Customer>()
.ToList();


var customer_updated = Base.Caches[typeof(Customer)].Updated //Gets customer updated from cache
.Cast<Customer>()
.ToList();
// Get the original state of the updated customer
Customer customerOriginal = null;
Customer customerUpdatedSingle = Base.Caches<Customer>().Updated.Cast<Customer>().FirstOrDefault();

try
{
if (customerUpdatedSingle != null)
{
// Get the original state of the updated customer
customerOriginal = Base.Caches<Customer>().GetOriginal(customerUpdatedSingle);
}
}
catch (Exception e)
{
// Log detailed exception information
PXTrace.WriteError($"Exception occurred in getting original customer: {e}");
}

var customer_deleted = Base.Caches[typeof(Customer)].Deleted
.Cast<Customer>()
.ToList();

baseMethod();
BAccount newBaccount = Base.BAccount.Current;
Customer newCustomer = PXSelect<Customer,
Where<Customer.bAccountID, Equal<Required<Customer.bAccountID>>>>.Select(Base, newBaccount.BAccountID);
CustomerValidationResult validationResult = ValidateCustomer(newCustomer);

// Start long operation
// Acuminator disable once PX1008 LongOperationDelegateSynchronousExecution [Justification]
PXLongOperation.StartOperation(Base, delegate ()
{
// Send the newly created location to another application
if (customer_added.Count > 0)
{
if (PXCache<BAccount>.GetExtension<BAccountExt>(newBaccount).UsrMedusaID == null)
{
CreateCustomerGroupMedusa(validationResult);


CreateCustomer(contactBefore, PXCache<BAccount>.GetExtension<BAccountExt>(validationResult.Customer).UsrMedusaCustomerGroupdID);


}
}

// Update the customer in Medusa
if (customer_updated.Count > 0)
{
updateCustomerMetadataMedusa(newCustomer, customerOriginal);
updateCustomerPriceListMedusa(newCustomer, customerOriginal);
}
// Remove the customer from Medusa
foreach (var deleted_customer in customer_deleted)
{
dynamic customerGroup = getCustomerGroupFromMedusa(PXCache<BAccount>.GetExtension<BAccountExt>(deleted_customer).UsrMedusaCustomerGroupdID);
var customerIds = new List<string>();
foreach (var customer in customerGroup?.customer_group?.customers)
{
customerIds.Add((string)customer?.id);
};
// Use GetValueExt to retrieve the extended field value
foreach (string customer_id in customerIds)
{
removeCustomerMedusa(customer_id);
}
removeCustomerGroupMedusa(PXCache<BAccount>.GetExtension<BAccountExt>(deleted_customer).UsrMedusaCustomerGroupdID);

}

});
}

 


So that can be a gotcha when you extend BAccount. Do you really want to extend BAccount or is it Customer that you want to extend?  By extending BAccount, you’re also adding those same fields to vendors, employees, companies - anything that an extension of BAccount.

Regardless of that I looked within CustomerMaint.cs and I see that it is referencing the rows as Customer vs a BAccount.

So I’m thinking that you want to be updating the cache related to the Customer and not BAccount.

Cachesatypeof(Customer)]

And part of the challenge is that if you’re forcing an update on the BAccount record that is connected to the Customer record before the customer record has been persisted then you’re likely heading for a concurrency issue.


Reply