Bean basics

Ok, I’ve added a 3rd party bean to my form, so I can talk to Quickbooks. I need a few questions answered by those in the know, so I can better understand beans and classes and objects with Servoy.

Once I’ve added a bean to a form, it shows up as an element on the form. I then reference properties of the objects as such: elements.bean_316.customerName. I have this working.

(I’m talking at my outer limits of knowledge of Java here, so don’t assume I know anything about it…)

It appears that I now have 1 object instance of class customer, by adding the bean to my form. In Java, I would create a new object with something like this: var cust1 new customer()
(keep in mind, I don’t know java, but I have read a bit about OO and understand the basics)

Can I do the same thing in my javascript, and instantiate a new customer object to work with?

What if I want a whole array of customers to work with at once? Can I create an array of customer objects?

So far, I’ve only worked with the bean_316 object in my script and so have only worked with one customer at a time.

Can someone be so kind as to explain some of this? I need some bearings in Servoy in relation to objects and beans, and classes that beans add to my Servoy solution.

Thank you!

(I’m talking at my outer limits of knowledge of Java here, so don’t assume I know anything about it…)

Not a direct answer to your question, but if you would like to learn more about Java + Servoy, I wrote a whitepaper that may be of interest to you. There is also a supporting blog, where you might be able to get some more answers.

The whitepaper can be found here: http://www.omnesoft.com/downloads/java_ … lopers.pdf

And the blog can be found here: http://java-servoy.blogspot.com/

FYI: Depending on how the third-party QB bean you are using is written/packaged you may be able to get access to its methods and properties etc. without actually placing it on a form e.g. placing the jar file in the lib dir and scripting a little Java. More about this in the whitepaper…

Good reading. Thank you.

I still have some basic questions I’m hoping someone can clarify. Can an expert please take a stab at this?

I have a bean object on my form (bean_316). I would like to read in a bunch of customers via the bean and store them in an array. How would I create an array of objects based on the bean?

Code would be something like this:

custArray = new Array()
for (i=0; i < numRecords; i++)
{
elements.bean_316.get(i)
custArray = elements.bean_316
}
When I try to reference a customer object in the array, I’m not getting any properties back. I’m trying to reference a property as such:
globals.gContactName = custArray[0].contactName
Any guidance on how to build this array of objects and be able to reference them within the array? Thanks.

What code do you have in the bean right now to ‘support’ what you are doing?

elements.bean_316.get(i) 
```When you retrieve a value you need to stick it in a variable I think....

custArray = elements.bean_316
```Here you add the complete bean to the array element.

Can you post a link to the QB bean’s API?

Thanks

Attached is a zip file with the Javadocs directory. If you open that, then open index.html, you’ll see their documentation.

This can all be downloaded for free (trial version) from www.nsoftware.com (bean and docs).

Let me know if see anything helpful.

Thanks.

javadoc.zip (893 KB)

Here is the method I am calling, just to test arrays of these customer objects:


elements.bean_115.QBConnectionString = “URL=‘http://yoda:2080’”
elements.bean_115.openQBConnection
elements.bean_115.QBRequestId = ‘’

var custArray = new Array()

elements.bean_115.getByName(“My Company”)
custArray.push(elements.bean_115)

elements.bean_115.getByName(“Elite Ops”)
custArray.push(elements.bean_115)

// see what is now stored in the array
for(i=0; i < custArray.length; i++)
{
globals.gTest1 += custArray*.contactName + ‘\n’*
}
-------------------------------
It seems to only store a reference to the bean_115 object because I get the same contactName twice in globals.gTest1, instead of two different contactNames.
So I think I have everything working except I don’t know how to get a copy of the object, instead of a reference to it.

Yes, that is correct.

My goal is to get all the customers into the realm of Servoy, so I can create value lists, edit customer records, etc. and not have to talk to Quickbooks on each and every transaction, which is expensive in terms of roundtrip time.

My strategy is to read each customer via the bean object and copy those objects one by one into my array of objects.

Here’s the heart of my problem…

If anyone can figure this one out, I owe you a root beer at Servoy World.

How do you do a true deep copy of an object in Servoy, instead of just copying the reference pointer to the object?

I found a method called .cloneNode(true) on google, but that didn’t work when appended to my object.

I’ve got some code that gets me part of the way there. Now I’m fighting array syntax. Here’s what I have (see comments inline)…

It goes thru one round of the inner nested FOR loop, then it gives me an “undefined property” error on the 2nd time through that loop when x=1


elements.bean_115.QBConnectionString = “URL=‘http://yoda:2080’”
elements.bean_115.openQBConnection
elements.bean_115.QBRequestId = ‘’

// Setup an array of three known companies to read in

var custs = [“My Company”, “Elite Ops”, “Contexo Media LLC”]

// This is the line I don’t know how to do.
// I only need a one dimensional array of objects
// but the inner loop below cycles through the properties of
// elements.bean_115 with which makes it appear to be
// a multi-dimensional array
var custArray = new Array(new Array({})) // ???
for(x = 0; x < custs.length; x++)
{
elements.bean_115.getByName(custs[x])
for (i in elements.bean_115)
{
// Here’s where it makes it thru once (x=0), but gives
// undefined property error when x=1
custArray = elements.bean_115*;
_}
}_
// this spits out my array to a text_area to see if it worked*

for(i=0; i < custArray.length; i++)
{
globals.gTest1 += custArray*.contactName + ‘\n’*
}
globals.gTest2 = custArray.length
----------------------------

Jeff Bader wins the root beer! Thanks Jeff.

The solution was not to copy any objects, but to utilize the ibiz Quickbooks jar bean in the /servoy/lib directory and call it directly. I didn’t have to put a bean on a form for this to work.

Once I did that, I was able to instantiate the Java class Packages.ibizqb.Customer() in my script and create an array of customer objects. This got me past the limitation of only having one instance of the bean on my form.

Here is my final code for your reading pleasure:

// This method reads 3 known customers from Quickbooks and puts their records into an array of Java objects
// Dependencies:
// ibiz Quickbooks Integrator (ibizqb.jar) file must be put in the /servoy/lib directory (www.nsoftware.com)
// Quickbooks must be running on a PC with the ibiz Remote Connector running (default port = 2080)
// This demo assumes you know the customerName of some existing customers in QB
// A global variable (globals.gTest1) is used on a form to display the contents of the objects as a final test
//
// Written by Jason Pierson with assistance from Jeff Bader
//
// License agreement: Pay it forward to other Servoyans when they need help! :)
//
// Create a global array
customers = new Array()

// For my test, I created this little array of company names I know exist, so I can pull their records
var custs = [“My Company”, “Elite Ops”, “Contexo Media LLC”]

// Loop thru once for each customer in my cust array
for(x = 0; x < custs.length; x++)
{
// create a new instance object of a customer using the ibiz java library directly instead of a bean
customers = new Packages.ibizqb.Customer()
// This is the string required to talk to the ibiz Quickbooks remote connector, running on the QB machine
customers.QBConnectionString = “URL=‘http://yoda:2080’”
customers.openQBConnection
// I have to work out exactly how the QBRequestId works. If I set it to blank everything works.
// If you want to validate that your transaction was accepted by the QB SDK, you can set it to a unique value each time
customers.QBRequestId = ‘’
// This method takes a customerName and gets the entire customer record back from QB with all properties populated
customers.getByName(custs)
}

// This is my test loop to inspect my customers array and make sure it’s really full of customer objects
// I put a global on my form as a text_area so I can see what I’m getting
for(x = 0; x < customers.length; x++)
{
// contactName is a standard property of the Customer() class that gets populated from QB into your object
globals.gTest1 += customers.contactName + ‘\n’
}

Just a heads up that the trial version of ibizQB has a 3-sec delay built in to all calls. At first I was shocked at how slow things were, but then I read about this delay. Production version should be fine.

Regards,