Simran Litt
Simran Litt

Reputation: 25

How to set an index of an array within a struct as a variable(Coldfusion)

This is the first error that I am getting: Invalid data [d6-2014-74, , ] for CFSQLTYPE CF_SQL_VARCHAR

Here is the corresponding code.

 <cfset x = Arguments.surveyDocNo>
 <cfprocparam cfsqltype="cf_sql_varchar" value="#x#">

First Error Image:

To solve that problem, I changed my code to this:

 <cfset x = Arguments.surveyDocNo[1]>

However, that returns this error:

500 You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

Error 2

So the array issue is fixed, but I don't know how to fix the second error.

 <cfdump var="#arguments.surveyDocNo#">

enter image description here

Upvotes: 0

Views: 181

Answers (1)

Alex
Alex

Reputation: 7833

I assume you pass the surveyDocNo argument sometimes as string and sometimes as an array of strings.

<!--- if argument is passed as array, take the first element --->
<cfif isArray(Arguments.surveyDocNo) and (not arrayIsEmpty(Arguments.surveyDocNo))>
    <cfset x = Arguments.surveyDocNo[1]>
<cfelse>
    <cfset x = Arguments.surveyDocNo>
</cfif>

<!--- if the provided argument is not a string, throw an exception --->
<cfif not isSimpleValue(x)>
    <cfthrow type="IllegalArgumentException" message="The provided value for argument [surveyDocNo] is invalid. Expected a string.">
</cfif>

<cfprocparam cfsqltype="cf_sql_varchar" value="#x#">

Upvotes: 4

Related Questions