Skip to main content
Question

Why is a code created sales order different to UI created sales order

  • January 20, 2025
  • 8 replies
  • 73 views

Forum|alt.badge.img+1

Hello. When I create a sales order with code it is different to the sales order I create via the UI.

The order type and customer are the same so I don’t understand when the final order is different.

The sales order below has been created with the UI. I only set the Customer and Customer Order Ref fields. Notice that the Contact field has been set to ‘Book In’ automatically.

 

 

To create a sales order using code, I’ve used the following code.

SOOrderEntry sOOrderEntry = PXGraph.CreateInstance<SOOrderEntry>();
SOOrder sOOrder = (SOOrder)sOOrderEntry.Document.Cache.CreateInstance();
sOOrderEntry.Document.Cache.SetValueExt<SOOrder.orderType>(sOOrder, "SA");
sOOrderEntry.Document.Cache.SetValueExt<SOOrder.customerID>(sOOrder, 7511);
sOOrderEntry.Document.Cache.SetValueExt<SOOrder.customerOrderNbr>(sOOrder, "Test Order");
sOOrder = sOOrderEntry.Document.Insert(sOOrder);
sOOrderEntry.Actions.PressSave();

The sales order created is shown below. The Contact field is empty! 

 

Can anyone explain why the Contact field isn’t updated?
Can anyone explain how to change the code to make the contact appear?

 

8 replies

Forum|alt.badge.img+6
  • Captain II
  • 562 replies
  • January 20, 2025

You want to create an instance of the DAC (SOOrder), set the order type and then call .Insert.  Once you done that, populate the rest of the fields and make calls to .Update as desired.

Now, in my code, I’m setting the ContactID manually.  If you don’t but, instead, call .Update after setting the CustomerID field, the base Acm logic for updating the CustomerID field will run and you’ll see the default contact appear if the customer is set up with one.

Call the graph’s Save.Press() at the end.

 

SOOrderEntry soGraph = PXGraph.CreateInstance<SOOrderEntry>();
var sOrder = new SOOrder();
sOrder.OrderType = "SO";
sOrder = PXCache<SOOrder>.CreateCopy(soGraph.Document.Insert(sOrder));

//I'm determining the customer record using lookup logic
Customer cust = FindTheCustomerRecord(sourceRecord, soGraph);

sOrder.CustomerID = cust.BAccountID;
sOrder.ContactID = cust.PrimaryContactID < 0L ? null : cust.PrimaryContactID;
sOrder = soGraph.Document.Update(sOrder);

//do other things to the order
//add lines, etc

soGraph.Save.Press();

//Now I'll have the OrdNbr field value and can do other things as required like adding a prepayment.

 


Forum|alt.badge.img+1
  • Author
  • Varsity I
  • 93 replies
  • January 21, 2025

Hi ​@Django,

I’ve used your code but with a few slight changes.
1. The order type has changed to SO to SA.
2. I know the customerID so I’m testing against, so I set it directly.
3. I’ve commented out the line which sets the contact. since I want Acumatica to complete this.

SOOrderEntry soGraph2 = PXGraph.CreateInstance<SOOrderEntry>();
var sOrder2 = new SOOrder();
sOrder2.OrderType = "SA";
sOrder2 = PXCache<SOOrder>.CreateCopy(soGraph2.Document.Insert(sOrder2));

//I'm determining the customer record using lookup logic
//Customer cust = FindTheCustomerRecord(sourceRecord, soGraph);

sOrder2.CustomerID = 7511;
//sOrder2.ContactID = cust.PrimaryContactID < 0L ? null : cust.PrimaryContactID;
sOrder2 = soGraph2.Document.Update(sOrder2);
sOrder2.CustomerOrderNbr = "Test Order 2";
sOrder2 = soGraph2.Document.Update(sOrder2);
//do other things to the order
//add lines, etc
soGraph2.Save.Press();

The Contact field on the Sales Order is empty.

It doesn’t seem to matter whether insert or update is used, the Contact isn’t populated. 

In this case I could set the Contact field myself but surely I shouldn’t have to - I expected the business rules in the graph to do the work and match the actions in the UI. Do you have any idea why some fields are set and others remain blank? I seems almost random to me, which isn’t a good place to be. 


Forum|alt.badge.img+6
  • Captain II
  • 562 replies
  • January 21, 2025

If the fields are working differently in your code vs the UI it usually means that the business logic isn’t being executed. Edit: It can also mean that other business logic is being run that is interfering with the existing logic.

Stuff like this thins my hair.

Going back to your original code - what we have you make the adjustments to calling .Insert and .Update.  Your calls to SetValueExt will trigger the business logic.

SOOrderEntry sOOrderEntry = PXGraph.CreateInstance<SOOrderEntry>();
SOOrder sOOrder = (SOOrder)sOOrderEntry.Document.Cache.CreateInstance();
sOOrderEntry.Document.Cache.SetValueExt<SOOrder.orderType>(sOOrder, "SA");
sOOrder = sOOrderEntry.Document.Insert(sOOrder);

sOOrderEntry.Document.Cache.SetValueExt<SOOrder.customerID>(sOOrder, 7511);
sOOrderEntry.Document.Cache.SetValueExt<SOOrder.customerOrderNbr>(sOOrder, "Test Order");
sOOrder = sOOrderEntry.Document.Update(sOOrder);

sOOrderEntry.Actions.PressSave();

 


Forum|alt.badge.img+1
  • Author
  • Varsity I
  • 93 replies
  • January 21, 2025

“Stuff like this thins my hair.”

Yep, I completely agree. I find this type of problem really frustrating because I don’t really want to have to poke around in the source code trying to figure out why it’s not working. Plus this is a simple sales order, I’m very familiar with this part of the system. Getting similar problems in other, less known, parts of the system would be ever more painful.

You are correct, I’m trying to use SetValueExt specifically because I think the contact id is returned from the FieldUpdated event of CustomerID.

The updated code is below, I had to perform a cache update after setting the customer id to avoid an error about currency codes, but it’s pretty much the same. 

SOOrderEntry sOOrderEntry1 = PXGraph.CreateInstance<SOOrderEntry>();
SOOrder sOOrder1 = (SOOrder)sOOrderEntry.Document.Cache.CreateInstance();
sOOrderEntry1.Document.Cache.SetValueExt<SOOrder.orderType>(sOOrder1, "SA");
sOOrder1 = sOOrder1 = sOOrderEntry.Document.Insert(sOOrder1);
sOOrderEntry1.Document.Cache.SetValueExt<SOOrder.customerID>(sOOrder1, 7511);
sOOrder1 = sOOrderEntry1.Document.Update(sOOrder1);
sOOrderEntry1.Document.Cache.SetValueExt<SOOrder.customerOrderNbr>(sOOrder1, "Test Order3");
sOOrder1 = sOOrderEntry1.Document.Update(sOOrder1);
sOOrderEntry1.Actions.PressSave();

The end result is the same. Still no Contact. I only include the screen shot to show that the Customer Order Ref has changed, to show I’ve tested it.

 

Sad times. 


Forum|alt.badge.img+6
  • Captain II
  • 562 replies
  • January 21, 2025

Are you using Visual Studio for your development?  If so I’d be tempted to look at what sOrder1 looks like after assigning the customerID and again after the .Update. After assigning the customerID you should see the contactID field assigned a value as long at the customer’s record has a default contact.

This will tell you if there’s something going on with another customization in place that is clearing the contactID field or if the field isn’t being set at all.

 

 


Forum|alt.badge.img+1
  • Author
  • Varsity I
  • 93 replies
  • January 22, 2025

I do use Visual Studio, I was able to track the variable and I can see that contactID is null at every step.

I’ve double checked the customer record to ensure that it has a default contact, and tried the code against a different customer since the customer I was testing against was on credit hold - not that I would expect being on credit hold to make a difference. 

Also, I checked the bin folder and removed any other custom dll’s which had been published.

Still the same result. I have a number of Acumatica system available for testing and dev work. Maybe the next step is to try the code against a different system. But I have a suspicion that I’ll see the same issue. 

Any other ideas are very welcome. 


Forum|alt.badge.img+6
  • Captain II
  • 562 replies
  • January 23, 2025

I would recommend installing VS. You can use the community version which is free.

I’ll have some time shortly to take another look at it.


Forum|alt.badge.img+1
  • Author
  • Varsity I
  • 93 replies
  • January 23, 2025

Just to be clear - I do have VS installed. I can’t imagine trying to code against Acumatica without it. 

If you did get a chance to try this on your system I would be very interested to know the result. 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings