Variable is assigned but never used (Arcade Question)

2357
10
Jump to solution
01-12-2023 02:29 PM
TimHayes3
Frequent Contributor

I have written an Arcade expression, it has 3 variables named link1, link2, and link3. When I test it in Map Viewer Classic, it says that these variables are assigned but never used. Can someone assist me in helping determine what is wrong with the syntax of my code? (I also tried the same expression except using Decode with same results, says variable assigned but never used)

var link1 = 'https://rtlt.preptoolkit.fema.gov/Public/Resource/ViewFile/9-508-1192?type=Pdf&s=Name&p=7'
var link2 = 'https://rtlt.preptoolkit.fema.gov/Public/Resource/ViewFile/9-508-1192?type=Pdf&s=Name&p=7'
var link3 = 'https://rtlt.preptoolkit.fema.gov/Public/Resource/ViewFile/9-508-1192?type=Pdf&s=Name&p=7'
if ($feature.RESOURCETYPE == "Type II (500 - 1,999 persons)")
return link1 = 'Type II';
if ($feature.RESOURCETYPE == "Type III (250 - 499 persons)")
return link2 = 'Type III';
If ($feature.RESOURCETYPE == "Type IV (249 or fewer persons)")
return link3 = 'Type IV';

 

variable is assigned but never used.png

 

var link = 'https://rtlt.preptoolkit.fema.gov/Public/Resource/ViewFile/9-508-1192?type=Pdf&s=Name&p=7'
var t = Decode($feature.RESOURCETYPE,
'Type II (500 - 1,999 persons)', 'Type II (500 - 1,999 persons)',
'Type III (250 - 499 persons)', 'Type III (250 - 499 persons)',
'Type IV (249 or fewer persons)', 'Type IV (249 or fewer persons)',
'')

If ($feature.RESOURCETYPE != "TBD - Leave Blank"){
return link = t
}

 

0 Kudos
1 Solution

Accepted Solutions
TimHayes3
Frequent Contributor

Rhett and Doug thank you for your assistance. I found what I needed in an earlier post from December: https://community.esri.com/t5/arcgis-online-questions/arcade-add-url-to-pop-up/m-p/1240570#M49331 

 

View solution in original post

0 Kudos
10 Replies
DougGreen
Frequent Contributor

Well it looks like there's some confusion here. First, you assign the value of the three variables. Then, you are re-assigning the same three variables to different values within your "if" statements. I think the error is due to trying to return the variables at the same time as assigning them the new value. I think you need to figure out if you want to return the values within your "if" statements or if you want to return the url's you set as the initial values of your variables. Then, set the values within the "if" statements. When you're all done assigning/setting values, return one item. You can't return all three variables I don't believe.

TimHayes3
Frequent Contributor

Thanks, I see what you mean. I made some changes to the code and it gets me the url as the output, but what I want is to have hyperlink text and not just the url in the field. Example output would be the hyperlinked text "Type IV" visible in the field. Any thoughts on how I can add this text. I am new to Arcade, so I assume it would be added in the IF statement Return line? i 

if ($feature.RESOURCETYPE == "Type II (500 - 1,999 persons)")
{return link1;}
if ($feature.RESOURCETYPE == "Type III (250 - 499 persons)")
{return link2;}
If ($feature.RESOURCETYPE == "Type IV (249 or fewer persons)")
{return link3;}

 

 

 

0 Kudos
RhettZufelt
MVP Notable Contributor

If you are trying to return the respective link based on the value of the RESOURCETYPE field (though in the example, all the links are the same so overkill):

 

var link1 = 'https://rtlt.preptoolkit.fema.gov/Public/Resource/ViewFile/9-508-1192?type=Pdf&s=Name&p=7'
var link2 = 'https://rtlt.preptoolkit.fema.gov/Public/Resource/ViewFile/9-508-1192?type=Pdf&s=Name&p=7'
var link3 = 'https://rtlt.preptoolkit.fema.gov/Public/Resource/ViewFile/9-508-1192?type=Pdf&s=Name&p=7'
if ($feature.RESOURCETYPE == "Type II (500 - 1,999 persons)"){
     return link1
     }
if ($feature.RESOURCETYPE == "Type III (250 - 499 persons)"){
    return link2
    }
If ($feature.RESOURCETYPE == "Type IV (249 or fewer persons)"){
    return link3
    }

 

R_

TimHayes3
Frequent Contributor

Yeah I saw that, I made a change, it's much cleaner:

if ($feature.RESOURCETYPE == "Type II (500 - 1,999 persons)")
{return link;}
if ($feature.RESOURCETYPE == "Type III (250 - 499 persons)")
{return link;}
If ($feature.RESOURCETYPE == "Type IV (249 or fewer persons)")
{return link;}
0 Kudos
RhettZufelt
MVP Notable Contributor

another way, works similar to IN

var link = 'https://rtlt.preptoolkit.fema.gov/Public/Resource/ViewFile/9-508-1192?type=Pdf&s=Name&p=7'

var field= $feature.RESOURCETYPE

var arr = ["Type II (500 - 1,999 persons)","Type III (250 - 499 persons)", "Type IV (249 or fewer persons)"]

if (Includes(arr, field)){
    return link
}

I use this method when there are a lot of comparisons, or I will be modifying it over time.

No need to create a bunch of other if statements, just add the values into the arr array.

R_

TimHayes3
Frequent Contributor

Excellent, that works too. Any idea how I can have hyperlink text instead of just the url as a value in the field? the syntax skips me at the moment. Example: 

if ($feature.RESOURCETYPE == "Type II (500 - 1,999 persons)")
{return link;} <-- what do I need to add here to include the text "Type II" hyperlinked to link?
if ($feature.RESOURCETYPE == "Type III (250 - 499 persons)")
{return link;}
If ($feature.RESOURCETYPE == "Type IV (249 or fewer persons)")
{return link;}
0 Kudos
RhettZufelt
MVP Notable Contributor

Not really sure, and not able to test, at least till next week or so, but I believe you will need to return an actual web link, not just the URL.

Something like this might work, but not sure how it will display in the popup.  will need to test:

var link = 'https://rtlt.preptoolkit.fema.gov/Public/Resource/ViewFile/9-508-1192?type=Pdf&s=Name&p=7'

var field= $feature.FEATURECODE

var arr = ["Type II (500 - 1,999 persons)","Type III (250 - 499 persons)", "Type IV (249 or fewer persons)"];
console(link)
if (Includes(arr, field)){
    var newlink = '<a href=' + link + 'target="_blank" rel="nofollow">(<b>Click Here</b>)</a>'
    return newlink
}

R_

TimHayes3
Frequent Contributor

I changed var field = $feature.RESOURCETYPE and the output in the popup was this:

 <a href= + link + target="_blank" rel="nofollow">(<b>Click Here</b>)</a>

It seems to return newlink as a text output in the popup. 

I wonder if it is even possible to output hyperlink text in a popup. I know how to do this if the values in the field are the same, but my situation is conditional. I will keep you posted on what I find out over the next week or so. Rhett, if you, or anyone else knows how to do this I would be much appreciative. Thank you for the assistance you have provided so far. 

0 Kudos
RhettZufelt
MVP Notable Contributor

I know that if you use the first one I supplied above that just returns the URL, you can add that as a popup expression, then add the expression in the fields list of the popup.  This will give you a dynamic link that can use variable substitution to create the URL.  

My example for testing (data I have), but still just returning the URL itself, not <a href/>:

var link = 'https://mygisorg.maps.arcgis.com/apps/webappviewer/index.html'

var field= $feature.Notes

var newlink = link + field 
return newlink

with the value of Notes field being the id (the rest of the URL) so they are concatenated to form final URL.

When added as a field in the popup, looks like:

RhettZufelt_0-1673571476625.png

And when I click on the "View", it opens the dynamically created URL in new window.

But, have not figured out a way, or if it is possible to have the returned link say what you want.  Might be stuck with   "View", but at least you can 'build' the URL dynamically.

R_