cfml variable inside cfoutput text - cfml

I'd like to resolve this problem. I have a variable:
<cfset myvar = "mytext <cfinclude template=""test.cfm"">">
and I have test.cfm with an amount of cfml code.
and I'm trying to make cfoutput like this:
<cfoutput>#myvar#</cfoutput>
I'd like that my page doesn't show this output:
mytext <cfinclude template="test.cfm">
but:
mytext followed by the execution of cfml code inside test.cfm
Is it possible?

I am not certain what you are trying to do from your question but if my guess is correct this may do what you want:
<cfsavecontent variable="myvar"><cfoutput>
mytext
<cfinclude template="test.cfm">
</cfoutput></cfsavecontent>
<cfoutput>
#myvar#
</cfoutput>
Note: You may not need the cfoutput inside the cfcontent tag, it depends on your page setup so I have just added it.

I have no idea what your included test.cfm file is doing since you did not share that information but this might work for you:
<cfset myvar = "mytext ">
<cfoutput>#myvar#</cfoutput>
<cfinclude template="test.cfm">
Assuming that the included file generates the output that you are looking for.

In your code, you are trying to set the cfinclude tag itself to another variable. This will never work. However, I have tested the following on a live server and it does work.
<cfsavecontent variable="inc">
<cfinclude template="test.cfm">
</cfsavecontent>
<cfoutput>mytext #inc#</cfoutput>
In this example, everything between the cfsavecontent tags is saved to a variable, in this case inc. Then you are able to reference that variable in the cfoutput tag.
Or, to use <cfset> as in your code:
<cfsavecontent variable="inc">
<cfinclude template="test.cfm">
</cfsavecontent>
<cfset myvar = "mytext" & inc>
& is the CFML string concatenation operator.
It's difficult to know for sure without seeing more of your code, but if you are trying to use includes in this way your code is perhaps disorganized. Providing a full example would help provide some context.

Related

How can I access the query for my cfoutput dynamically, based on a query name variable?

I have a struct that contains 2 queries. I have a variable with the "key" of one of the queries, and I want to output the query dynamically using that variable. My basic code:
<cfquery name="myQueries.names" ... >...</cfquery>
<cfquery name="myQueries.places" ... >...</cfquery>
<cfset queryName = "places" />
<cfoutput query="myQueries[queryName]">
...
</cfoutput>
This gives me the error Attribute validation error for tag cfoutput.
The cfoutput "query" attribute doesn't seem to support bracket notation. How can I access the query from the cfoutput?
The query attribute of cfoutput requires a valid variable name, so you can set an intermediary value and use that to reference your query
<cfset realQuery = myQueries[queryName]>
<cfoutput query="realQuery">
...
</cfoutput>

how to get a value out of a ColdFusion struct object

I am creating a pdf document (via ColdFusion), and rendering the pdf in a browser. The pdf form is already created and I am prefilling and populating the fields.
So what I am doing is dumping the variables out from the pdf to use as the name in a cfpdfformparam. (to get the variables) Then what I am doing is creating where owner email is the name of the variable from the pdf and then for the value I am assigning the session variable from another form. So that what they have entered prefills in the form they need to fill out. So really name decides the location on the pdf and value is the session variable of what was entered on the other form.
The issue I am having is that the variables that were already created, a few of them have structs inside of the variables so I am not sure how to call them in (name portion) in order to prefill and populate the information.
For example how would I prefill in the information for FEID/DL/DMVacct and for FL reg when they contain a struct within the variable?:
like this?: <cfpdfformparam
name="FEID/DL/DMVacct##"
value="#session.checkout.info.driverlicense_1#">
<cfpdfform action="read" source="82040y.pdf" result="data" />
<cfdump var="#data#" />
I am pre-populating the pdf form fields (via ColdFusion session variables), and then rendering the pdf using the following markup:
<cfpdfform source="82040.pdf" action="populate">
<cfpdfformparam name="org" value="">
</cfpdfform>
Any help would be greatly appreciated!
You can access variables embedded inside a struct like this:
<!--- using dot notation --->
<cfif StructKeyExists(myStruct, "myKey")>
<cfoutput> #mystruct.myKey#</cfoutput><br>
</cfif>
<!--- or using access notation --->
<cfif StructKeyExists(myStruct, LastName)>
<cfoutput>#LastName#: #mystruct[LastName]#</cfoutput><br>
</cfif>
You can use IsDefined to see if a value exists:
IsDefined("structure_name.key")>
However, if the key is dynamic, or contains special characters, you must use the StructKeyExists function.
NOTE: You must be careful about your variable names in ColdFusion. Some of the names assigned to your struct values would be considered invalid if used as variable names. So if your struct names contain invalid characters, you will need to access them via access-notation in order to retrieve them:
<!--- use access-notation for value names with special chars --->
data["FEID/DL/DMVacct"]["#"]
Otherwise, you will have runtime errors if you attempt dot-notation:
<!--- Invalid markup! Don't do this! --->
#data.FEID/DL/DMVacct.##
So here are some examples of how you could access your data:
<!--- Output the value --->
<cfoutput> #data["FEID/DL/DMVacct"]["#"]#</cfoutput><br>
<!--- Storing the value in a variable named 'myVar' --->
<cfset myVar = #data["FEID/DL/DMVacct"]["#"]# />
<cfoutput>#myVar#</cfoutput>
If your struct names follow ColdFusion's variable naming rules, then you can also access your data with dot-notation, notice I changed the value names FEID/DL/DMVacct and # to valid variable names: FEID_DL_DMVacct and num:
<!--- Output the value via dot-notation --->
<cfoutput> #data.FEID_DL_DMVacct.num#</cfoutput><br>
<!--- Storing the value in a variable named 'myVar' --->
<cfset myVar = #data.FEID_DL_DMVacct.num# />
<cfoutput>#myVar#</cfoutput>
Hope this helps!
structName['FEID/DL/DMVacct']['##'] = variable
You need the double # to escape them and produce one singular #

ColdFusion Query Output Displaying Variable Name Instead Of Field Value

I'm an "old dog" and largely self-taught on this stuff and can usually make things work (primative and convoluted as it might be), but this is the first thing that has me really stymied.
I didn't want to burden everyone with a lot of stuff to try to explain, but, here is perhaps a better explanation and example:
(#Leigh - and thank you for your time and help!) - The query is dynamic because what I desire to have is a single "universal" page combination (form page plus accompanying action page) that is used to edit multiple different (but fairly similar) record sets (so that I don't have to write a whole bunch of individual form/action page pairs).
When this "universal" "change" form page is invoked, it is passed the "ID" variable for the particular record to be edited, along with a "listID" variable unique to the particular record set containing the record to be edited.
Using the "URL.listID", the form page then looks at a pre-defined included list of record set variables (datasource, query table, field for column 1, field for column 2, etc.) pertaining to the value of the "listID" and sets (using ) the dynamic variables. Example - if "listID" is "5", which has only one column:
<cfif #URL.listID# EQ 5>
<cfset page_title = 'Change Member Role Picklist'>
<cfset datasource = '#Session.db_docs#'>
<cfset query_tbl = 'tblMemberRole'>
<cfset columns = 1>
<cfset column1_label = 'Member Role'>
<cfset column1_field = "role">
<cfset column1_input_type = "text">
<cfset column1_input_size = "100">
<cfset column1_input_maxlength = "100">
</cfif>
The query uses those variables ("ID" plus the others it got from the above list) to retrieve the individual record to be edited, and populate the "change" form.
Run query:
<cfquery name="cfqGetItem" datasource="#datasource#">
SELECT *
FROM #query_tbl#
WHERE ID = <cfqueryparam value="#URL.ID#" cfsqltype="cf_sql_integer">
</cfquery>
My "change" form (abbreviated here without table HTML) to be populated would be:
<form name="form_item_chg" action="chg_item2.cfm" method="post" enctype="multipart/form-data">
<input type="text" name="#column1_field#" maxlength="#column1_input_maxlength#" size="#column1_input_size#" value="#cfqGetItem[column1_field][currentRow]#">
<input type="Submit" value="Post Changes">
</form>
However, instead of the "change" form being populated with the VALUE for field "role", it instead is trying to use the variable name "column1_field", which it says is (true, of course) undefined in the query.
When I tried "#cfqGetItem[column1_field][currentRow]#", it says "Variable currentrow is not defined".
When I tried "#cfqGetItem.column1_field#", it says "Element column1_field is not defined in query cfqGetItem".
I apologize in advance for not knowing/using all the correct terminology, and hope I am explaining this reasonably clearly. I suspect I will have to revert to writing individual form-page/action-page pairs. Thank you to all for your time and help!
ORIGINAL POST:
I'm not highly technical, and this is probably something simple, but here is
my dilemma, where I am attempting to retrieve a single record using a variable name in the query.
First, I define some variables:
<cfset ID = #Form.ID#<!--- the single record I want to retrieve, passed from a form --->
<cfset datasource = 'MyDatabase'>
<cfset query_tbl = 'MyDatabaseTable'>
<cfset field1 = 'actual_fieldname1'><!--- field in MyDatabaseTable --->
<cfset field2 = 'actual_fieldname2'><!--- field in MyDatabaseTable --->
ETC.
Then, to retrieve this single record, I run a query using those variables:
<cfquery name="cfqGetItem" datasource="#datasource#">
SELECT *
FROM #query_tbl#
WHERE ID = #ID#
</cfquery>
Then, I attempt to display the query output:
EITHER AS
<cfoutput>
<p>#cfqGetItem.field1#
<p>#cfqGetItem.field2#
</cfoutput>
OR, AS
<cfoutput>
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfoutput>
In each case, I get a similar CF error message: "Element field1 is not defined in query cfqGetItem", or "Variable currentrow is not defined".
How can I get the query output to generate the actual values for the record instead of the variable names?
Thank you very much for any help!
If you change this:
<cfoutput>
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfoutput>
to this:
<cfoutput>
<cfloop query="cfqGetItem">
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfloop>
</cfoutput>
You should be ok. However, this is the simplest way
<cfoutput query="cfqGetItem">
<p>#actual_fieldname1#
<p>#actual_fieldname1#
</cfoutput>
Setting the table and field names to variables complicates matters. Unless they really can vary, don't do it. Also, the cfqueryparam tag is your friend. Use it in places like this:
where id = <cfqueryparam
cfsqltype = "cf_sql_integer"
value = "#id#">
You can try out like this also:
<cfquery
name="GetParks" datasource="cfdocexamples"
>
SELECT PARKNAME, REGION, STATE
FROM Parks
ORDER BY ParkName, State
</cfquery>
<cfoutput>
<p>#GetParks.PARKNAME#
<p>#GetParks.REGION#
</cfoutput>
<cfoutput >
#GetParks['state'][GetParks.RecordCount]#
</cfoutput>
<cfoutput >
#GetParks['state'][2]#
</cfoutput>

ColdFusion - Nested Variable (dynamic variable read) [duplicate]

This question already has answers here:
Coldfusion - variable field name when looping through database query results
(2 answers)
Closed 9 years ago.
Edit: The QUESTION is a duplicate, but the ANSWER is not!
I have the following code:
<cfquery name="contact" datasource="thesource">
SELECT * FROM #table# WHERE foo = '#bar#'
</cfquery>
and then later on (this is the problem part):
<cfloop from="0" to="9" index="i">
<cfset thisvar = Evaluate("contact.check" & i) />
<cfoutput>
#thisvar#
</cfoutput>
</cfloop>
Upon execution, it throws a nice big "Variable contact.check0 is Undefined". However, #contact.check0# will output just fine if hardcoded.
Any ideas on how to fix this?
Note:
I have seen Coldfusion - variable field name when looping through database query results, and although the problem seems to be exactly the same, the solutions do not work. As per the comments, I am also getting a "cannot be converted to a number" error. I notice Tomalak mentions "a little catch", but never says what it is. In the article he links to, I have tried every syntactically equivalent form, and it all throws an error...either cannot convert to a number or is not defined.
Also, I'm aware Evaluate() has overhead and "should not be used". I'll take any solution that works, whether it has Evaluate in it or not.
This is on ColdFusion 9.
Thank you
Edit: while a similar question already had an answer, this question had a different cause. See accepted answer below.
First of all, standard blub about escaping inputs. cfqueryparam exists for a reason. That aside you can do this without evaluate. Give this a try instead, including a check to make sure there is actually a result
<!--- make sure that there is actually a result --->
<cfif contact.RecordCount EQ 0>
NO RESULT
<cfelse>
<cfloop from="0" to="9" index="i">
<!--- assumes that you want row 1 --->
<!--- check.CurrentRow could also be used instead of 1 --->
<cfset thisvar = contact['check'&i][1] />
<cfoutput>
#thisvar#
</cfoutput>
</cfloop>
</cfif>
I suspect that it is attempting to Evaluate the column without specification of the row number, while when hard-coding it you'll find it uses the current row.
edit as Dan Bracuk pointed out this is an exact duplicate of the question you linked in your question, Coldfusion - variable field name when looping through database query results
This is ridiculous and absurd. Working code (moved things around a bit):
<cfoutput>
<cfloop from="0" to="9" index="i">
<cfset thisvar = contact["check"&i][1] />
#thisvar#
</cfloop>
</cfoutput>
NOT working code:
<cfoutput query="contact"> <!--- this line here --->
<cfloop from="0" to="9" index="i">
<cfset thisvar = contact["check"&i][1] />
#thisvar#
</cfloop>
</cfoutput>
Someone else had put cfoutput tags around the whole page. While this normally wouldn't be a problem, they ALSO declared the query attribute. This somehow overwrote the original query from the db. Calling contact.check0 worked just fine, but building the variable name dynamically, I think, was attempting to reference the cfoutput's query attribute.
Thanks to Peter Boughton for the suggestion to extract the problem code...which led me to this realization, and Simon for posting the syntactically correct code. Points go to them.

ColdFusion CFPDFFORM does not populate PDF form fields

I created a quick PDF file with two form fields, first name and last name (or FirstName and LastName), with a Submit button and used the following code to try to populate these fields in a new PDF file:
<cfpdfform action="populate" source="#variables.AbsPath#\test.pdf">
<cfpdfformparam name="FirstName" value="foo" />
<cfpdfformparam name="LastName" value="bar" />
</cfpdfform>
However, I receive this error:
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.
Platform, Locale, and Platform Name must not be null
I tried reading the PDF file to make sure the form fields were correct using this and they are:
<cfpdfform action="read" source="#variables.AbsPath#\test.pdf" result="fields" />
<cfdump var="#fields#">
The struct dumps out:
FirstName: [empty string]
LastName: [empty string]
Submit: [empty string]
I tried searching the Adobe docs and around some other sites, but can't find a solution for this error. I also tried adding a "destination" parameter to the cfpdfform tag in case it was needed, but got the same result. The PDF file does exist. Any ideas? Thanks in advance.
UPDATED:
Here is the stack trace:
java.lang.NullPointerException: Platform, Locale, and Platform Name must not be null
at com.adobe.fontengine.fontmanagement.platform.PlatformFontDescription.<init>(Unknown Source)
at com.adobe.fontengine.font.opentype.OpenTypeFont.getPlatformFontDescription(Unknown Source)
at com.adobe.fontengine.font.FontImpl.getPlatformFontDescription(Unknown Source)
at com.adobe.fontengine.font.FontImpl.getPlatformFontDescription(Unknown Source)
at com.adobe.fontengine.fontmanagement.platform.PlatformFontResolverImpl.addFont(Unknown Source)
at com.adobe.internal.pdftoolkit.core.fontset.impl.PDFFontSetImpl.addFont(Unknown Source)
at com.adobe.internal.pdfm.util.FontSetBuilder.loadFontsPath(FontSetBuilder.java:418)
at com.adobe.internal.pdfm.util.FontSetBuilder.loadSystemFonts(FontSetBuilder.java:346)
at com.adobe.internal.pdfm.util.FontSetBuilder.makePDFFontSet(FontSetBuilder.java:239)
at com.adobe.internal.pdfm.util.FontSetBuilder.getPdfFontSet(FontSetBuilder.java:93)
at com.adobe.internal.pdfm.PDFM7Factory.getPdfFontSet(PDFM7Factory.java:97)
at coldfusion.document.DocumentServiceImpl.getAssemblerFontSet(DocumentServiceImpl.java:512)
at coldfusion.pdf.PDFForm.populateFields(PDFForm.java:292)
at coldfusion.tagext.lang.PDFFormTag.doEndTag(PDFFormTag.java:347)
at cfsubmit2ecfm1224790315.runPage(C:\ColdFusion\wwwroot\nVision\projects\test\pdfform\submit.cfm:9)
at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:231)
at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:416)
at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65)
at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:381)
at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:48)
at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
at coldfusion.filter.PathFilter.invoke(PathFilter.java:94)
at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:27)
at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:70)
at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:46)
at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62)
at coldfusion.filter.RequestThrottleFilter.invoke(RequestThrottleFilter.java:126)
at coldfusion.CfmServlet.service(CfmServlet.java:200)
at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:86)
at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:94)
at jrun.servlet.FilterChain.service(FilterChain.java:101)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:106)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:286)
at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:543)
at jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:203)
at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:428)
at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)
I believe this is a font issue with the specific server being used. This is why the same code will work on the production server and not the testing server. I am experiencing the same issue and although I don't have a great solution here's what I learned.
The stacktrace shows that there's an error getting the getPlatformFontDescription of an OpenType font.
Our Windows server is listing some TrueType fonts as OpenType. Is yours?
Using the following code corrected the issue, but it's a work around only.
<cfpdf name="local.pdffile" action="read" source="some path" >
<cfscript>
local.pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader").init(tobinary(local.pdffile));
local.outputStream = createObject("java", "java.io.ByteArrayOutputStream").init();
local.pdfStamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init(local.pdfReader,local.outputStream);
local.Acroform = local.pdfStamper.getAcroFields();
//Populating Form Fields
local.Acroform.setField("Field1",Arguments.Value1);
local.Acroform.setField("Field2",Arguments.Value2);
// etc.
local.pdfStamper.setFormFlattening(true); //optional
local.pdfStamper.close();
local.pdfReader.close();
local.pdffile = local.outputStream.toByteArray();
</cfscript>
<!--- flatten="no" must be set or you will get the error again --->
<cfpdf action="write" source="local.pdffile" destination="#variables.OutputPath##local.UUID#.pdf" overwrite="yes" flatten="no" />
More Diagnostics: Simply listing all the fonts available to the server using the Java subsystem also fails. Try this.
<cfset list=createobject("java","com.adobe.internal.pdfm.util.FontSetBuilder")>
<cfdump var="#list#">
<cfset dummy = list.getPdfFontSet()>
<cfdump var="#dummy.toString()#">
<!--- this should fail --->
We have "fixed" our problem at the moment by removing all the fonts from the Windows\Fonts folder, and isolating the damaged ones. It's painstaking but seems to work.
Good luck!
You did not mention how you created your PDF file. Depending on how you created the PDF file the form fields may be contained within a subform. PDFs generated from templates within LiveCycle will do this. By default LiveCycle will name the subform "form1". In order to populate these form fields you need to include the cfpdfsubform tag. See the documentation here: cfpdfsubform documentation
So your code would look something like this:
<cfpdfform action="populate" source="#variables.AbsPath#\test.pdf">
<cfpdfsubform name="form1">
<cfpdfformparam name="FirstName" value="foo" />
<cfpdfformparam name="LastName" value="bar" />
</cfpdfsubform>
</cfpdfform>
The code that you included to read your PDF file should show you if the fields are included in a subform. The dump would show your form fields contained within another structure. That would be the name that you use in the cfpdfsubform tag's name attribute.
HTH.
I just wrote up a script to fix this issue here: https://stackoverflow.com/a/20408559/1223555
It's an issue with a font file not having a locale or platform name set. the PDF functions in Coldfusion call this
<cfset list=createobject("java","com.adobe.internal.pdfm.util.FontSetBuilder")>
<cfset dummy = list.getPdfFontSet()>
This enumerates every font and if one is bad it throws an exception and kills your process.
My code enumerates all the fonts in c:\windows\fonts and then tells you the bad ones.
Here are the ones we have issues with:
AdobeNaskh-Medium.otf
Mechanical.TTF
msgothic.ttf
MyriadArabic-Bold.otf
MyriadArabic-BoldIt.otf
MyriadArabic-It.otf
MyriadArabic-Regular.otf
I just realized after re-reading your question that I was going down the wrong path. I was thinking you were having an issue populating the fields in the pdf. But your real issue is the null pointer. Doh! Sorry about the confusion.
Usually that error is telling you that a variable you are using is not defined. The only variable I see in your example is #variables.AbsPath#. You are specifying the variables scope. Is that correct? The variables scope is available only on the page in which it was created (and to included pages).
You did not include how you are defining that variable. Can you share that?
Is the error happening randomly or all the time?
Are you sure the error is happening on that cfpdfform line and not somewhere else? I would put a cftry/cfcatch block around your code and dump the cfcatch structure to get a more specific error. Can you try that and post back?