call same component twice in a page while passing different parameter

armanbena
armanbena used Ask the Experts™
on
Hi All,

I am storing user ids in a table.  In order to retrieve a name, I look that up based on the ID.  
I have 2 ids.  One that is saved, one for the user who is currently filling the form.  I tried invoking the components and changing one of the returnvariable to something say something else, to distinguish the 2, but am getting the same value in both fields.  When I load 1 and comment out the other, info is fine.  What can I do, to distiinguish both of these and get my data on my form?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
do a CFSET  for the one you wish to use again
Store it
 and call that varaible when needed
 
sounds to me like you have a variable with the same name but for 2 different purposes? can't really tell without some sample code.

We'll need you to post code or elaborate a bit more.
<cfset foo = createObject("component","com.foo")>
<cfset var1 = foo.getID(variable.something)>
<cfset var2 = foo.getID(variable.somethingelse)>

It would help to post some code.

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

if you can post some sample code! that will help us in debugging the issue properly

Author

Commented:
<cfinvoke component="function" method="getInfo" returnvariable="UserInfo">
    <cfinvokeargument name="userid" value="#qResult.submitted_by#">
</cfinvoke>
<cfinvoke component="function" method="getInfo" returnvariable="testInfo">
    <cfinvokeargument name="userid" value="#qResult.reviewer#">
</cfinvoke>

Passing 2 different values to same function.  submitted_by is user1 who submits the first part of the form.  This part of the form where I call these functions is being filled by someone else.  I'm sure there's a way to distinguish the 2.
hmm.. maybe I'm not understanding

what you have there should run getInfo twice, use qResult.submitted_by as the userid argument, set the first result to UserInfo, the second will use qResult.reviewer as the argument and set the result to testinfo

so what is the issue?

Author

Commented:
the issue is, when I have both on a page, I end up getting the submitted_by value for both fields.  It's like something hasn't cleared, or like calling the function once, for submitted_by, supplies both values with submitted_by name.  Not sure what's causing it or where to look.
ok -  the invoke call should be fine but things you need to look for

to test you have the right values in qResult add the following right before the cfinvoke

<cfdump var="#qResult#">
<cfabort>

The dumpo will show you if you have the expected variable values in qresult


also make sure in your display page that you are referencing the different returnvariables


<cfdump var="#UserInfo#">
<cfdump var="#testInfo#">
<cfabort>


could do with a bit more code as this only is a guess
The example I had provided is shorthand for your cfinvoke statements. If you are using GoDaddy or someone like that for your hosting you will not be able to use CreateObject or cfdump.

Are you sure that the output variable is not UserInfo for both instances?
Most Valuable Expert 2015

Commented:
(no points ...)

>> when I have both on a page, I end up getting the submitted_by value for both fields

When I hear things like that it makes me think of missing VAR statements or possible variable scope problems. I'm sure it would help the other experts if they could see what your function is actually doing.  Please post the code for the "getInfo" function.

Also, make sure you've verified the obvious. ie that the two values really _are_ different

<cfoutput>
submitted_by = #qResult.submitted_by#
reviewer = #qResult.reviewer#
</cfoutput>

Good luck

Author

Commented:
Okay, I've been looking at this, and I changed the setup a bit and I think I have a naming collision.  Not sure what to change to get this to work the way it should.  
I am now calling the component for the submitted_by field to display the name.  
///////////////////////////////////////////////////////

On the cfc page, I have this code:
<cfoutput query="UserId">
  <cfset #employeeNumber# = #iif(employeeNumber IS "", de(UserId.employeeNumber), de(employeeNumber))#>
  <cfset #FirstName# = trim(#UserId.givenName#)>
  <cfset #LastName#  = trim(#UserId.sn#)>
  <cfset #Email#     = trim(#UserId.mail#)>
  <cfset #supervisorID#     = trim(#UserId.supervisorID#)>
  <cfset #MailCode#     = trim(#UserId.mailCode#)>
  <cfset #CostCenter#     = trim(#UserId.costCenterCode#)>
  <cfset #Phone#     = trim(#UserId.telephoneNumber#)>
  <cfset #Position#     = trim(#UserId.title#)>
  <cfset #Badge#     = trim(#UserId.employeeNumber#)>
</cfoutput>


<cfset employeeInfo = StructNew()>
<cfset employeeInfo.firstName = FirstName>
<cfset employeeInfo.lastName = LastName>
<cfset employeeInfo.employeeNumber = employeeNumber>
<cfset employeeInfo.email = email>

<cfreturn employeeInfo>

<cfset UserInfo = StructNew()>
<cfset UserInfo.firstName = FirstName>
<cfset UserInfo.lastName = LastName>
<cfset UserInfo.employeeNumber = employeeNumber>
    <cfreturn UserInfo>
      
      
      <cfset testInfo = StructNew()>
<cfset testInfo.firstName = FirstName>
<cfset testInfo.lastName = LastName>
<cfset testInfo.employeeNumber = employeeNumber>
    <cfreturn testInfo>
      
      </cffunction>

</cfcomponent>
//////////////////////////////////////////////////

Now for the second field, I am using a script that gets the current user's info (given to me)
<cfoutput query="getPersonByUserId">
  <cfset #employeeNumber# = #iif(employeeNumber IS "", de(getPersonByUserId.employeeNumber), de(employeeNumber))#>
  <cfset #FirstName# = trim(#getPersonByUserId.givenName#)>
  <cfset #LastName#  = trim(#getPersonByUserId.sn#)>
  <cfset #Email#     = trim(#getPersonByUserId.mail#)>
  <cfset #supervisorID#     = trim(#getPersonByUserId.supervisorID#)>
  <cfset #MailCode#     = trim(#getPersonByUserId.mailCode#)>
  <cfset #CostCenter#     = trim(#getPersonByUserId.costCenterCode#)>
  <cfset #Phone#     = trim(#getPersonByUserId.telephoneNumber#)>
  <cfset #Position#     = trim(#getPersonByUserId.title#)>
  <cfset #Badge#     = trim(#getPersonByUserId.employeeNumber#)>
</cfoutput>

They both use employeeNumber.  I'm thinking that's what could be the issue.
But I'm not sure.  To get the script I do an include in my form, and then have a cfoutput for the query.
Any insights?
For starters when you "SET" a variable you do not evaluate it. In other words no pound signs...
these are ways to set a variable...

this example there is nothing to evaluate, setting a variable from a variable
<cfset FirstName = trim(getPersonByUserId.givenName)>

this example you are setting a variable to a string. Part of the string is a variable so it needs to be evaluated
<cfset FirstName = "This is #getPersonByUserId.givenName#">

this is a more complex example where I would be creating the actual variables dynamically and setting them from another variable...
<cfset "#dynamicVar[i]#" = dynamicVar[j]>

OK. Your CFC is really odd. It looks like you have a single function trying to return a value 3 times. And then your 3 different structs also have all of the same data...

You are not showing us your complete CFC because this would never compile. CF would throw an error if you tried to instantiate this CFC.


<!--- This would be your CFC --->
<cfcomponent hint="you would name this users.cfc and place it in a root folder called com">
	<cffunction name="getUserInfo" access="public" returntype="query">
  	<cfargument name="userId" type="numeric" required="yes">
    	<cfquery name="getUserInfo" datasource="#request.dsn#">
      	SELECT * FROM users WHERE userId = #val(arguments.userid)#
      </cfquery>
  	<cfreturn getUserInfo />
  </cffunction>
</cfcomponent>

<!--- On your page you would invoke the CFC and then call it twice. --->
<!--- this is the same as cfinvoke, just shorter to write --->
<cfset userObj = createObject("component","com.users")>
<!--- userObj is now the component --->

<!--- set what comes back from the function as a variable, in this example we get back a query object --->
<cfset submittedByQry = userObj.getUserInfo(val(theSubmittedByUserId))>

<!--- make a second call to the function providing it the other userid and you will get back a different result set --->
<cfset reviewerQry = userObj.getUserInfo(val(theReviewersUserId))>

What you would get back are two queries that each hold all of the user information for the two userids.
To then use these variables you would get the name simple by outputting #submittedByQry.FirstName# or #reviewerQry.FirstName#

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial