Copying child debtor. Keep parent.  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Copying child debtor. Keep parent.

Postby DannyC » Tue Oct 01, 2019 7:22 pm

I have a debtor who is a child/branch debtor.
When the user clicks 'Copy', a new debtor is created but when it's saved the relationship with the parent is lost.

I thought this would be an easy plugin to develop, so I quoted the client 1 hour.
I've now been at it 5 hours and still no joy. Can you relieve my pain?

Here's my plugin. I am declaring a parent debtor entity & assigning it from the source debtor withing the CopyStart event.
Then in the CopyEnd event the debtor with focus is the new debtor, so I am just assigning the parent debtor ID.

But each time I save the new debtor, the parent ID is lost.
I've even tried doing a SQL UPDATE statement during the save event, but not even that is working.

Here's my plugin.
Version 7.2.1.
Attachments
Plugin Attkey. Copy child debtor to new debtor b.xml
(32.19 KiB) Downloaded 70 times
User avatar
DannyC
Senpai
Senpai
 
Posts: 635
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 29

Re: Copying child debtor. Keep parent.

Postby DannyC » Tue Oct 08, 2019 11:49 am

Bump. More of a slight nudge.
Would be keen for an answer in the next week or so.
User avatar
DannyC
Senpai
Senpai
 
Posts: 635
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 29

Re: Copying child debtor. Keep parent.

Postby Scott.Pearce » Fri Oct 11, 2019 2:46 pm

After the copy, you need to update the parent debtor, by adding the new child debtor to the .BranchDebtors collection.
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 742
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 221

Re: Copying child debtor. Keep parent.

Postby DannyC » Fri Oct 11, 2019 5:46 pm

you need to update the parent debtor

Good tip.
But now that throws up a whole heap of other errors.
Using
Code: Select all
parentDebtor.BranchDebtors.Add(newlycreatedDebtor)

says that it needs to be a type BranchDebtor.

If I try to assign a new BranchDebtor object from the newly created debtor, it has another error 'cant implicitly convert type debtor to branch debtor'.

Can you grab my sample plugin and tweak it?
User avatar
DannyC
Senpai
Senpai
 
Posts: 635
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 29

Re: Copying child debtor. Keep parent.

Postby Scott.Pearce » Mon Oct 14, 2019 9:05 am

You need to create a branch debtor object from the debtor object you have. It is the branch debtor object you then add to the branch debtors collection. Use the appropriate properties from the debtor object to construct your branch debtor object for adding (ie. BranchDebtor.Debtor.Read(fullDebtorObject.DebtorID).
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 742
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 221

Re: Copying child debtor. Keep parent.

Postby DannyC » Mon Oct 14, 2019 9:40 am

You need to create a branch debtor object from the debtor object you have

From the parent, or the new debtor?
I had done that already but that's where I get the 'can't implicitly convert' errors.

I have 3 debtors:
The original child debtor.
The above's parent debtor
The new debtor created from the copy.

I've created a branch debtor object & tried to add it to the parent. I just can't this to work. No joke I reckon I've spent nearly 2 days trying to get this to work, for something I thought I could knock over in an hour!
Sorry for the frustration, but can you just grab my attached plugin & modify it so I can understand better where I'm going wrong?
User avatar
DannyC
Senpai
Senpai
 
Posts: 635
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 29

Re: Copying child debtor. Keep parent.

Postby Scott.Pearce » Mon Oct 14, 2019 11:54 am

Danny, I actually found doing this pretty difficult myself so don't feel bad. Much harder than I thought it would be. I've attached a plugin that does what you want.

Further, note that there are tools that you can use to "disassemble" our DLLs (such as "dotPeek") and see our sourcecode - this may have helped you, as I had to refer to the sourcecode a couple of times to figure out what was going on.
Attachments
Plugin Attkey. Copy child debtor to new debtor.xml
(34.34 KiB) Downloaded 52 times
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 742
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 221

Re: Copying child debtor. Keep parent.  Topic is solved

Postby Mike.Sheen » Mon Oct 14, 2019 12:26 pm

Here's my take on it (attached). I find it more intuitive to leverage the CopyEnd event to dynamically add a handler for SaveEnding (if it is a child debtor being copied) which then updates the parent with the new child debtor.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2440
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 755

Re: Copying child debtor. Keep parent.

Postby DannyC » Mon Oct 14, 2019 5:32 pm

And therein children lies todays lesson.
Some things are just outside ones skill level.

Both solutions I'd never have reached.

Thanks so much for giving me a different angle on how the same result can be achieved with some different code. Both use the GenericObjectCollection, so that's something to store in the recesses somewhere for next time.
Can you explain the usage of GenericObjectCollection a bit?

I was also stumped on this line
Code: Select all
branchDebtor.BranchDebtor.ReadRecord(debtor.DebtorID)


Really appreciate your efforts!
User avatar
DannyC
Senpai
Senpai
 
Posts: 635
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 29

Re: Copying child debtor. Keep parent.

Postby Mike.Sheen » Mon Oct 14, 2019 6:00 pm

DannyC wrote:Can you explain the usage of GenericObjectCollection a bit?


Some of our base classes (like the Maintenance forms and the maintenance Business Logic) have a property called GenericObjectCollection. It's not used by Jiwa, it's purpose for plugins to store "stuff" in there for an instance of the form or business logic. The collection is just a standard Jiwa collection of GenericObjectItem - which is just simple class with just two important properties - RecID and Object.

You shove something into the instance of an object by deciding on a unique identifier and a value - in this example we are going to create and add to the GenericObjectCollection of the debtor instance a GenericObjectItem with a RecID (the unique identifier) of "ParentDebtorID" and the value we're going to give it is the debtors parent debtor ID. It does not have to be a string, you can store anything in there.

Code: Select all
debtor.GenericObjectCollection.SetOrAdd("ParentDebtorID", debtor.ParentDebtor.DebtorID);


And later on, when we want to retrieve the value of the thing we stored in the "ParentDebtorID" item, we can retrieve that with:
Code: Select all
string parentDebtorID = debtor.GenericObjectCollection.GetValue<string>("ParentDebtorID");


The GetValue method has a Generic parameter indicating the Type you want is to convert the Object to before returning it.

The reason why we use this is because with Plugins you cannot just declare a private variable of the class and store the value in there - because the classes (FormPlugin, BusinessLogicPlugin and so on) in the plugin are not created every time a form or business logic is loaded - we create those at application startup and invoke the Setup Method every time a form or business logic is loaded - so loading two Debtor forms in your example here would mean if we put the ParentDebtorID into a private variable of the class, it would be set by the first form load, then when a 2nd instance of the debtor form is loaded (the first one still open too), it overwrites that value and so when you switch back to the first debtor it would behave as though it had the parent debtor id of the 2nd form's debtor - not what you want.

So, the GenericObjectCollection is just a tool you can use to store things which are not shared across instances of objects.

DannyC wrote:I was also stumped on this line
Code: Select all
branchDebtor.BranchDebtor.ReadRecord(debtor.DebtorID)



A debtor has a collection of BranchDebtors in the BranchDebtors property. To add a debtor as a branch of another debtor you need to add to the BranchDebtors collection of the debtor to act as the parent. This collection wants items in itself to be of type JiwaFinancials.Jiwa.JiwaDebtors.BranchDebtor - so you create one of those - but before you add it to the BranchDebtors collection, there is a property "BranchDebtor" which needs to be "read" - this is our generic debtor Entity - it has a method ReadRecord which accepts a DebtorID to read the debtor information.

Looking at the types of the properties will make things much clearer - it's easy to become confused when we have things like Debtor entity and Debtor Business logic classes and the Debtor Business Logic (JiwaDebtors.Debtor) has a property named BranchDebtors which is a collection of JiwaDebtors.BranchDebtor which contains a property that is called BranchDebtor but it's type is JiwaFinancials.Jiwa.JiwaApplication.Entities.Debtor.Debtor.

Like Scott mentioned, and others have also mentioned this in the past - use a disassembly tool to generate the source code from our Assemblies and then everything should become much easier to grok by looking at how we do things. In this case, you'd want the JiwaDebtorsUI.dll disassembled to see how our Debtor Maintenance form adds a branch debtor using the Debtor Business Logic.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2440
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 755


Return to Technical and or Programming

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron