Select to view content in your preferred language

Arcade split at comma and return URL

983
3
09-05-2023 09:32 AM
ZachBodenner
MVP Regular Contributor

Hello,

For starters, I've been referencing this thread to try to solve my arcade question.

I have a feature class of utilities points (hydrants, curb stops etc) with a field that references a document number where a user can fin as-built information. Those documents live in our document management system and we use the information in the "As-Builts" field (string) to populate the "As-Built Link." As an example, if the As-Built number is 930, an arcade function to calculate the link is "http://documentmanager/browse.aspx?searchParams"+ As-Built+"moreSearchParams";

Now, data entry in the past hasn't been perfect, which has resulted in a few hurdles: a) sometimes the field is empty, b) sometimes a feature appears on two as-builts, and attribute value reads something like 930,931 (which is to say, comma-separated as-built numbers), c) some features just have a string of text instead of a number, like "building plan"

I've been able to calculate a link out to the document manager which essentially excludes everything that's not one single number (because if there are multiple numbers getting calculated into the link, it will screw up the URL search). But I feel like this can be improved and automated, so I've been trying to write an attribute rule to do this when features are edited or added. 

My end goal: the URL link field should be populated if there is only one as-built number, and should at least have a link calculated from the first number if there are multiple as-built numbers. If the field is empty, it would be great to have a string return, something like "no- as-built found,". Bonus for being able to calculate multiple links from the as-built number into the URL field so that each number could produce it's own document link. 

To that end, following the above linked thread, I've tried to use the Split() function to pull out the first number of the split array and run the calculation. However, my rule is returning the default "not found" text regardless of whether there is a number (or two) or not.

var hasAB = !isEmpty($feature.AsBuilts);
var splitAB = Split(hasAB,",");
var countAB = Count(splitAB)

console (splitAB)
Console(countAB +" items in split array")
 
If (countAB > 1)
{return http://documentmanager/browse.aspx?searchParams"+splitAB [1]+"moreSearchParams";}
else
{return "No as-built found";}
Tags (3)
0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor

Your first line returns a Boolean, so the variable splitAB will always be an array of 1 item. Give this a try

var hasAB = $feature.AsBuilts;
if (IsEmpty(hasAB)) return "No as-built found";
var splitAB = Split(hasAB,",");

var output; 
for (var index in splitAB) {
  output += `http://documentmanager/browse.aspx?searchParams${splitAB[index]}moreSearchParams${TextFormatting.NewLine}`;
}
return output;

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

You're splitting hasAB, which is a boolean value. You want to split the actual field value of AsbUilts.

You're checking for array size greater than one, so at least two numbers. You probably want to check for greater than zero.

You're using splitAB[1], which is the second element of the array (Arcade [and most other programming  languages] uses zero-based indexes). If there is only one element in the array, the expression will fail.

Your url looks weird. I think it has to be formatted like http://bla/browse.aspx?parameter=number&otherparameter=value

 

 

This expression will return a semicolon-separated list of urls (if applicable). It will return a default message for empty fields, and it will return the field value if it is not a number.

var ab = $feature.AsBuilts

if(IsEmpty(ab)) { return "No as-built found" }

var urls = []
var ab_split = Split(ab, ",")
for(var i in ab_split) {
    var n = Number(Trim(ab_split[i]))
    if(IsNaN(n)) {
        Push(urls, ab_split[i])  // if the value is not a number, just use the value
    } else {
        Push(urls, `http://documentmanager/browse.aspx?searchParams=${n}&moreSearchParams`)
    }
}

return Concatenate(urls, ";")

 

 

Alternatives:

  • replace line 10 with continue, this will skip all non-numeric values
    • add this before the return to give an error message:
    • if(Count(urls) == 0) { return "No numeric as-built found" }
  • Only return the first url: replace line 16 with this (and add the count check from above)
    • return urls[0]

 

I haven't tested it, but I don't think this will return clickable links for multiple as-builts. You would have to use an Arcade element (not an expression) in a popup for that.

 


Have a great day!
Johannes
0 Kudos
ZachBodenner
MVP Regular Contributor

Well first of all, thank you both, both of these work really great for the attribute rule, but both have the unfortunate drawback, as you mentioned @JohannesLindner, of not producing clickable links once they're published to a server (works fine in Pro though). For frustrating reasons, it's not super feasible to add an arcade element to that popup, but I figured for the sake of learning I'd give it a try, but nothing shows up where the element should be.

0 Kudos