We help IT Professionals succeed at work.

CF Function help

CFbubu
CFbubu used Ask the Experts™
on
Hi,

I am trying to get a function to work but somehow I keep getting an error telling me that 'a variable is undefined'. I would appreciate it if someone could point out what I am doing wrong.

I have placed this function below inside a cfc file:

<cffunction name="textMessage" access="public" returntype="string" hint="Converts an html email message into a nicely formatted with line breaks plain text message">
    <cfargument name="string" required="true" type="string">
    <cfscript>
        var pattern = "<br>";
        var CRLF = chr(13) & chr(10);
        var message = ReplaceNoCase(arguments.string, pattern, CRLF , "ALL");
        pattern = "<[^>]*>";
    </cfscript>
    <cfreturn REReplaceNoCase(message, pattern, "" , "ALL")>
</cffunction>  




I than write the code below inside 'somepage.cfm' to call the function above in the cfc file & use it.


<cfsavecontent variable="mailmessage">
<cfoutput>
<html>
<head>
</head>

<body>

<h4>Hello!</h4>

<p>This is an interesting test</p>

</body>
</html>
</cfoutput>
</cfsavecontent>

<cfinvoke component="#APPLICATION.sitepath#" method="textMessage"   >
   
   <cfinvokeargument name="string" value="#mailmessage#">
   
 </cfinvoke>


<cfmail    
 subject="This is a test title"
 from="someone@js.com"
 to="something@js.com"
 type = "HTML"
 >
<cfmailpart type="text/plain" charset="utf-8">#textMessage(mailmessage)#</cfmailpart>
<cfmailpart type="text/html" charset="utf-8">#mailmessage#</cfmailpart>

</cfmail>  




When I run somepage.cfm, I get the error: Variable TEXTMESSAGE is undefined

Please let me know what I am doing wrong...thanks so very much!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015
Commented:
You need to capture the result of cfinvoke in a variable:

    <cfinvoke component="path.to.YourComponentName"
                        method="textMessage"  
                        returnVariable="textVersionOfMessage">
        <cfinvokeargument name="string" value="#mailmessage#">
   </cfinvoke>

Then use the result variable like this:
    <cfmailpart type="text/plain" charset="utf-8">#textVersionOfMessage#</cfmailpart>

... instead of this
    <cfmailpart type="text/plain" charset="utf-8">#textMessage(mailmessage)#</cfmailpart>
Most Valuable Expert 2015

Commented:
I have placed this function below inside a cfc file:
<cfmailpart type="text/plain" charset="utf-8">#textMessage(milmessage)#</cfmailpart>


By the way, the reason that doesn't work is because the function exists in a separate cfc. So you can't call it directly. You must access it through the component, either using cfinvoke or createObject.

Author

Commented:
Hi _agx_,

Thanks for your response :)

I did write the function in a seperate cfc file, but I did try to access the 'variable' by using the cfinvoke before I used the variable inside the cfmail tag......

I used the invoke below:

<cfinvoke component="#APPLICATION.sitepath#" method="textMessage"   >
   
   <cfinvokeargument name="string" value="#mailmessage#">
   
 </cfinvoke>

Was that the wrong way?

Thanks for your clarification!
Most Valuable Expert 2015

Commented:
EDIT

but I did try to access the 'variable' by using the cfinvoke before I used the variable inside the cfmail tag...... I used the invoke below:


Using cfinvoke is like calling any CF function that returns a value. You have to use the "returnVariable" attribute to capture the result, or it's lost and it'll seem like nothing happened.
         
The other problem was you can't do this:

          #textMessage(milmessage)#

There's no function or variable in your CFM page named textmessage. That's why CF threw an error.  When you call the function with CFINVOKE you must capture the result in a variable and use the new variable in your mail tag instead:

<cfinvoke component="path.to.YourComponentName"
                        method="textMessage"  
                        returnVariable="theNewVariable">
        <cfinvokeargument name="string" value="#mailmessage#">
   </cfinvoke>

    ....
    <cfmailpart type="text/plain" charset="utf-8">#theNewVariable#</cfmailpart>

Author

Commented:
Hi _agx_,

AGAIN you are brilliant!

Yes....I added changed the cfinvoke to add the returnvariable:

<cfinvoke component="#APPLICATION.sitepath#" method="textMessage" returnvariable="message"   >   
                <cfinvokeargument name="string" value="#mailmessage#">
                </cfinvoke>

And I changed the code below to:

<cfmailpart type="text/plain" charset="utf-8">#message#</cfmailpart>

instead of:

<cfmailpart type="text/plain" charset="utf-8">#textMessage(mailmessage)#</cfmailpart>


Thank you for always being so quick with your insights!

Regards.

Author

Commented:
Again... much thanks!
Most Valuable Expert 2015

Commented:
Welcome :)