Defining/Using Coldfusion Struct in Flex

I am trying to figure out how to define and use a coldfusion struct in flex.  I have figured out how to create a value object class in flex.  I created an action script class (Customer.as)  I am using just three variables in my class (recordID, firstName, lastName).

I learned how to populate an ArrayCollection using static data and this class and populated a datagrid. Good Stuff

I am trying to figure out how to populate this ArrayCollection from a struct returned from Coldfusion.

I would also like to return the struct to an Object in flex so I can populate a form with the same fields.  Record ID, First Name, Last Name.

I have been pointed in the right direction thus far (thanks, zadoc)  and I am hoping I can wrap my head around this soon.

Below is my CFC and Flex code.  The first half of my CFC inserts a record and returns a record id.  The second query gets the record and creates a struct...and returns it.

Thank You!
<!--- Coldfusion CFC--->
 
<cfcomponent output="false">
    
    <!---Insert New Customer--->
<cftransaction>
     <cffunction name="insertCustomer" access="remote" returntype="struct">
		<cfargument name="firstName" type="string" required="true"/>
        		<cfargument name="lastName" type="string" required="true"/>
 
		<cfquery name="insertCustomer" datasource="mydatasource" >
			insert		
			into		customers
						(firstName, lastName)
			values		(<cfqueryparam cfsqltype="cf_sql_varchar" value="#firstName#"/>,
            					<cfqueryparam cfsqltype="cf_sql_varchar" value="#lastName#"/>)
                        
                        SELECT Scope_Identity () as quoteID         
		</cfquery>     
        
    <cfset quoteID = #insertCustomer.quoteID#>
    
    <cfquery name="getCustomer" datasource="mydatasource">
    SELECT recordID, firstName, lastName
    FROM customers
    WHERE recordID = #quoteID#
    </cfquery>
    
    <cfset objCustomer = {
	recordID = "#getcustomer.recordID#",
	firstName = "#getcustomer.firstName#",
	lastName = "#getCustomer.lastName#"
	} />
    
    <cfreturn insertCustomer/>
    </cffunction>
</cftransaction>
</cfcomponent>
 
<!---Flex Code --->
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
 
	<mx:RemoteObject id="conn" destination="ColdFusion" source="simpleform" showBusyCursor="true">
		<mx:method name="insertCustomer"/>
	</mx:RemoteObject>
 
 
<mx:Script>
	<![CDATA[
		 import valueObjects.Customer;
       		 import mx.rpc.events.ResultEvent;
       		 import mx.collections.ArrayCollection;
        
        [Bindable]
        private var customerInfoData:ArrayCollection;
        
        	private function init():void{
           		populateCustomerInfoData();
           }
           
           
            private function clickHandler():void {			
				conn.insertCustomer(inputFirstName.text,inputLastName.text);
				inputFirstName.text = "";
				inputLastName.text = "";
				
			} 
            
             private function populateCustomerInfoData():void{
             customerInfoData = new ArrayCollection
             
            var customerInfo:Customer = new Customer();
					customerInfo.recordID = 27;
					customerInfo.firstName = "Tom";
					customerInfo.lastName = "Jones";
					
			var customerInfo2:Customer = new Customer();
					customerInfo2.recordID = 32;
					customerInfo2.firstName = "Anne";
					customerInfo2.lastName = "Johnson";
					
					customerInfoData.addItem(customerInfo);
					customerInfoData.addItem(customerInfo2);
             
             }
 
	]]>
</mx:Script>
 
		<mx:VBox>
 
					<mx:Form id="inputForm">
							<mx:FormItem label="First Name">
							<mx:TextInput id="inputFirstName"/>
							</mx:FormItem>
							<mx:FormItem label="Last Name">
							<mx:TextInput id="inputLastName"/>
							</mx:FormItem>
						</mx:Form>
						
			<mx:Button label="Save" click="clickHandler()"/>
		
						<mx:Form id="resultsForm">
							<mx:FormItem label="First Name">
							<mx:Text id="resultsFirstName"/>
							</mx:FormItem>
							<mx:FormItem label="Last Name">
							<mx:Text id="resultsLastName" />
							</mx:FormItem>
							<mx:FormItem label="Record ID">
							<mx:Text id="resultsRecordID"/>
							</mx:FormItem>
						</mx:Form>
						
	<mx:DataGrid id="orderGrid" width="500" height="75" dataProvider="{customerInfoData}" y="56" x="262" >
		<mx:columns>
			<mx:DataGridColumn headerText="ID" width="10" dataField="recordID" />
			<mx:DataGridColumn headerText="First Name" width="35" dataField="firstName" />
			<mx:DataGridColumn headerText="Last Name" width="35" dataField="lastName" />
		</mx:columns>
	</mx:DataGrid>
						
		
		</mx:VBox>
	
</mx:Application>

Open in new window

tomhwttAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Andrew MaurerCommented:
Yikes! what happened to your result and fault handler methods? Right now, you are making a call to the CF service but you dont handle the result of the data.

put your result handler functions back in, then set a break-point within it. If you browse the data in the debugger, you'll be able to see how to access it.

Also.. for being new at this stuff.. ArrayCollection is like a CFQuery object.

Here is the code you posted in another thread.
        <mx:RemoteObject id="conn" destination="ColdFusion" source="simpleform" showBusyCursor="true">
                <mx:method name="insertCustomer" result="insertHandler()" fault="mx.controls.Alert.show(event.fault.faultString)"/>
        </mx:RemoteObject>
 
<mx:Script>
        <![CDATA[
            import mx.rpc.events.ResultEvent;
        
                        private function insertHandler():void {
                                inputFirstName.text="";
                                inputLastName.text="";
                                
                                }
                        
                        private function clickHandler():void {                  
                                conn.insertCustomer(inputFirstName.text,inputLastName.text);
                                
                        } 
        ]]>
</mx:Script>

Open in new window

0
Andrew MaurerCommented:
Now add a param to the insertHandler
 private function insertHandler(event:ResultEvent):void {
                                inputFirstName.text="";
                                inputLastName.text="";
                                
                                }

Open in new window

0
tomhwttAuthor Commented:
thanks for the help, zadoc... I am going to get back at this tomorrow.

My brain is officially fried!  :)
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

tomhwttAuthor Commented:
zadoc,

I changed things up a little because...to be honest I am completely lost.  I studied structs quite heavily this weekend and I think I understand.

Where I am having the problem is the flex code.  I found a good tutorial that showed me how to return a variable from a cfc and when I click a button...it appears in the text box.  Good Stuff.

I broke everything down to the basics to try and understand this.

I am including my cfc with a function that returns a struct.  I then have a flex page where I click the button, it calls the cfc and I want to have the first name and last name fill in the perspective textInput blocks.

I tried the debugging perspective and I cannot see a thing.  I am sure i will learn more as I go.

Can you help me complete the flex code needed to make this work?  I have searched every blog and tutorial on the internet and cannot find it (or don't understand what i am reading :)

Thanks

Tom
<!--- CFC --->
 
<cfcomponent name="Customer">
  <cffunction name="getCustomer" access="remote" returnType="struct">
  
  <cfset objCustomer = {
	firstName = "Tom",
	lastName = "Jones"
	} />  
    <cfreturn objCustomer/>
  </cffunction>
</cfcomponent>
 
<!--- Flex --->
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  <mx:RemoteObject id="returnstruct" destination="ColdFusion" source="returnstruct" result="remotingCFCHandler(event)"/>
 
  <mx:Script>
  <![CDATA[
    import mx.rpc.events.ResultEvent;
    
    private function remotingCFCHandler(e:ResultEvent):void
    {
      firstName.text = I AM LOST :);
      lastName.text = I AM LOST :);
    }
 
    private function callRemotingCFC():void
    {
      returnstruct.getCustomer();
    }
    ]]>
  </mx:Script>
  <mx:VBox>
    <mx:TextInput id="firstName" />
    <mx:TextInput id="lastName" />
    <mx:Button label="Show" click="callRemotingCFC()"/>
  </mx:VBox>
</mx:Application>

Open in new window

0
Andrew MaurerCommented:

private function remotingCFCHandler(e:ResultEvent):void
    {
      firstName.text = e.result.FIRSTNAME;
      lastName.text = e.result.LASTNAME;
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andrew MaurerCommented:
the data that is returned from the cfc will be in
e.result

Set a breakpoint and look at it in debug... it'll open a world to you...
0
tomhwttAuthor Commented:
Zadoc,

Do I set the breakpoint on the curly brace?  Do I look at the Variables screen in the degug perspective?

Thanks
0
Andrew MaurerCommented:
private function remotingCFCHandler(e:ResultEvent):void
    {
      firstName.text = e.result.FIRSTNAME;
      lastName.text = e.result.LASTNAME;
DOUBLE CLICK HERE
    }


AND YES    =)
0
Andrew MaurerCommented:
private function remotingCFCHandler(e:ResultEvent):void
    {
OR HERE
      firstName.text = e.result.FIRSTNAME;
      lastName.text = e.result.LASTNAME;
    }
0
Andrew MaurerCommented:
expand e
then expand result

you should see your vars FIRSTNAME and LASTNAME  in the list
0
tomhwttAuthor Commented:
SWEEEET!  There it is...clear as day :)

It is starting to make sense.

Thank you so much for all the help this weekend..

Tom
0
Andrew MaurerCommented:
YW =)

I've learned a lot just by browsing the variables in debug perspective.
0
tomhwttAuthor Commented:
Yeah... that is very cool.  Now that I know what I am looking for... it sure will help a lot.

before this weekend I did not even know there was a debugging perspective :)

Thanks again...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Flex

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.