How to pass a struct to Coldfusion CFC using CFINVOKE? - coldfusion

I have a CFC file which handles all of the emails I'm sending form an application (using Coldfusion8).
I was using CFINVOKE to call the respective function inside this CFC and passed a struct with all user data along like so:
<cfscript>
var User.data = {};
User.data.name = "John Doe";
User.data.email = "john#doe.com";
...
</cfscript>
// call mailer
<cfinvoke component="mailer_user" method="say_hi">
<cfinvokeargument name="userData" value="#User.data#">
</cfinvoke>
And inside my mailer.cfc
<cffunction name="say_hi" access="public" output="false">
<cfargument name="userData" type="struct" required="true" />
....
For some reason this now fails and I can only get it to work if I pass fields seperately as cfargument, which is a a pain, since I'm passing a lot of data.
Question:
How can I get this to work using argumentCollection.
Even if I CFINVOKE like this:
<cfinvoke component="mailer_user" argumentcollection="#User.data#" method="say_hi"></cfinvoke>
it still doesn't do a thing. I'm setting output flags right before the cfinvoke and after, as well as inside the "say_hi" function going in and out. I'm only getting the flag before CFINVOKE.
Side note: This is all done through AJAX and I'm only getting back success="false" if my CFC has an error somewhere. I only work remotely on the system, so I can't set AJAX debugging in CFADMIN

As I typed the comment above it occurred to me what the problem is likely to be.
You are passing in a structure to your function. You pass User.data which has name,email,blah,etc as keys in that structure. Those keys need to match the arguments in your function
<cffunction name="say_hi" access="public" output="false">
<cfargument name="name" type="struct" required="true" />
<cfargument name="email" type="struct" required="true" />
<cfargument name="blah" type="struct" required="true" />
<cfargument name="etc" type="struct" required="true" />
If you want to pass in the structure as a argument, you would need to have a user.userData as your structure of user data and your function should be
<cffunction name="say_hi" access="public" output="false">
<cfargument name="userData" type="struct" required="true" />
When you pass the collection as argumentCollection you should do argumentCollection="#user#", so that the userData part matches your cfargument in the function.
Clear as mud?

I think you should stay in cfscript style by writing
// call mailer
mailUser = createObject("component", "mailer_user"); // or new mailer_user(); for CF9+
mailUser.say_hi(User.data);
That should work, if it doesn't, it's somewhere else in your code. Try looking at the error log.

You should map the variable to the data you pass, then no problem sending a struct. Do it this way
<cfset objMailer = createObject("component","mailer_user") />
<cfset objMailer.say_hi(userData:user.data)/>
This works even in CF7.

Ok. There was a typo inside my mailer CFC, where I had a variable with "##". As is was inside my email text
it went unnoticed...
So you can pass a struct allright using this:
<cfinvoke component="mailer_user" method="say_hi">
<cfinvokeargument name="userData" value="#User.userdata#">
</cfinvoke>
and grab it inside your called function like so:
<cffunction name="say_hi" access="public" output="false" hint="">
<cfargument name="userData" type="struct" required="true" hint="user data passed" />
<cfscript>
var internalInfo = "";
var User = {};
User.userdata = userData;
</cfscript>
...
Maybe someone else can use the snippet.

Related

Coldfusion and Related Selects

For several years, I have used dropdowns to determine the requirements for a SQL query and have never needed to use dropdowns with ‘related selects’ until now. I found a sample on how to do this and I can create related selects (with static data) which works well, see below using my data relating to vehicles and makes.
However, when I come to apply this to my environment, and use dynamic SQL query data, I cannot get it to work. I am confident that all the pieces of code are working together (as it works perfectly for the static data), but if I try and build the data automatically and then create the string to pass to the queryNew function, it fails.
This is the working Content.cfc file with STATIC query strings/data array
<cfcomponent>
<cfset tblMake = queryNew("name,id", "varchar,varchar", [{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'}])>
<cfset tblModel = queryNew("name,code,continent_id", "varchar,varchar,varchar", [{name:"TSERIES",code:"TSERIES",continent_id:"RENAULT"},{name:"MASTER",code:"MASTER",continent_id:"RENAULT"}])>
<cfset tblVoltage = queryNew("name,code", "varchar,varchar", [{name:"24 volt",code:"MASTER"},{name:"12 volt",code:"TSERIES"}])>
<cffunction name="getContent" access="remote" returntype="query" output="true">
<cfargument name="strTableName" type="string" required="true">
<cfargument name="strID" type="string" required="true">
<cfargument name="strName" type="string" required="true">
<cfargument name="intDistinct" type="numeric" required="false" default="0">
<cfargument name="selectedCol" type="string" required="false" default="0">
<cfargument name="selectedID" type="string" required="false" default="0">
<cfquery name="qryContent" dbtype="query">
select
<cfif arguments.intDistinct eq 1>distinct</cfif>
#arguments.strID# as theID,
#arguments.strName# as theValue
from #arguments.strTableName#
<cfif arguments.selectedID neq 0>
where #arguments.selectedCol# = '#arguments.selectedID#'
</cfif>
order by #arguments.strName#
</cfquery>
<cfreturn qryContent />
</cffunction>
</cfcomponent>
When I try and create the query using this method, nothing appears to work. I am looping through the database (from a query) and then creating a string using the required structure and the passing it to the function.
<cfcomponent>
<cfset XSTATIC = ""/>
<cfquery name="NOXVehicleModels" datasource="EBSNOX" >
SELECT DISTINCT VehicleMake
FROM [dbo].[NOX-Master]
WHERE VehicleMake IS NOT NULL
</cfquery>
<cfloop query="NOXVehicleModels">
<cfset XSTATIC = XSTATIC & "{name:'" & #Trim(NOXVehicleModels.VehicleMake)# & "',id:'"& #Trim(NOXVehicleModels.VehicleMake)# & "'},"/>
</cfloop>
<cfset XLEN=LEN(#XSTATIC#)/>
<cfset XSTATIC = MID(XSTATIC,1,XLEN-1)/>
<cfoutput>#XSTATIC#</cfoutput>
<cfset tblMake = queryNew("name,id", "varchar,varchar", [#XSTATIC#])>
I have created a separate cfoutput to test the string for structure etc and it appears to be correct, but it is just not passing to the querynew function. This is what the output looks like:-
{name:'CUMMINS',id:'CUMMINS'},{name:'DAF',id:'DAF'},{name:'IVECO',id:'IVECO'},{name:'MAN',id:'MAN'},{name:'MERCEDES',id:'MERCEDES'},{name:'RENAULT',id:'RENAULT'},{name:'SCANIA',id:'SCANIA'},{name:'VOLVO',id:'VOLVO'}
{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'}
What I have done so far:-
I have checked that the quotes are not important i.e. single/double.
I have tried to build the query outside of the tag.
Again, all appears (?) to be in order and I cannot understand that could be the problem. Possibly the structure is missing something before parsed to the function?
Any help would be greatly appreciated.
Thanks,
Jack

a query string into a structure

I am using taffy and am passing an unknown query string to a function. I do not know the query string values passed in advance, so I am trying to use that in the function but it is not working. Please point me to right direction.
Here is my code:
<cffunction name="qrystringToStruct" returntype="any">
<cfargument name="myStruct" default="#structNew()#" type="struct">
<cfargument name="str" required="true" default="">
<cfscript>
for(i=1; i LTE listLen(arguments.str,'&');i=i+1) {
structInsert(myStruct, i, listGetAt(arguments.str,i,'&'));
}
</cfscript>
<cfreturn myStruct>
</cffunction>
<cffunction name="getCourseById" taffy:verb="get" taffy:docs:hide>
<cfargument name="structurl" type="any" default="" />
<cfdump var="#structurl#">
<cfdump var="#qrystringToStruct(structurl)#" abort>
<cfset var local = {} />
This is how I am calling the url:
http://localhost:9002/taffy/index.cfm//coursesMethods?credits=3&coursetitle=power
but all I am getting is [empty string]
Let me just preface this by saying I've never used Taffy. However with that said, I don't think it's relevant to the problem specified in your posted question. There are a few things in your code that's puzzling to me.
Your call qrystringToStruct(structurl) passes one parameter but your function definition has two parameters.
Why would you declare myStruct as a parameter and then <cfreturn myStruct> in your qrystringToStruct function definition? It makes no sense.
You say you pass the full url to http://localhost:9002/taffy/index.cfm//coursesMethods?credits=3&coursetitle=power? Why not just pass the querystring portion using cgi.QUERY_STRING?
Anyhow, I think you're overcomplicating this and you don't need a custom function to parse out your querystring. All you need is one line of code.
<cfset qryString = listToArray(cgi.QUERY_STRING, "&")>
You can test it out here here.

How to display <cfreturn> output from a CFC being called from a form

I have a web form which uses the action attribute to call a CFC like this:
<form action="mycfc.cfc?method=registeruser">
The CFC processes the data in the form and then I want it to return a variable telling the user if their form submission has been successful or not.
So within my CFC, I put this:
<cffunction name="registeruser" access="remote" hint="registers a new user" returnformat="JSON">
... PROCESSES FORM HERE THEN...
<cfset Msg = 'Success'>
<cfreturn Msg>
<cflocation url = "/registrationpage.cfm">
</cffunction>
How do I display the Msg variable in the registrationpage.cfm page? My output is set to JSON so I guess I have to DeSerialize but I have no idea how to actually reference/access this output from the method.
My whole answer is for educationnal purposes only and I strongly advise you to use an existing framework rather than reinventing the Wheels. Have a look at Picking a ColdFusion MVC Framework
You can store the value in the session scope. A lot of frameworks does it using a flash memory concept, which is a temporary memory (implemented as a struct) that destroys members when accessed.
Have a look at http://cfwheels.org/docs/1-1/chapter/using-the-flash it's quite straight forward to implement an API that does this.
Client code could look like (depending on your implementation):
<cfset session.flash.set('importantMsg', 'some important msg')>
<cflocation ...>
Then from the other page:
<cfif session.flash.has('importantMsg')>
<!--- The following line should also destroy the 'importantMsg' key --->
#session.flash.get('importantMsg')#
</cfif>
Here's an implementation example (not that the implementation is not thread-safe):
FlashMemory.cfc
<cfcomponent>
<cffunction name="init" returntype="FlashMemory">
<cfset variables.instance = {flash = {}}>
</cffunction>
<cffunction name="set" returntype="void">
<cfargument name="key" type="string" required="true">
<cfargument name="value" type="any" required="true">
<cfset variables.instance.flash[arguments.key] = arguments.value>
</cffunction>
<cffunction name="get" returntype="any">
<cfargument name="key" type="string" required="true">
<cfset var v = variables.instance.flash[arguments.key]>
<cfset structDelete(variables.instance.flash, arguments.key)>
<cfreturn v>
</cffunction>
<cffunction name="has" returntype="boolean">
<cfargument name="key" type="string" required="true">
<cfreturn structKeyExists(variables.instance.flash, arguments.key)>
</cffunction>
</cfcomponent>
onSessionStart
<cfset session.flash = new FlashMemory()>
Also please note that in your case, your remote CFC methods shouldn't return anything. You will use the flash memory to pass data around instead. That means when the method has finished it's work you can simply redirect the client.
You probably shouldn't use remote CFC methods in this particular case:
I have never really used remote CFC methods as stateful web services. The various advantages of remote CFC methods like their ability to spit out data in multiple data-interchange formats (JSON, WDDX...) is lost with your implementation.
You could simply do something like:
registration.cfm
<cfset errors = session.flash.get('registrationErrors')>
<cfif arrayLen(errors)>
<!--- Display errors --->
</cfif>
<form method="post" action="register.cfm">
...
</form>
register.cfm
<cfset registration = new Registration(argumentcollection = form)>
<cfset validator = new RegistrationValidator(registration)>
<cfif validator.validate()>
<cfset application.userService.register(registration)>
<!--- You can also redirect to a page that represents the correct state --->
<cflocation url="registered.cfm" addtoken="no">
<cfelse>
<!--- Store the errors collection in the flash memory --->
<cfset session.flash.set('registrationErrors', validator.errors())>
<!--- Redirect to the page the user came from --->
<cflocation url="#cgi.http_referer#" addtoken="no">
</cfif>
Take the cflocation tag out of your function. It will not execute anyway because it's after the cfreturn tag.
Also, post your form to a .cfm page, not the .cfc. From that .cfm page, do this:
<cfset MyObject = CreateObject(stuff for your cfc goes here)>
<cfset MessageFromFunction = MyObject.registeruser()>
<cfoutput>#MessageFromFunction</cfoutput.

Access function arguments from another function

I am trying to create a custom debug tool and I need to use a component with two separate functions in it. The first function (startTimer) has some arguments such as startCount and the other one (endTimer) has endCount. What I am trying to accomplish is something like the following code:
<cffunction name="startTimer" access="public" returntype="void">
<cfargument name="startActionTime" type="string" required="no">
</cffunction>
<cffunction name="endTimer" returntype="void" access="public">
<cfargument name="endActionTime" type="string" required="no">
<cfset finalTime = endActionTime - startTimer.startActionTime>
<!---Some SQL will go here to record the data in a db table --->
</cffunction>
And this is how I am calling the function
<cfscript>
location = CreateObject("component","timer");
loc =location.startTimer(
startActionTime = getTickCount()
);
end = location.endTimer(
endActionTime = getTickCount()
);
</cfscript>
I guess I am having scope issues because when I am trying to run the code I am getting an undefined error on startTimer.startActionTime. What is the correct way to do something like this?
You can use the variables scope like so:
<cfcomponent>
<cfset variables.startActionTime = 0>
<cffunction name="startTimer" access="public" returntype="void">
<cfargument name="startActionTime" type="numeric" required="no">
<cfset variables.startActionTime = arguments.startActionTime>
</cffunction>
<cffunction name="endTimer" returntype="string" access="public">
<cfargument name="endActionTime" type="numeric" required="no">
<cfset finalTime = endActionTime - variables.startActionTime>
<!---Some SQL will go here to record the data in a db table --->
<Cfreturn finaltime>
</cffunction>
</cfcomponent>
From the Adobe Docs: Variables scope variables created in a CFC are available only to the component and its functions, and not to the page that instantiates the component or calls its functions.

Testing for existence of FORM scope / struct in ColdFusion

Problem: When requesting the WSDL for a CFC, I get the following error: Variable FORM is undefined. It happens in this line of code, in the OnRequestStart method in application.cfc
<cfif structKeyExists(form,'resetappvars')>
<cfset OnApplicationStart() />
</cfif>
If I request a specific method, it works fine. I have considered using cfparam to create a default form struct if none exists, but that seems like an ugly hack and I worry it will actually create the form struct in the variables or this scope of the CFC. Maybe this is a legitimate bug as well?
Note: This only happens when I request the WSDL, if I invoke a method directly - the code executes as expected without problems.
Update: Application.cfc code sample - just add any CFC to your app and request it with ?wsdl to see the issue. This has been tested (and failed) on ColdFusion 7 and ColdFusion 8.
<cfcomponent output="false">
<cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false" hint="Fires when the application is first created.">
<cfset application.dsn = "my_dsn" />
<cfreturn true />
</cffunction>
<cffunction name="OnRequestStart" access="public" returntype="boolean" output="false" hint="Fires at first part of page processing.">
<cfargument name="TargetPage" type="string" required="true" />
<cfif structKeyExists(form,'resetappvars')>
<cfset OnApplicationStart() />
</cfif>
<cfreturn true />
</cffunction>
</cfcomponent>
Maybe try adding a:
<cfif IsDefined("form")>...</cfif>
around the above code?
You could also cfparam the variable you're looking for then just change your logic a little (assuming resetAppVars is a boolean:
<cfparam name="form.resetAppVars" default="false" />
...
<cfif form.resetAppVars>
<cfset OnApplicationStart() />
</cfif>
Edit: I'm not sure if the above code could be considered a hack, but it seems pretty standard CF, to me.
This post of Ben Nadel gives detailed list of scopes available for different types of requests.
By reading it you can easily find out that form scope is not available in given context, but url is.
I've heard it's just a matter of opinion, but it seems to me that it is improper to reference your form scope within a CFC, as there is no guarantee that the form scope will be available when your cfc is invoked and when your method is called. It is better to ensure that any data that needs to be available to the method is provided explicitly to your object. This can be done either by including an argument:
<cfargument name="resetAppVars" type="boolean" required="false" default="false" />
Then you check arguments.resetAppVars, and it is always defined, but defaulted to false.
Or by creating an attribute on your object and creating an explicit set method:
(at the top of your cfc)
<cfset this.resetAppVars = false />
<cffunction name="setResetAppVars" access="public" returnType="void" output="false">
<cfargument name="flagValue" type="boolean" required="true" />
<cfset this.resetAppVars = arguments.flagValue />
</cffunction>
In which case you will check against this.resetAppVars. You can also scope this locally using <cfset var resetAppVars = false /> as the declaration, which makes it a private attribute of your object, and is probably proper, so code that invokes the object cannot improperly overwrite this variable with a non-boolean type. In that case, you would simply refer directly to resetAppvars in your test, instead of using this scope.
You could also do this:
<cfif NOT isSoapRequest()>...
and stick your remaining logic inside that chunk.