SalesOrder.CreateNew() and order lines  Topic is solved

Discussions relating to Jiwa 7 plugin development, and the Jiwa 7 API.

SalesOrder.CreateNew() and order lines

Postby Mark Shipman » Tue Jul 28, 2020 7:34 am

Hi,

Env: Jiwa 7.2.1; MS-SQL 2019; VS2019

When I invoke the CreateNew() method on a SalesOrder object, will the SalesOrderLines collection be initialised?

Or is this something I need to specifically do?
Mark Shipman
Software Developer
Mark Shipman
Occasional Contributor
Occasional Contributor
 
Posts: 21
Joined: Mon Mar 02, 2020 1:44 pm
Location: Auckland, New Zealand

Re: SalesOrder.CreateNew() and order lines

Postby Scott.Pearce » Tue Jul 28, 2020 9:53 am

Technically, the SalesOrderLines collection is already initialised - this happens when you use the factory to create the SalesOrderObject:

Code: Select all
salesOrder = Manager.BusinessLogicFactory.CreateBusinessLogic<JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder>(null)


When CreateNew() is called, one of the first things it calls is Clear(), which results in SalesOrderLines.Clear() being called, which properly removes all items from the collection along with their handlers, and resets calculated totals properties to 0.

tl;dr - There is nothing you specifically need to do.
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 765
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 230

Re: SalesOrder.CreateNew() and order lines

Postby Mark Shipman » Tue Jul 28, 2020 2:37 pm

Thank you.

My apologies that my query was not clear enough.

We use the factory to create a sales order object, but the SalesOrder Debtor property is read only.

The only [obvious] way to create the sales order with a debtor is to invoke the CreateNew() method with a "debtor seed":
Code: Select all
                // Because the debtor ID is needed
                JiwaFinancials.Jiwa.JiwaApplication.Entities.Debtor.Debtor debtor = ResolveDebtor();

                // Because the order needs to be created anew...
                jiwaSalesOrder = jiwaManager.BusinessLogicFactory.CreateBusinessLogic<JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder>(null);

                jiwaSalesOrder.CreateNew(
                    SalesOrder.NewSalesOrderTypes.e_NewSalesOrder,
                    // ...and debtor is assigned now
                    debtor.DebtorID, true);


This seems to suggest (happy to be wrong) the SalesOrder object is "re-created".

If the SalesOrder object is "re-created", is the lines collection preserved at the time of "re-creation"?
Mark Shipman
Software Developer
Mark Shipman
Occasional Contributor
Occasional Contributor
 
Posts: 21
Joined: Mon Mar 02, 2020 1:44 pm
Location: Auckland, New Zealand

Re: SalesOrder.CreateNew() and order lines

Postby SBarnes » Tue Jul 28, 2020 3:18 pm

You need to make the factory call and follow this with a call to createnew otherwise you will get some nasty side effects as certain this may not have been initialised.

To explain the difference between the two the factory method instantiates the object, attaches the manager and calls the business objects set up routine, if you were writing your own business logic object which you can do this can be viewed to the the equivalent of your constructor where you might initialise other objects in your object that require a manager for example as the setup only gets called after the objects manger is attached.

The create new as Scott said basically clears the the sales order object and puts it into the mode for adding lines etc, you could try going salesorder.Debtor.ReadRecord(Debtorid) but DON'T as I said not calling createnew will mean the sales order object is not completely initialised for use and certain properties can cause null reference exceptions.

What I am saying is don't view createnew and the c# use of the keyword new.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1696
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 191

Re: SalesOrder.CreateNew() and order lines  Topic is solved

Postby Scott.Pearce » Tue Jul 28, 2020 4:53 pm

.CreateNew() can be considered a helper method. It makes sure that your sales order object and it's properties are properly cleared and set up as a new sales order (i.e. property defaults are set, collection properties cleared, insert flags set, etc.).

You should always use .CreateNew() to create new sales orders. A debtor is always required for a sales order, so .CreateNew() accepts one as a parameter and sets the .Debtor property for you (well, it actually accepts a DebtorID value and does a .Debtor.Read()).

You could choose to not use the .CreateNew() method and set/clear properties manually, but that's a lot of work and opens you up to missing something.

Finally, the reason the .Debtor property is read-only, is that it is an entity - you don't set it, you do a .Read() on it, thus:

Code: Select all
SalesOrder.Debtor.Read(myDebtorID)


So the idea with our objects is that you use a factory to instantiate it. Then you can do a .CreateNew() to create a new record, or perhaps you will do a .Read() to read an existing record. You may very well do a .Read(), look at some stuff, and then call a .CreateNew() on that same object to create a new record. The object is created once, and you use it over and over for different operations.
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 765
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 230


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 2 guests