Skip to main content
Participating Frequently
November 29, 2017
Answered

cfscript syntax for functions(methods) in componets

  • November 29, 2017
  • 3 replies
  • 3886 views

I've got the following function written in tag syntax:

************

<cffunction name="myQuery" returntype="query" output="false" access="remote" hint="run some query">
   <cfargument name="someRequiredID" required="true" default="24">
  
<cfquery name="local.getThings">
  SELECT ID, entry_title
  FROM entries
  WHERE blogID =
<cfqueryparam value="#ARGUMENTS.blogid#"
  
cfsqltype="cf_sql_integer">
   
</cfquery>

  
<cfreturn local.getThings>
</cffunction>

****************

How can I rewrite this using script syntax?

ie

component  displayname="testQuery"

{

     // confusion starts here ??

     // define method myQuery

     public query function myQuery() {

          //run some query

          peopleQuery = new Query(sql="SELECT * FROM PEOPLE").execute().getResult();

          return peopleQuery;

    

     }

}

*****

Essentially, I'm not sure how to handle arguments passed to the function - and how to set the access="remote" and returnType ="query"

when using cfscript syntax.

This topic has been closed for replies.
Correct answer Charlie Arehart

illiquent, you put those "other attributes" AFTER the argument definitions. So for instance, to make a method accessible to "remote" calls, use:

function testmethod (numeric x access="public" returntype="numeric") {

    return arguments.x+1;

}

[Update: after posting this, I double-checked my work and found that I was mislead in my testing to suggest the above. And I was misreading the doc below. Fortunately, what I wrote next is indeed accurate.]

You can also put them after the closing parenthesis of the methods args, as in:

function testmethod (numeric x) access="public" returntype="numeric" output="false" {

    return arguments.x+1;

}

Then again, as you already know from your example above, you can also declare the first two of those specific "attributes" implicitly at the front of the method declaration, but others do need to be after the close paren:

public numeric function testmethod (numeric x) output="false" {

    return arguments.x+1;

}

As for how one would know that they can provide what would have been cffunction attributes to a script function in the way I show above, t is indeed documented, as wolfshade indicated. The problem is that it's not in the CFML reference. Instead, see see ColdFusion Help | Defining components and functions in CFScript , which says (rather subtly and buried among lots of other info):

"The syntax to define a function is similar to the component definition:

...[snip]...

access returnType function functionName(arg1Type arg1Name="defaultValue1"

arg1Attribute="attributeValue...,arg2Type

arg2Name="defaultValue2" arg2Attribute="attributeValue...,...)

functionAttributeName="attributeValue" ... {

and the "arg1Attribute" "functionAttributeName" is an example of what they mean by what would have been an attribute to a cffunction, and note that it appears AFTER the closing parenthesis of the args.

[another edit after original posting:]

As for what they mean by the references to arg1attribute above, I honeslty don't know. the reference to 'arg1type arg1name="defaultValue1"' should be all that is needed for each arg. Anyone else know what the "arg1attribute" would be about?

3 replies

Charlie Arehart
Community Expert
Community Expert
November 30, 2017

I'll add one more answer: I just confirmed that Pete's nifty http://cfscript.me/ tool (an online tool for converting CF tags to script) does indeed properly handle the conversion of CFFUNCTION attributes to appearing as a parenthesized list of args after the first parenthesized list of any actual args for the method. (And it shows also how the returntype and access can be declared BEFORE the method name, like I mentioned above. But if you try something like output or returnformat, you'll see those get put AFTER the method args.)

/Charlie (troubleshooter, carehart. org)
Carl Von Stetten
Legend
November 29, 2017

Something like this:

remote query function myQuery( required numeric SomeRequiredID ) {

...

}

or 

remote query function myQuery( numeric SomeRequiredID=24 ) {

...

}

You shouldn't mix required and default together in the same argument, since supplying a default value will negate it being required.

Also, if you are on CF11 or above, use queryExecute() instead of new Query().  The Query CFC provided by Adobe has numerous bugs and they are not being fixed, so treat it as a deprecated function.

-Carl

illiquentAuthor
Participating Frequently
November 29, 2017

Thanks. That's what I eventually came up with as well.

Is there some place that shows how to use all the equivalent attributes for cffunction?

ie

access returntype function someFunctionName(required numeric someID)

deals with access, returntype, and required attributes from cffunction

how does one uses the other attributes (roles,resultFormat,httpMethod,produces,consumes, etc)?

WolfShade
Legend
November 30, 2017

I think that there is an helpx.adobe.com page for it.  I'd provide the link, but our network security is so tight that I cannot access the link from work.  (Supposedly there is a fix being readied for that; but no timeline on when it will be finished or implemented.)

Google "function cfscript" and find the helpx.adobe.com page for it.

V/r,

^ _ ^

WolfShade
Legend
November 29, 2017

I did a quick Google search, and found the following link that may be helpful:

http://www.isummation.com/blog/cfquery-using-cfscript/

HTH,

^ _ ^

illiquentAuthor
Participating Frequently
November 29, 2017

That link definitely helps with the query part of the function.
I'm still confused on how to replace cffunction with all its attributes with the cfscript syntax.

ie

public returnType? function runQuery() {

//where to specify attributes like name, access, returntype??

//do a query and return object

return queryObj;

}

WolfShade
Legend
November 29, 2017

You can't see it, but I'm facepalming myself, right now, for having flaked on the biggest part of your request. 

Hopefully this link will help with that.

https://cfdocs.org/cffunction

V/r,

^ _ ^