views:

226

answers:

2

I'm invoking a cfc, the cfc has a default set of arguments like so:

<cfargument name="EMAIL_TEMPLATE_CODE" type="string" required="yes" hint="EMAIL_TEMPLATE_CODE is required.">
<cfargument name="EMAIL_TEMPLATE_SUBJECT" default="" type="string" required="no" hint="EMAIL_TEMPLATE_SUBJECT is NOT required.">
<cfargument name="EMAIL_TEMPLATE_BODY" default="" type="string" required="no" hint="EMAIL_TEMPLATE_BODY is NOT required.">

What I'd like to do is make these arguments not requred (as you can see by required="no") but I'd like to reassign the arguments variable if needed.

So something like:

<cfargument name="EMAIL_TEMPLATE_CODE" type="string" required="yes" hint="EMAIL_TEMPLATE_CODE is required.">
<cfargument name="EMAIL_TEMPLATE_SUBJECT" default="" type="string" required="no" hint="EMAIL_TEMPLATE_SUBJECT is NOT required.">
<cfargument name="EMAIL_TEMPLATE_BODY" default="" type="string" required="no" hint="EMAIL_TEMPLATE_BODY is NOT required.">

<cfinvoke component="#Request.CFCPath#.email_template" method="getEmailTemplate" returnvariable="getEmailTemplate">
    <cfinvokeargument name="EMAIL_TEMPLATE_CODE" value="#ARGUMENTS.EMAIL_TEMPLATE_CODE#">
</cfinvoke>

<cfif getEmailTemplate.RecordCount>
    <cfparam name="ARGUMENTS.EMAIL_TEMPLATE_SUBJECT" default="#getEmailTemplate.EMAIL_TEMPLATE_SUBJECT#" type="string">
    <cfparam name="ARGUMENTS.EMAIL_TEMPLATE_BODY" default="#getEmailTemplate.EMAIL_TEMPLATE_BODY#" type="string">
</cfif>

But I'm not able to override the default ARGUMENTS variable. Is there anything you can spot that I'm doing wrong?

EDIT:

I'm doing this because if no argument is passed to the cfc, I want to create one. I guess I should cfset a local variable if the argument doesn't have length?

<cfif Len(ARGUMENTS.EMAIL_TEMPLATE_ADDRESS_FROM)>
    <cfset EMailTemplateAddressFrom = ARGUMENTS.EMAIL_TEMPLATE_ADDRESS_FROM>
<cfelse>
    <cfset EMailTemplateAddressFrom = getEmailTemplate.EMAIL_TEMPLATE_ADDRESS_FROM>   
</cfif>
+5  A: 

<cfparam> works only if the variable has been undefined before. Your function parameters are not undefined, they just happen to have their default values. So you could do this:

<cffunction name="foo">
  <cfargument name="arg" type="string" required="yes">
  <cfargument name="opt" default="default" type="string" required="no">

  <cfif arguments.opt eq "default">
    <cfset arguments.opt = "whatever dynamic value">
  </cfif>
</cffunction>

This way, if you don't supply "opt" (or deliberately set it to "default"), it will get assigned some kind of dynamic default value. And you can still make it empty, if you need.

Instead of "default", you could choose some other kind of improbable value that allows you to make a distinction between "not supplied" and "empty". (Sometimes I wish ColdFusion would support actual null values...)

Tomalak
+2  A: 
<cfargument name="EMAIL_TEMPLATE_CODE" type="string" 
    default="#getEmailTemplate.EMAIL_TEMPLATE_ADDRESS_FROM#">
<cfargument name="EMAIL_TEMPLATE_SUBJECT" default="" type="string">
<cfargument name="EMAIL_TEMPLATE_BODY" default="" type="string">

or....

<cfargument name="EMAIL_TEMPLATE_CODE" type="string">
<cfargument name="EMAIL_TEMPLATE_SUBJECT" default="" type="string">
<cfargument name="EMAIL_TEMPLATE_BODY" default="" type="string">
<cfif NOT isDefined("arguments.EMAIL_TEMPLATE_CODE")>
    <cfset arguments.EMAIL_TEMPLATE_CODE = getEmailTemplate.EMAIL_TEMPLATE_ADDRESS_FROM>
</cfif>

FYI, required="no" by default, so I usually don't specify it to the already verbose CFML. :)

Henry
I tried the first option you provided earlier, before I asked the question. It seems that, for whatever reason, the function would error out if anything was above the arguments. In order to set the default value as #getEmailTemplate.EMAIL_TEMPLATE_ADDRESS_FROM# I needed to run the query above the arguments. Strange behavior and I've never ran into that problem, until today.
jyoseph
well, that depends if it can reference the variable getEmailTemplate, whatever it is. If it is a variable in This or Variables scope, it will be fine.
Henry