Skip to main content
Participant
October 14, 2009
Question

Pretty simple cfm giving me lots of problems

  • October 14, 2009
  • 3 replies
  • 1680 views

First off, i am pretty new to cold fusion and am attempting to use it as a backend to flex, but on to the question.

I am following along a tutorial in Flex For Developers and am attempting to create a pretty simple form. Using the code below, which seems to follow the book, doesn't work. Any help getting this to work would be great.

<cfprocessingdirective suppresswhitespace="yes" pageencoding="utf-8">
        <cfif isDefined("ReceivedSent") and isDefined("ReceivedSentDate") and isDefined("PhaseID") and isDefined("SubPhase") and isDefined("SubID") and isDefined("SubVendorName")
                and ReceivedSent NEQ "" and ReceivedSentDate NEQ "" and PhaseID NEQ "" and SubPahse NEQ "" and SubID NEQ "" and SubVendorName NEQ "">
        <cfquery name="AddInfo" datasource="mailtracker">
            Insert into MailLog(LoggedDate, Received/Sent, Received/SentDate, PhaseID, SubPhase, SubID, Sub/VendorName, Subject,Remarks)
            Values (getDate(),"#ReceivedSent#","#ReceivedSentDate#","#PhaseID#","#SubPhase#","#SubID#","#SubVendorName#","#Subject#","#Remarks#")
        </cfquery>
        </cfif>
        <cfquery name="allInfo" datasource="mailtracker">
            Select LoggedDate, Received/Sent as ReceivedSent, Received/SentDate as ReceivedSentDate, PhaseID, SubPhase, SubID, Sub/VendorName as SubVendorName, Subject, Subject, Remarks
            From MailLog
        </cfquery>
        <cfxml variable="mesageXML">
            <logs>
                <cfloop query="allInfo">
                    <cfoutput>
                        <maillog>
                            <LoggedDate>#LoggedDate#</Loggeddate>
                            <ReceivedSent>#ReceivedSent#</ReceivedSent>
                            <ReceivedSentDate>#ReceivedSentDate#</ReceivedSentDate>
                            <PhaseID>#PhaseID#</PhaseID>
                            <SubPhase>#SubPhase#</SubPhase>
                            <SubID>#SubID#</SubID>
                            <SubVendorName>#SubVendorName#</SubVendorName>
                            <Subject>#Subject#</Subject>
                            <Subject>#Subject#</Subject>
                            <Remarks>#Remarks#</Remarks>
                        </maillog>
                    </cfoutput>
                </cfloop>
            </logs>
        </cfxml>
        <cfoutput>#messageXML#</cfoutput>
</cfprocessingdirective>
    This topic has been closed for replies.

    3 replies

    sunMessakAuthor
    Participant
    October 14, 2009

    basically, i'm attempting to create a form with the following fields

    LoggedDate, Received/Sent as ReceivedSent, Received/SentDate as ReceivedSentDate, PhaseID, SubPhase, SubID, Sub/VendorName as SubVendorName,Subject, Remarks

    and passing them from flex to cf which in turn inserts them into a msaccess database and displays the results in a grid back in flex.

    Inspiring
    October 14, 2009

    Okay. Thanks for the follow-up. Here's how I do these sorts of things in Flex applications with a CF back-end.

    In Flex, create your form:

    <mx:Form id="myform">

         <mx:FormItem label="The Field">

              <mx:TextInput id="thefield" />

         </mx:FormItem>

         <mx:FormItem label="The Field">

              <mx:Button id="thebtn" click="doIt(event)" />

         </mx:FormItem>

    </mx:Form>

    Setup a RemoteObject. The destination, ColdFusion, should be enabled in any CF8 or CF9 install, if not Google on setting up the services-config.xml and remoting-config.xml files in Flex. The source is the dot-notated path to the CFC, starting at the web root (you can enable ColdFusion mappings to be used instead of this full path but that requires editing the remoting-config.xml file). The mx:method tag or tags are used to list the CFC functions that get invoked and the appropriate event handlers after the CFC function is executed.

    <mx:RemoteObject id="roMyCFC" destination="ColdFusion" source="path.to.cfc.on.server.CFC_NAME_NO_EXTENSION">      <mx:method name="doAction" fault="serverFault(event)" result="yeah(event)" /> </mx:RemoteObject>

    Next, for this example, you need to set up three functions: (1) the doIt method to handle the form submission (invoked by the button's click handler); (2) the serverFault method which handles errors sent back by ColdFusion, and (3) the yeah method for when the CFC method works properly and returns a result.

    private function doIt(event:MouseEvent=null):void{

         // this example only submits a single parameter but you just add more and more as needed

         roMyCFC.doAction(

              this.thefield.text

         );

    }

    private function serverFault(event:FaultEvent):void{

         mx.controls.Alert.show(mx.utils.ObjectUtil.toString(event.fault));

    }

    private function yeah(event:ResultEvent):void{

         mx.controls.Alert.show( event.result as String );

    }

    Finally, you need the CFC and all methods to be used by Flex must have their access set to remote. Because of the ActionScript/Flex 'yeah' method above is expecting a string to be returned from ColdFusion, we set the returntype attribute to string ... and return a string, of course! 

    <cfcomponent>

         <cffunction name="doAction" output="false" access="remote" returntype="string">           <cfargument name="thefield" required="true" />

              <cfscript>

                   // do some stuff (insert into db, etc.)

                   result = "Yeah! Everything was saved";           </cfscript>

              <cfreturn result />      </cffunction>

    </cfcomponent>

    I know this is an overly simplified example, but I hope it shows you how you can better integrate Flex and ColdFusion applications (better than HTTPService and WebService calls, that is!). One final note: all the Flex and ActionScript code I have above would go into the same MXML file.

    ilssac
    Inspiring
    October 14, 2009

    Just note that the database SQL syntax issues and the XML syntax issue (if you choose to return data as XML) is not going to change by using Remote Object rather then HTTPService or a WebService.

    Inspiring
    October 14, 2009

    How are you trying to get Flex and CF to communicate? The ideal approach is to use mx:RemoteObject, which lets you post data from Flex to a CFC and then back again, including converting CF objects to the appropriate Flex objects. It's much easier and a billion times faster to use RemoteObjects over HTTPService calls or Web Service calls -- note: "a billion times faster" may be a slight over estimate .

    As Ian mentioned, post a little more about what you're doing, how you're doing it and what, if any, error details you're receiving.

    ilssac
    Inspiring
    October 14, 2009

    It would really help to know how this code "doesn't work," errors, unexpected behavior, incorrect results?

    It is rather hard to look at a piece of code and advice on it if one does not know what the intention for the code is.

    sunMessakAuthor
    Participant
    October 14, 2009

    Sorry about that, as it sits it gives me a       [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 5. error.

    I tried to simplify the query and just query Subject and Remarks and i got the following error:

    The entity name must immediately follow the '&' in the entity reference.

    ilssac
    Inspiring
    October 14, 2009

    The first is an error being thrown by the Microsoft Access driver, it is not liking your SQL statement.  My first suspicion is those field names with a slash caracter in them, that is unusual and I bet not well supported.  Try escaping those names with square brackets [Received/Sent].  Or better yet, rename the fields not to use unsupported characters.

    Secondly when you simplified your SQL to not use those problematic fields, you exposed another issue where some of your data has the ampersand & character which is a special character in XML structures and can not be part of the data.  ColdFusion provides the XMLformat() function to properly escape data like this.  Wrap all your variables in this function.

    <LoggedDate>#xmlFormat(LoggedDate)#</Loggeddate>