Contents > Developing ColdFusion MX Applications > Using Web Services > Handling complex data types > Consuming web services that use complex data types Handling return values as complex types PreviousNext

Handling return values as complex types

When a web service returns a complex type, you can write that returned value directly to a ColdFusion variable.

The previous section used a complex data type named Employee to define an input parameter to an operation. A WSDL file can also define a return value using the Employee type, as the following code shows:

<message name="updateEmployeeInfoSoapOut"> 
   <part name="updateEmployeeInfoResult" type="s0:Employee" /> 
</message>
<operation name="updateEmployeeInfo"> 
   <input message="s0:updateEmployeeInfoSoapIn" /> 
   <output message="s0:updateEmployeeInfoSoapOut" /> 
</operation>

In this example, the operation updateEmployeeInfo takes a complex type as input and returns a complex type as output. To handle the input parameter, you create a structure. To handle the returned value, you write it to a ColdFusion variable, as the following example shows:

<!--- Create a structure using CFScript, then call the web service. --->
<!--- Write the returned value to a ColdFusion variable. --->
<cfscript>
   stUser = structNew();
   stUser.active = TRUE;
   stUser.fname = "John";
   stUser.lname = "Smith";
   stUser.age = 23;
   stUser.hiredate = createDate(2002,02,22);
   stUser.number = 123.321;

   ws = createObject("webservice", "http://somehost/echosimple.asmx?wsdl");
   myReturnVar = ws.echoStruct(stUser);

</cfscript> 

<!--- Output the returned values. --->
<cfoutput>
   <br>  
   <br>Name of employee is: #myReturnVar.fname#  #myReturnVar.lname#
   <br>Active status: #myReturnVar.active#
   <br>Age:  #myReturnVar.age#
   <br>Hire Date: #myReturnVar.hiredate#
   <br>Favorite Number: #myReturnVar.number# 
</cfoutput>

You access elements of the variable myReturnVar using the dot notation in the same way you access structure fields. If a complex type has nested elements, in the way a structure can have multiple levels of nested fields, you use dot notation to access the nested elements, as in a.b.c.d, to whatever nesting level is necessary.

However, the variable myReturnVar is not a ColdFusion structure. It is a container for the complex type, but has none of the attributes of a ColdFusion structure. Calling the ColdFusion function isStruct on the variable returns False.

You can copy the contents of the variable to a ColdFusion structure, as the following example shows:

<cfscript>
...
   ws = createObject("webservice", "http://somehost/echosimple.asmx?wsdl");
   myReturnVar = ws.echoStruct(stUser);

   realStruct = structNew();
   realStruct.active = #myReturnVar.active#;
   realStruct.fname = "#myReturnVar.fname#";
   realStruct.lname = "#myReturnVar.lname#";
   realStruct.age = #myReturnVar.age#;
   realStruct.hiredate = #myReturnVar.hiredate#;
   realStruct.number = #myReturnVar.number#;

</cfscript> 

Calling IsStruct on realStruct returns "True" and you can use all ColdFusion structure functions to process it.

This example shows that ColdFusion variables and structures are useful for handling complex types returned from web services. To understand how to access the elements of a complex type written to a ColdFusion variable, you have to inspect the WSDL file for the web service. The WSDL file defines the API to the web service and will provide you with the information necessary to handle data returned from it.


Contents > Developing ColdFusion MX Applications > Using Web Services > Handling complex data types > Consuming web services that use complex data types Handling return values as complex types PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.