Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conditionally pass arguments to an instance of a CFC?

I am currently using the <cfinvoke> tag to invoke CFCs and pass them arguments. This is really convenient because I can use tags to pass in only the parameters that I want like such:

<cfinvoke component="pathtofolder.imagehandler" method="SomeMethod" argumentcollection="#VARIABLES#" returnvariable="ImageHandlerResult">
<cfif structkeyexists(ARGUMENTS, 'Argument1')>
<cfinvokeargument name="Parameter1" value="#ARGUMENTS.Argument1#" />
</cfif>
<cfif structkeyexists(ARGUMENTS, 'Argument2')>
<cfinvokeargument name="Parameter2" value="#ARGUMENTS.Argument2#" />
</cfif>
<cfif structkeyexists(ARGUMENTS, 'Argument3')>
<cfinvokeargument name="Parameter3" value="#ARGUMENTS.Argument3#" />
</cfif>
</cfinvoke>
<cfreturn ImageHandlerResult /> <!--- how do you get this using createObject/new method? --->

If I use the new() or createObject() methods to create an instance of the CFC and then call the methods within this newly created instance I'm not able to conditionally pass arguments. I get errors at runtime.

<cfset ImageHandler = new pathtofolder.imagehandler()/>
<cfset ImageHandler.SomeMethod(
    <cfif StructKeyExists(ARGUMENTS, 'Argument1')>
    Parameter1 = ARGUMENTS.Argument1
    </cfif>
    <cfif StructKeyExists(ARGUMENTS, 'Argument2')>
    Parameter2 = ARGUMENTS.Argument2
    </cfif>
<cfif StructKeyExists(ARGUMENTS, 'Argument3')>
    Parameter3 = ARGUMENTS.Argument3
    </cfif>
)/>

How can I pass in arguments conditionally using the above method? Should I use the cfinvoke method on the new instance - in which case what is the point in making an instance and then using cfinvoke again when I could just stick to using cfinvoke on the actual CFC directly?

like image 455
volume one Avatar asked Oct 30 '18 16:10

volume one


People also ask

Does Oracle Application Support conditionally mandatory parameters?

Oracle Application does not support conditionally mandatory parameters. But sometime business requires conditionally mandatory parameters. So we need to achieve the same functionality using some workaround so that the program will be submitted with proper parameters.

Can you pass objects as arguments to a function?

Passing instances of classes (objects) as arguments to a function is not something new in any language. This is most certainly *not the problem. Could you paste the entire traceback please? The error message implies that you are subscripting a non-subscriptable object, so your hypothesis about the error is mistaken.

How to add conditional mandatory parameter in SSMP with default values?

And add parameters to it with default values Create a concurrent program “SSMP Conditional Mandatory Parameter” with seven parameters as below Create the parameter for Print of New and enter the Value Set as : Yes_No with default value: No


1 Answers

There is a similar way which can be used to pass conditional attributes to ColdFusion tags. Following is an example for a <cfmail> tag.

<cfset local.cfmailArguments = {
  to : '[email protected]',
  from : '[email protected]',
  subject : 'Passing custom smtp',
  type : 'html',
}>
<!--- There are custom mail settings available in session.SMTPDetails --->
<cfif structkeyexists(session, "SMTPDetails")>
  <cfset local.cfmailArguments['from'] = session.SMTPDetails.FromEmail>
  <cfset local.cfmailArguments['server'] = session.SMTPDetails.Server>
  <cfset local.cfmailArguments['username'] = session.SMTPDetails.UserName>
  <cfset local.cfmailArguments['password'] = session.SMTPDetails.Password>
  <cfset local.cfmailArguments['port'] = session.SMTPDetails.Port>
  <cfset local.cfmailArguments['usetls'] = session.SMTPDetails.TLS>
  <cfset local.cfmailArguments['usessl'] = session.SMTPDetails.SSL>
</cfif>
<cfmail attributecollection="#local.cfmailArguments#">
  Your mail content.
</cfmail>

Instead of having to manage different tags in each conditions.

like image 55
rrk Avatar answered Nov 15 '22 13:11

rrk