Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Can a form variable hold a javascript complex object?

Avatar

Level 4

I have a method in a fragmentScript script object that creates a javascript object formatted like below:

customerObject
    name      [string]
    address  [string]
    city        [string]
    state      [string]
    list of accounts    [array]

When the script is done executing, the subform containing the script gets hidden, and needs to pass the object to another subform which becomes visible. 

Can I pass this object via a form variable (located under File-Form Properties, Variables tab)?  That is, can a form variable contain a complex javascript object?

Thanks,

Elaine

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Elaine,

The variable tempRetCust seems to be assigned twice, the first one assigns it some XFA object (with a value property) the second one assigned it to a string (without a value property).  So I think eval(tempRetCust) will work, eval(tempRetCust.value) would be the same as eval(undefined).

Bruce

View solution in original post

7 Replies

Avatar

Level 10

Yes. For example, the attached form contains text fields and list box of accounts.

var customer = new Object();
customer.name = form1.page1.subform1.name.rawValue;
customer.address = form1.page1.subform1.address.rawValue;
customer.cty = form1.page1.subform1.city.rawValue;
customer.state = form1.page1.subform1.state.rawValue;
customer.accounts = new Array();

var accountCnt = form1.page1.subform1.accounts.length;
for (var i=0; i < accountCnt; i++) {
    customer.accounts[i] = form1.page1.subform1.accounts.getDisplayItem(i);
}

xfa.host.messageBox("The customer name is " + customer.name);
xfa.host.messageBox("The first account is " + customer.accounts[0]);

Avatar

Level 10

Hi Elaine,

I've tried something like this before but a form variable can only hold a string value.  However you could acheive what I think you want using the toSource() method and eval function.

var co1 = { name : "customer name",
            address : "customer address",
            city : "customer city",
            state : "customer state",
            accounts : [1,2,3]
          };

CustomerObject.value = co1.toSource();

var co2 = eval(CustomerObject.value)

console.println(co2.name);
console.println(co2.address);
console.println(co2.city);
console.println(co2.state);
console.println(co2.accounts);

Though have you tried storing the object in a script object at a higher level in the form structure available to subforms.

Bruce

Avatar

Level 4

Thanks, Steve, but I think I am referring to the form variable that you select using File-Form Properties - Variables tab.  I hope I am not misunderstanding your reply.  Thanks for the information!

Avatar

Level 4

Hey Bruce, I'm having a problem with the eval funtion returning a null, can you help me?

Here's my code snippet:

var tempRetCust = objFragment.resolveNode("returnCustomerObj");    //this is good

var objCustomer = myCustomerObject;    

tempRetCust

= objCustomer.toSource();    

app.alert("tempRetCust "

+ tempRetCust);       //  the toSource method works great and the alert here returns proper data

var retCust = eval(tempRetCust.value);  

if

(retCust==null ) app.alert ("return cust is null");     // the eval doesn't seem to work and the retCust variable is null.

Thanks!

Elaine

Avatar

Correct answer by
Level 10

Hi Elaine,

The variable tempRetCust seems to be assigned twice, the first one assigns it some XFA object (with a value property) the second one assigned it to a string (without a value property).  So I think eval(tempRetCust) will work, eval(tempRetCust.value) would be the same as eval(undefined).

Bruce

Avatar

Level 4

Thanks Bruce!  I got it working!  Thanks again!  --elaine