function calls in cfscript

New Here ,
Oct 07, 2014 Oct 07, 2014

Copy link to clipboard

Copied

i am looking for an example of using cfscript to pass varibles into a query  and NOT HARDCODED into the addParam value attribute.

Sugesstions ?

Views

160

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Oct 08, 2014 Oct 08, 2014

Copy link to clipboard

Copied

LATEST

You could just create your own custom function. To illustrate, take the following example from the documentation:

<cfscript>

    queryService = new query();

    queryService.setDatasource("cfdocexamples");

    queryService.setName("getParks");

    queryService.addParam(name="state",value="MD",cfsqltype="cf_sql_varchar");

    queryService.addParam(value="National Capital Region",cfsqltype="cf_sql_varchar");

    result = queryService.execute(sql="SELECT PARKNAME, REGION, STATE FROM Parks WHERE STATE = :state and REGION = ? ORDER BY ParkName, State ");

    getParks = result.getResult();

</cfscript>

<cfdump var="#getParks#">

You could customize it as follows:

<cfscript>

    queryService = new query();

    queryService.setDatasource("cfdocexamples");

    queryService.setName("getParks");

    qParams = getQueryParams();

    result = queryService.execute(sql="SELECT PARKNAME, REGION, STATE FROM Parks WHERE STATE LIKE '#qParams.stateFirstLetter#%' and REGION LIKE '#qParams.regionFirstLetter#%' ORDER BY State, ParkName");

    getParks = result.getResult();

    function getQueryParams() {

        var myParams=structnew();

        myParams.stateFirstLetter = "M";

        myParams.regionFirstLetter = "N";

        return myParams;

    }

</cfscript>

<cfdump var="#getParks#">

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines