1 Reply Latest reply on Oct 8, 2014 8:28 AM by BKBK

    function calls in cfscript

    jbird4k2

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

      Sugesstions ?

        • 1. Re: function calls in cfscript
          BKBK Adobe Community Professional & MVP

          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#">