Using cfif in coldfusion
Asked Answered
L

2

4

For example, please consider the following code snippet:

Scenario 1:

The company name I received as a part of SOAP response is as follows:

<Company>Amazon, Inc </Company>

Scenario 2:

Another company name I received as a part of SOAP response is as follows:

<Company>Google, Inc </Company>

Basically, from the user end I am inputting some information and based on that I am getting different company names inside the <Company> tag.

The following code shows how I am storing the response in XMLResponse variable

 <cfset XMLResponse = XmlParse(

    httpResponse.fileContent.Trim()

    ) />

The following piece of code shows how I am parsing the response and storing the content in a variable:

<cfset arrCOMPANY = XmlSearch(
             XMLResponse,
             "//*[name()='Company']"
             ) /> 

So now I have arrCOMPANY = Amazon, Inc is I happen to be in Scenario and Google, Inc otherwise.

My Question:

I have to insert these data into the database based on the company name and set an integer field to be equal to 1 if the company name is Amazon,Inc and set it to zero otherwise for all other companies.

Please let me know if I am following the right path:

I am thinking of writing two cfqueries based on the company names and hence I am thinking to place the following condition :

<cfif arrCompany eq "Amazon, Inc">

// Here I will write cfquery with an integer field = 1


Or 

<cfif arrCompany eq "Google, Inc">

// Here I will write cfquery with integer field 0

So, am I doing the right comparision, I am wondering whether the company names that I am comparing with eq sign will be actually compared or not as it contains spaces after first word ( say for example the space between Amazon, and Inc).

Please share your experience.

Thanks

Here is what I did:

I have applied trim function on specific element as well. For example:

The Value in the CompanyName variable is Amazon, Inc

<cfset CompanyName = Trim(arrCompany[1])>  // adding index 1 because  it's a complex structure

Here is how I am trying to use cfif condition:

<cfif CompanyName eq "Amazon, Inc">

    <cfset m = 1>

    <cfelse>

    <cfset m = 0>  

    </cfif> 

    <cfoutput>#m#</cfoutput>

Despite doing above, I am getting 0 as my output. Please let me know if I am comparing wrong.

Loring answered 29/10, 2013 at 13:43 Comment(2)
You do not need separate queries just to set a bit flag. What is the issue you are experiencing? If the node contains the exact value Amazon, Inc it will be considered equal. Is something different happening in your actual code?Pratincole
Try an output of the companyName between quotes so that you can see what is the exact trimmed value. <cfoutput>'#companyName#'</cfoutput>. the quotes will help to see if there is any more characters that trim cannot remove.Architectural
P
1

<cfset CompanyName = Trim(arrCompany[1])>

If you cfdump that variable, or wrap it in xmlFormat(), you will see it contains the whole node, not just the text. It is easy to miss when using only cfoutput, because the browser treats the xml as tags, so they are not visible unless you view the source:

<cfoutput>#XMLFormat(CompanyName)#</cfoutput>

To grab only the node text use the xmlText attribute, then trim it:

<cfset CompanyName = trim( arrCompany[1].xmlText )>

Edit: Also, as mentioned in the comments, you do not need separate queries just to set a bit flag. Simply set a CF variable to 1 or 0, based on the company name, then use it in your query.

Pratincole answered 30/10, 2013 at 10:48 Comment(1)
Also, consider using more descriptive variable names throughout the code. For example, instead of m a better name for the boolean flag might be isAmazon.Pratincole
S
0

The trim function only works on the entire XML fragment. You want to:

<cfset arrCOMPANY = trim(XmlSearch(
         XMLResponse,
         "//*[name()='Company']"
         )) />

Having said that you many want to consider

a) Having the database do the translation from string to integer
OR
b) Storing the whole extracted string
OR
c) Storing the whole XML fragment

Stadia answered 29/10, 2013 at 13:49 Comment(2)
Thanks. Please check my updated question with the problem I am facing now. ThanksLoring
When I have two strings that I think match but don't, I do a quick sanity check with Len(), it that doesn't reveal anything, I might <cfoutput> both strings, run the page, save the results into a text file, and compare with a hex editor. Last but not least, I would try something tacky like <cfset arrCompany="#arrCompany#">Stadia

© 2022 - 2024 — McMap. All rights reserved.