Skip to main content
June 5, 2007
Question

CFC Return Message Best Practice

  • June 5, 2007
  • 9 replies
  • 1253 views
Hi,

I have a CFC file with all of my SQL queries. Some return results and some have queries which insert new records.

My question is, if I submit a form on say index.cfm which calls a CFC function to insert a new record, how can I then send a message back to the index.cfm to say what has happened? I use cftransaction and cftry blocks around most queries so a useful message depending on what happens regardless is almost essential.

One way I saw would simply be to do a cflocation with the message append to the URL but for other reasons I don't want to do it this way. e.g. <cflocation url="index.cfm?message=#urlEncodedFormat("Record inserted")#"

But that method above looks dirty. Is there another way? Hopefully a standard way of doing it that will work accross the board.

Many thanks for your advice - anything is much appreciated!

Mikey.

    This topic has been closed for replies.

    9 replies

    BKBK
    Community Expert
    Community Expert
    June 8, 2007
    Also <cfset cfc_info.status = "status_ok" />?

    BKBK
    Community Expert
    Community Expert
    June 7, 2007
    > Is this making any sense?
    Not quite. I don't understand what you mean by expressions "show the latest results on the right." and "refresh the page at the same time it's sending the message back". I think it's simpler to post the code.



    BKBK
    Community Expert
    Community Expert
    June 7, 2007
    The following pseudocode assumes you put all functions in one component.


    June 7, 2007
    Hi BKBK,

    My code is almost the exact same theory as that. Anyway, nevermind that, now I'm getting a bizarre error.

    When I invoke the createItem function I get the error "Variable ERROR is Undefined"...but I don't ask for a variable called error OR even create one so I am puzzled. Check out the code...

    Now the strange thing is, this was working the other day, but now it's not and I havn't touched a thing - I'm really puzzled.

    I have only post the relevant code. CF's exact error message is:

    Variable ERROR is undefined.


    The error occurred in C:\ColdFusion8\wwwroot\coldbox\cfc\system.cfc: line 346
    Called from C:\ColdFusion8\wwwroot\coldbox\index.cfm: line 146
    Called from C:\ColdFusion8\wwwroot\coldbox\index.cfm: line 141
    Called from C:\ColdFusion8\wwwroot\coldbox\index.cfm: line 1
    Called from C: line -1

    344 : <cfcatch type="any">
    345 : <cftransaction action="rollback" />
    346 : <cfset cfc_info.status = status_error />
    347 : <cfset cfc_info.message = "There was a problem creating '#arguments.item_name#'.">
    348 : </cfcatch>



    June 7, 2007
    Hi BKBK,

    Firstly thanks again for your help.

    The form does indeed post to itself on the index.cfm page which also displays the results from another query CFC that simply gets the items.

    The problem is, within my logic (much like you have show above) the message is returned from the CFC as a structure to the page but does not refresh to show the latest results on the right.

    The results as mentioned before is from the invokation of another CFC that simply selects all records from the same table I am using the form to insert data into.

    If I refresh my page after submitting the form, I will then see my item in a table row...but only after I refresh it. I need it to refresh the page at the same time it's sending the message back.

    Is this making any sense?

    In essence there are two CFC functions here. One that displays all items in table "A" and the one that is called to insert data into table "A".

    Perhaps there could be some way of invoking the other CFC at the same time as the message gets sent back? But then again, this in theory wouldn't make a difference as the display CFC is already called at the top of the page.

    I hope you understand what I'm saying - it's like one of those things I could explain to someone very quickly in person but it's a bit tricky with text, ha.

    Many thanks again, you've already been a great help.

    Mikey.
    June 6, 2007
    Ignore what I just said above - I was being ignorent - too ignorent to notice such simple details! haha.

    Thanks for your help BKBK, it works a treat and means I can send back a whole host of things to my page cheers.

    One more thing though...

    The index.cfm I add my items on via a form also displays the items in a table. Because there is no redirect from the CFC like I had before via CFLOCATION the index.cfm page does not refresh and thus I don't see my new item created until I refresh the page.

    Has anyone got a solution to get around this? The returned message displays fine, it's just the page wont display my new item straight after creating it - need to refresh the page, then it appears.

    Hmm.
    BKBK
    Community Expert
    Community Expert
    June 7, 2007
    > The index.cfm I add my items on via a form also displays the items in a table.

    I assume from this that the form page and action page are or should be the same. You could then design it as follows:

    BKBK
    Community Expert
    Community Expert
    June 6, 2007
    > to have the code displaying this message
    You can display the message in any page you like. Suppose the component that contains the function is {webroot}/cfc/validation.cfc. Then you simply have to do something like

    <cfset myStruct = structNew()>
    <cfset myObj = createobject("component","cfc.validation")>
    <cfset myStruct = myObj.func()>
    <!--- perhaps better, as it has no reference to struct in func --->
    <!--- <cfset myStruct = duplicate(myObj.func())> --->
    <p>
    status: <cfoutput>#myStruct.status#</cfoutput><br>
    message: <cfoutput>#myStruct.message#</cfoutput>
    </p>



    June 6, 2007
    Hi BKBK,

    The last bit of cade you have given me throws an error because the function (which is simply an SQL insert) requires an ID value parameter.

    All I want the second code you provided to do is to display any message returned from the CFC function (in the form of a structure) - is this possible?

    I'm not too clever with Coldfusion - the process is kinda meant to work like this...

    1 - Form posts, then after validation success calls the createItem function in a CFC file
    2 - The Function then inserts the form variables into the database.
    3 - The function should then return a message to the page that called it
    4 - The page that called it should now display any of the structure variables passed to it

    Am I making sense? Sometimes I confuse myself! Thanks for your help - it's much appreciated.

    Thanks,

    Mikey.
    June 6, 2007
    BKBK you are a life saver! I haven't tried it yet but the logic looks perfect.

    Is there any way on my index.cfm to have the code displaying this message? e.g. I guess the code needs to know when the structure is returned and what to do with it.

    Do I use the logic of "isDefined my structure then show the variables"?

    I'm not too familiar with structures so you'll have to forgive me on this one!!

    Thanks for your help.
    BKBK
    Community Expert
    Community Expert
    June 6, 2007
    You could return a structure that comprises status, message, and so on.

    June 6, 2007
    Yes, well my insert query method is set to Boolean so I simply do:

    <cfreturn true />

    How could I use this cfreturn to display a message on the page that called it?

    Many thanks,

    Mikey.
    Participating Frequently
    June 5, 2007
    Try <CFRETURN> inside your <CFFUNCTION> in your CFC.