Setting a variable in Cold Fusion

Hi,
I am trying to set a variable (guiOfficeID) to a new value if the current value of guiOfficeId is "B9D0F3F5-13BA-4E0C-AF55-037611A60577"
I have the cffile in there so I can see what is going on. However, no matter how I try to do it, the guiOfficeID variable will not change. I have confirmed that I definitely do get into the "IF" and if I try to set a new variable name (newGuiOfficeID) to a new value it works fine. But I can't change guiOfficeID to a new value? Am I missing something very obvious here?
Thanks!
Nacht

<cfif #guiOfficeID# eq "B9D0F3F5-13BA-4E0C-AF55-037611A60577">
      <cfset #guiOfficeID# = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">
      <cffile action="append" file="C:\home\mls\logs\OfficeDropdownList.txt" output="AFTER SET OFFICE GUID: #guiOfficeID#">
      </cfif>
LVL 1
nachtmskAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_agx_Commented:
What makes you think it is not changing? Your code worked fine for me w/CF10.  The problem might be somewhere else in the code.  

Code
<cfset guiOfficeID = "B9D0F3F5-13BA-4E0C-AF55-037611A60577">

<br><cfdump var="BEFORE = #guiOfficeID#">

<cfif #guiOfficeID# eq "B9D0F3F5-13BA-4E0C-AF55-037611A60577">
      <cfset #guiOfficeID# = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">
</cfif> 

<br><cfdump var="AFTER = #guiOfficeID#">

Open in new window


Though to avoid any possibility of unintended conversion issues, get rid of the extraneous # signs and use compareNoCase (to do a string comparison) rather than EQ:

Code:
<cfset guiOfficeID = "B9D0F3F5-13BA-4E0C-AF55-037611A60577">

<br><cfdump var="BEFORE = #guiOfficeID#">

<cfif compareNoCase(guiOfficeID, "B9D0F3F5-13BA-4E0C-AF55-037611A60577") eq 0>
      <cfset guiOfficeID = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">
</cfif> 

<br><cfdump var="AFTER = #guiOfficeID#">

Open in new window


Results:

BEFORE = B9D0F3F5-13BA-4E0C-AF55-037611A60577
AFTER = 152A656F-C1AF-448C-BAB2-4D7B30CEEE65
0
nachtmskAuthor Commented:
What makes me think it's not changing, is the cffile I am writing to. It has the old value each time I try to reset the variable. Maybe it is someplace else in the code, I'll have a look.
Thanks for confirming the cfset was done correctly.
0
gdemariaCommented:
As AGX suggested, you need to remove the #s from your set.  You are setting the new value to the VALUE of the variable, not to the variable itself.

 <cfset #guiOfficeID# = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">

should be

 <cfset guiOfficeID = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">

better yet, scope your variables...

 <cfset variables.guiOfficeID = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

_agx_Commented:
Edit:
> Thanks for confirming the cfset was done correctly.
@nachtmsk - I didn't say it was correct exactly - just that it worked under CF10 ;-) The "correct" way to write it is to get rid of the # extra signs and use a string comparison.  As GD suggested, the # signs might be causing an issue.  Any change if you do that?

GD, yes it may be something along those lines, but surprisingly I couldn't reproduce it under CF10. Using the original code, the "AFTER" result changes as you'd expect. So I figured removing the pound signs and doing a string comparison would probably resolve it. If not, the issue is something else.
0
nachtmskAuthor Commented:
Trying now.. FYI -- using CF 11
0
nachtmskAuthor Commented:
Your code works on my server,
but the code below doesn't. It's still not setting the var.
Here is some more code. The whole thing is wrapped in a loop. I don't see how that changes anything though.

The file I am writing to (officeDropDownList) has the old value B9D0F3F5-13BA-4E0C-AF55-037611A60577 each time I run this code. The page this code creates, also has the old value. Driving me a bit nuts!


<cfset qryOffices = office.getAll(1) />
            <select size="1" id="office">                    
                  <option value="">- Select an Office -</option>
                  <cfoutput query="qryOffices">
                  <cfif #guiOfficeID# eq "B9D0F3F5-13BA-4E0C-AF55-037611A60577">
                  <cfset guiOfficeID = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">
            
<cffile action="append" file="C:\home\logs\OfficeDropdownList.txt" output="AFTER OFFICE GUID: #guiOfficeID#">
                  </cfif>
           
                  <option value="#guiOfficeID#">#OfficeName#</option>
                  </cfoutput>
                  </select>
0
nachtmskAuthor Commented:
This wound up working for me, but I'd still rather do it the other way by resetting the var. So strange...

<cfoutput query="qryOffices">
                  <cfif #guiOfficeID# eq "B9D0F3F5-13BA-4E0C-AF55-037611A60577">
                  <cfset newguiOfficeID = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">
                  <option value="#newguiOfficeID#">#szOfficeName#</option>
                  <cfelse>
                  <option value="#guiOfficeID#">#szOfficeName#</option>
</cfif>
           
                  <!---<option value="#guiOfficeID#">#szOfficeName#</option>--->
                  </cfoutput>
                  </select><br/>
0
gdemariaCommented:
Good to see more code.   As I suggested, you need to scope your variables.   If the guiOfficeID is part of the Query, you need to indicate that because when you set the variable, you are setting it into a VARIABLE scope, you are not overwritingt he query scope value.  The output value doesn't know which scoped variable to use query or variable.   So it is using query which takes precedence.  

  <cfoutput query="qryOffices">
      <cfset theValue = qryOffices.guiOfficeID>
      <cfif theValue is "B9D0F3F5-13BA-4E0C-AF55-037611A60577">
          <cfset theValue = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">
          <cffile action="append" file="C:\home\logs\OfficeDropdownList.txt" output="AFTER OFFICE GUID: #theValue#">
      </cfif>
      <option value="#theValue#">#OfficeName#</option>
 </cfoutput>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gdemariaCommented:
Just to be clear, the problem was on this line...

 <cfoutput query="qryOffices">
        <cfif #guiOfficeID# eq "B9D0F3F5-13BA-4E0C-AF55-037611A60577">
                 <cfset guiOfficeID = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">

You believe you are writing to the variable qryOffices.guiOfficeID  but you are not, you are writing to the variable:    variables.guiOfficeID.      To change a query value you need to use a function setQuerycell().
0
_agx_Commented:
>> To change a query value you need to use a function setQuerycell().

Or use array notation.  
 
 <cfoutput query="qryOffices">
            <cfset qryOffices.guiOfficeID[CurrentRow] = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">
  </cfoutput>

Overwriting a query variable within a loop is near impossible without special handling.  One of those fun gotchas...
0
nachtmskAuthor Commented:
Ahh! Thanks! I will do what you suggested.
0
nachtmskAuthor Commented:
I just tried this and it worked fine

<cfif #guiOfficeID# eq "B9D0F3F5-13BA-4E0C-AF55-037611A60577">
                  <cfset qryOffices.guiOfficeID = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">
                  </cfif>

Is there some reason I should use setQuerycell(). as opposed to what I just did? And if I do need to use setQueryCell(), can you please show me how I would do that in this case?
0
_agx_Commented:
It works because you added the query scope, which resolves the ambiguity GD mentioned.  

                  qryOffices.guiOfficeID  <=== tells CF to look for query variable named "guiOfficeID"

Queries are strange beasts.  qryOffices.guiOfficeID is actually an array of values (one for each row in the query).  Inside a query loop, CF adds a little syntactic sugar that automatically translates the shortcut:

        "qryOffices.guiOfficeID"  

... into -->

        qryOffices.guiOfficeID[ currentRowWithinTheQueryLoop ].  

You can probably get away with what you have now. However, I've seen some strange things happen in loops, usually unintentional. If you really must modify the base query value,   I'd supply a row number explicitly (see example) . Net effect is same as using querySetCell.  

<cfset qryOffices = queryNew("")>
<cfset queryAddColumn(qryOffices, "guiOfficeID", ["B9D0F3F5-13BA-4E0C-AF55-037611A60577"])>

<cfoutput query="qryOffices">
    <br><cfdump var="BEFORE = #qryOffices.guiOfficeID#">
    <cfif guiOfficeID eq "B9D0F3F5-13BA-4E0C-AF55-037611A60577">
        <cfset qryOffices.guiOfficeID[CurrentRow] = "152A656F-C1AF-448C-BAB2-4D7B30CEEE65">
    </cfif>
    <br><cfdump var="AFTER = #qryOffices.guiOfficeID#">
</cfoutput>

Open in new window

0
_agx_Commented:
>>  I'd supply a row number explicitly

BTW, that's obviously a personal preference. When you run into one problem due to ambiguity,  it's better to solidify the code IMO. Make it more explicit to remove any chance of further issues. Also, leave comments so you'll remember when you modify the code months down the road.
0
nachtmskAuthor Commented:
Thanks very much to both of you. You both taught me something I didn't know!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.