Coldfusion cfselect bind only works when cfdebug is enabled - coldfusion

I have a cfselect statement that is bound to a remote cfc. It passes the value of another element in the form to the cfc in order to populate the cfselect. I was running this code with cfdebug enabled and it seemed to be working fine, but I found that if I do not have cfdebug enabled nothing is populated. I am not getting any other errors that I know of, and I have tried it in chrome and mozilla - same problem.
Here is my cfselect:
<cfselect name="intlRepID" id="intlRepID" required="yes" value="userID" display="businessName" bind="cfc:nsmg.extensions.components.user.getIntlRepRemote({programID})" bindonload="true" />
Here is my remote function:
<cffunction name="getIntlRepRemote" access="remote" returntype="array" output="false" hint="Gets a list of Intl. Reps. assigned to a candidate">
<cfargument name="programID" default="" hint="Get Intl. Reps. Based on a list of program ids">
<cfscript>
// Define variables
var qGetIntlRepRemote='';
var result=ArrayNew(2);
var i=0;
</cfscript>
<cfquery
name="qGetIntlRepRemote"
datasource="#APPLICATION.DSN#">
SELECT
u.userID,
u.businessName
FROM
smg_users u
INNER JOIN
smg_students s ON s.intRep = u.userID
WHERE
s.companyid = <cfqueryparam cfsqltype="cf_sql_integer" value="#CLIENT.companyid#">
<cfif LEN(ARGUMENTS.programID)>
AND
s.programID IN ( <cfqueryparam cfsqltype="cf_sql_integer" value="#ARGUMENTS.programID#" list="yes"> )
</cfif>
AND
s.active = <cfqueryparam cfsqltype="cf_sql_integer" value="1">
GROUP BY
u.userID
ORDER BY
u.businessName
</cfquery>
<cfscript>
// Add default value
result[1][1]=0;
result[1][2]="---- All - Total of " & qGetIntlRepRemote.recordCount & " International Representatives ----" ;
// Convert results to array
For (i=1;i LTE qGetIntlRepRemote.Recordcount; i=i+1) {
result[i+1][1]=qGetIntlRepRemote.userID[i];
result[i+1][2]=qGetIntlRepRemote.businessName[i];
}
return result;
</cfscript>
</cffunction>

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

ColdFusion function returns an error "The system has attempted to use an undefined value"?

I have function that should save some form fields. This function is able to process multiple insert rows. So far I was able to create logic to handle this but the only issue so far I have once user tries to save the record. The server response looks like this:
The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code. Null Pointers are another name for undefined values. coldfusion
At first I was looking and trying to find which value is not defined/missing in my function but so far I couldn't detect the issue. The next thing I did was commenting out the cfquery. After that I was getting message Form successfully saved.. This tells me that my code is breaking inside of the cfquery tag. Here is example of my code:
<cffunction name="saveRecords" access="remote" output="false" returnformat="JSON">
<cfargument name="formID" type="string" required="true" default="">
<cfargument name="status1" type="string" required="true" default="0">
<cfargument name="status2" type="string" required="true" default="0">
<cfargument name="status3" type="string" required="true" default="0">
<cfargument name="status4" type="string" required="true" default="0">
<cfargument name="status5" type="string" required="true" default="0">
<cfargument name="comment1" type="string" required="true" default="">
<cfargument name="comment2" type="string" required="true" default="">
<cfargument name="comment3" type="string" required="true" default="">
<cfargument name="comment4" type="string" required="true" default="">
<cfargument name="comment5" type="string" required="true" default="">
<cfset local.fnResults = structNew() />
<cfset local.runProcess = true />
<cfset local.arrStatus = arrayNew(1) />
<cfloop collection="#arguments#" item="i">
<cfif findNoCase(left(i,6), "status") and arguments[i] eq 1>
<cfset arrayAppend(local.arrStatus, right(i,1)) />
</cfif>
</cfloop>
<cfset local.frmValidation = {
formID : len(arguments.formID),
status1 : ArrayContains([0,1], trim(arguments.status1)),
status2 : ArrayContains([0,1], trim(arguments.status2)),
status3 : ArrayContains([0,1], trim(arguments.status3)),
status4 : ArrayContains([0,1], trim(arguments.status4)),
status5 : ArrayContains([0,1], trim(arguments.status5))
}/>
<cfloop collection="#frmValidation#" item="i">
<cfif !frmValidation[i] or !arrayLen(arrStatus)>
<cfset local.runProcess = false />
<cfset local.fnResults = {status: 400, message: "Form data is either missing or incorrect."}>
<cfbreak>
</cfif>
</cfloop>
<cfif runProcess>
<!---
<cfquery name="saveRec" datasource="testDB">
<cfloop array="#arrStatus#" index="i">
INSERT INTO formDetails (
formid,
refid,
status,
comment,
userid,
lastupdate
)
SELECT
<cfqueryparam cfsqltype="cf_sql_numeric" value="#trim(arguments.formID)#">,
#i#,
<cfqueryparam cfsqltype="cf_sql_bit" value="#trim(arguments["status"&i])#">,
<cfqueryparam cfsqltype="cf_sql_varchar" maxlength="8000" value="#trim(arguments["comment"&i])#" null="#!len(trim(arguments["comment"&i]))#">,
<cfqueryparam cfsqltype="cf_sql_varchar" maxlength="6" value="#trim(session.userid)#" null="#!len(trim(session.userid))#">,
#now()#
WHERE NOT EXISTS (
SELECT refid
FROM formDetails
WHERE formid = <cfqueryparam cfsqltype="cf_sql_numeric" value="#trim(arguments.formID)#">
AND refid = #i#
)
</cfloop>
</cfquery>
--->
<cfset local.fnResults = {status: 200, message: "Form successfully saved!"}>
</cfif>
<cfreturn fnResults>
</cffunction>
One thing that I forgot to mention is that before I commented out cfquery, I tried to save the records and error will occur that I mentioned above but at the same time record is saved in database. That is weird since I didn't expect query to execute successfully because of the error. I use Sybase database and here are the details about formDetails table:
column name data type length
recid numeric 18 PK
formid numeric 10 FK
refid numeric 18 FK
status bit 1
comment varchar 8000
userid varchar 6
lastupdate datetime 23
I'm not sure where my code is breaking and why I'm getting an error message about undefined value. If anyone can help or provide some useful resource on how to fix this issue please let me know.
UPDATE
I have been looking for solution on how to fix this problem and I tried using cfscript with UNION ALL. This was suggested from few people and here is example of my code:
remote function saveRecords(required string formID, string status1="0", string status2="0", string status3="0", string status4="0", string status5="0", string comment1="", string comment2="", string comment3="", string comment4="", string comment5="") output=false returnFormat="JSON" {
local.fnResults = structNew();
local.runProcess = true;
local.arrReference = arrayNew(1);
try {
local.frmValidation = {
formID : len(arguments.formID),
status1 : ArrayContains([0,1], arguments.status1),
status2 : ArrayContains([0,1], arguments.status2),
status3 : ArrayContains([0,1], arguments.status3),
status4 : ArrayContains([0,1], arguments.status4),
status5 : ArrayContains([0,1], arguments.status5)
};
for ( i in frmValidation ) {
if ( !frmValidation[i] ) {
local.runProcess = false;
local.fnResults = {status: 400, message: "Form data is either missing or incorrect."};
break;
}
}
for ( fld in arguments ) {
if ( findNoCase(left(fld,6), "status") && arguments[fld] == 1 ) {
arrayAppend(arrReference, right(fld,1));
}
}
if ( runProcess ) {
local.qrySql = "INSERT INTO formDetails(recid, formid, refid, status, comment, userid, lastupdate)";
local.qryParams = [];
for ( idx=1 ; idx<=arraylen(arrReference) ; idx++ ) {
local.referenceID = arrReference[idx];
local.recPK = trim(arguments.formID) & trim(referenceID);
local.statusVal = trim(arguments["status" & local.referenceID]);
local.commentVal = trim(arguments["comment" & local.referenceID]);
if ( idx != 1 ) {
local.qrySql &= " UNION ALL ";
}
local.qrySql &= " SELECT ?,?,?,?,?,?,?";
qryParams.append({cfsqltype="cf_sql_numeric", value=local.recPK});
qryParams.append({cfsqltype="cf_sql_numeric", value=trim(arguments.formID)});
qryParams.append({cfsqltype="cf_sql_numeric", value=local.referenceID});
qryParams.append({cfsqltype="cf_sql_tinyint", value=local.statusVal});
qryParams.append({cfsqltype="cf_sql_varchar", value=local.commentVal, maxlength=8000, null=!len(local.commentVal)});
qryParams.append({cfsqltype="cf_sql_varchar", value=trim(session.userid), maxlength=6, null=!len(trim(session.userid))});
qryParams.append({cfsqltype="cf_sql_timestamp", value=now()});
}
local.qryResult = queryExecute(qrySQL, qryParams, {datasource="#application.datasource#", result="insertRecords"});
local.fnResults = {result: insertRecords, sql: qrySql, params: qryParams, array: arrReference, args: arguments, status: 200, message: "Form successfully saved!"};
}
} catch (any e) {
local.fnResults = {sql: qrySql, params: qryParams, error: e, status: 400, message: "Error! Something went wrong..."};
}
return fnResults;
}
After I tried to save the record from I got the message Something went wrong.... I looked in response and all I can see is error that is pointing to the this line:
local.qryResult = queryExecute(qrySQL, qryParams, {datasource="#application.datasource#", result="insertRecords"});
I checked the database and records are in there. No details about this error nothing. At least I'm not getting an error that I described in the question above but still no clue what is causing my code to crash.
I've found two things
1) you insert query does not have any value. INSERT INTO tablename( columns ... ) VALUES ( column values.. ) But you have missed VALUE key word in your query.
2) Whenever you are going to do two query operations in single cfquery tag you should add some Connection String in your testDB data sources. Which is available in CFML admin part. Open you data source and set below options. In CFML admin :
If you are using Lucee means: Please check the below options in your data source which is available in lucee server / web admin,

ColdFusion session variables not setting correctly

I have a few session variables I need setup for permissions and only the userID, userName and sessionAdmin variables are making it through. I do a cfdump and see all of the variables set to 0, which is the cfparam default. I also can not get the page to redirect to my sample address.
login.cfm
<cfif isDefined("FORM.login")>
<cfset Encryptpwd = Encrypt(FORM.password, application.PsswrdKy)>
<cfset loginInfo = CFCmain.getLogin(FORM.username,Encryptpwd)>
<cflock timeout=20 scope="session" type="exclusive">
<cfset session.UserName = loginInfo.username>
<cfset session.userid = loginInfo.id>
<cfset session.Access_admin = loginInfo.superadmin>
<cfset session.Access_var1 = loginInfo.var1>
<cfset session.Access_var2 = loginInfo.var2>
<cfset session.Access_var3 = loginInfo.var3>
<cfset session.Access_var4= loginInfo.var4>
<cfset session.Access_agency = loginInfo.agency>
</cflock>
<cflocation url="http://www.google.com">
</cfif>
and main.cfc where my variables are pulled from the db
<cffunction name= "getLogin" access="remote" returntype="any" >
<cfargument name="uname" type="string">
<cfargument name="pwd" type="string">
<cfquery name="getdata" datasource="#application.db#">
select * from users2 where username = '#arguments.uname#' and password = '#arguments.pwd#'
</cfquery>
<cfreturn getdata>
</cffunction>
For me it looks like the variable FORM.login is not defined!?
try:
<cfif isDefined("FORM.login")>
<cfdump var="hello">
<cfabort>
....
if you don't see the "hello", FORM.login is not defined...
try also:
<cfdump var="#form#" abort>
to see what is defined in the form scope.
And how others said, use cfqueryparam!! (security)
<cfquery name="getdata" datasource="#application.db#">
select *
from users2
where username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.uname#">
and password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.pwd#">
</cfquery>

Null pointer exception on <cfthread>

The following code works on Adobe ColdFusion 9.01+ but not Railo 4.1
Furthermore, this only fails on Post
This code is called inside of FW/1 setupRequest() in application.cfc
<cfthread name="threadA" action="run">
<cftry>
<cfquery>
INSERT
INTO dbo.Traffic (Circuit, Fuseaction, IP_hash)
VALUES (<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#listfirst(variables.rc.fuseaction, '.')#">,
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#listlast(variables.rc.fuseaction, '.')#">,
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#cgi.remote_addr#">
)
</cfquery>
<cfcatch />
</cftry>
</cfthread>
Error
java.lang.NullPointerException at
railo.runtime.net.http.HttpUtil.cloneParameters(HttpUtil.java:66):66
at
railo.runtime.net.http.HttpServletRequestDummy.clone(HttpServletRequestDummy.java:677):677
at
railo.runtime.thread.ThreadUtil.cloneHttpServletRequest(ThreadUtil.java:67):67
at
railo.runtime.thread.ThreadUtil.clonePageContext(ThreadUtil.java:29):29
at
railo.runtime.thread.ChildThreadImpl.(ChildThreadImpl.java:101):101
at railo.runtime.tag.ThreadTag.register(ThreadTag.java:269):269 at
scorecard38.application_cfc$cf.udfCall(D:\railo\webapps\www\Scorecard38\Application.cfc:162):162
at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:94):94
Wild Guess
variables.rc is not getting copied into the thread
variables.rc will not be copied into the thread unless you add it as an argument. Add rc as an argument and then instead of variables.rc you'll access it via arguments.rc
<cfthread name="threadA" action="run" rc="#variables.rc#">
<cftry>
<cfquery>
INSERT INTO dbo.Traffic (Circuit, Fuseaction, IP_hash)
VALUES (<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#listfirst(arguments.rc.fuseaction, '.')#">,
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#listlast(arguments.rc.fuseaction, '.')#">,
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#cgi.remote_addr#">
)
</cfquery>
<cfcatch />
</cftry>
</cfthread>
If that doesn't work you'll have to remove the try/catch and dump out your cfthread to see the error. The code below will output 'Variable C is undefined'
<cfthread name="threadA" action="run">
<cfset b = c>
</cfthread>
<cfdump var="#cfthread#"><cfabort>
I had the same problem s you. You are using CGI varaible and that's what is causing the problem

ColdFusion9 and scripted components and hql syntax

How can this be rewritten using the new scripting syntax along with the hibernate query language in CF9?
<cfcomponent output="no">
<cffunction name="Login" output="no">
<cfargument name="UsrName">
<cfargument name="UsrPassword">
<cfquery name="local.qry">
SELECT * FROM UsrView
WHERE UsrName = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.UsrName#">
AND UsrPassword = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.UsrPassword#">
</cfquery>
<cfreturn local.qry>
</cffunction>
</cfcomponent>
Is this what you are after?
component{
public query function Login(UsrName,UsrPassword){
// get array of entities matching filter
var arrayOfUsers = EntityLoad('User',{
UsrName=arguments.UsrName, UsrPassword=arguments.UsrPassword
});
// convert entity array to a query object
return EntitytoQuery(arrayOfUsers);
}
}