Using Arcade Expressions to return image urls based on DateDiff

978
8
Jump to solution
02-27-2020 05:17 AM
TimFlynn
New Contributor III

Hello, I'm trying to write an expression that will return one of three image urls based on an inspection date.  The distinction being the most recent date is less than 30 days, greater than 30 days, or there is no recent date.  The expression is then pasted into the URL of an image for a pop up.  Here is what I have so far.

var relatedrecords = OrderBy(FeatureSetByRelationshipName($feature,"AST_Inspection"), "insp_date DESC");
var cnt = Count(relatedrecords);
var relatedinfo = "";
if (cnt>0) {
    var info = First(relatedrecords);
    relateddate = Text(ToLocal(info.insp_date), "MM/DD/Y");
    var diff = DateDiff(Now(), relateddate, "days");
        relatedinfo = IIf (diff<30, "img1url", "img2url");
} else {
    relatedinfo= "img3url"    
}
return relatedinfo‍‍‍‍‍‍‍‍‍‍‍‍

I tried nesting If/Else statements and it just kept giving the Else image url.  I'm sure there is a simpler way to do this, but I'm not sure.

Thanks.

0 Kudos
1 Solution

Accepted Solutions
TimFlynn
New Contributor III

I figured it out!  Sorry to bug you.  Turns out I was doing too much, and the DateDiff function was getting an error because of the text formatting of the insp_date.  The code that works is as follows:

var relatedrecords = Top(OrderBy(FeatureSetByRelationshipName($feature,"AST_Inspection"), "insp_date DESC"), 1);
var cnt = Count(relatedrecords);

var relatedinfo = "";
if (cnt>0) {
    var info = First(relatedrecords);
    var diff = Round(DateDiff(Now(), ToLocal(info.insp_date), "days"));
        relatedinfo = IIf (diff<30, "img1url", "img2url");
} else {
        relatedinfo = "img3url"
}
 return relatedinfo

Thanks for taking a look though!

View solution in original post

0 Kudos
8 Replies
TimFlynn
New Contributor III

Xander Bakker‌ any ideas where I'm going wrong?

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Tim Flynn ,

The way you use "img1url", "img2url" and "img3url" in the expression will not return a field value, but just the text "img1url", "img2url" or "img3url". 

In case these are fields in the related "info", you should use:

relatedinfo = IIf(diff<30, info.img1url, info.img2url);

However, this is not applicable for line 10 ("img3url") since you wont have any related records.

0 Kudos
TimFlynn
New Contributor III

I am trying to get it to return those text values.  The image urls will not be stored as fields, they'll be static.  I've been able to get it to pass img2url but not img1url when the related field "insp_date" is less than 30 days old.  Here's what I've got now, after realizing there was a bit of a syntax error before.

var relatedrecords = OrderBy(FeatureSetByRelationshipName($feature,"AST_Inspection"), "insp_date DESC");
var cnt = Count(relatedrecords);
var relatedinfo = "";
if (cnt>0) {
    var info = First(relatedrecords);
    var relateddate = Text(ToLocal(info.insp_date), "MM/DD/Y");
    var diff = DateDiff(Now(), relateddate, "days");
        relatedinfo = IIf (diff<30, "img1url", "img2url");
} else {
    relatedinfo= "img3url"    
}
return relatedinfo‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
TimFlynn
New Contributor III

I figured it out!  Sorry to bug you.  Turns out I was doing too much, and the DateDiff function was getting an error because of the text formatting of the insp_date.  The code that works is as follows:

var relatedrecords = Top(OrderBy(FeatureSetByRelationshipName($feature,"AST_Inspection"), "insp_date DESC"), 1);
var cnt = Count(relatedrecords);

var relatedinfo = "";
if (cnt>0) {
    var info = First(relatedrecords);
    var diff = Round(DateDiff(Now(), ToLocal(info.insp_date), "days"));
        relatedinfo = IIf (diff<30, "img1url", "img2url");
} else {
        relatedinfo = "img3url"
}
 return relatedinfo

Thanks for taking a look though!

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Tim Flynn ,

I'm glad it works now, but if I look at your expression, I still see the same things that in theory should not work. Did you share the correct expression? If this is correct, can you share a screenshot of how the result looks?

0 Kudos
TimFlynn
New Contributor III

Sure.  Here's an instance where the most recent inspection date was 2/20/2020, which is within 30 days.

It passes the url for the checkmark image into the inspection.  I use the expression's tag in the image tag.  Underneath I use the same expression to pass phrases instead of urls.  This one says, "Inspection Current."

Here is a pop up for a point with an inspection date from December, well over 30 days old:

And for all points that don't have inspections this is the result of the Else statement:

And just for good measure, here is the expression for the message underneath the image:

var relatedrecords = Top(OrderBy(FeatureSetByRelationshipName($feature,"AST_Inspection"), "insp_date DESC"), 1);
var cnt = Count(relatedrecords);

var relatedinfo = "";
if (cnt>0) {
    var info = First(relatedrecords);
    var diff = Round(DateDiff(Now(), ToLocal(info.insp_date), "days"));
        relatedinfo = IIf (diff<30, "Inspection Current", "Inspection Due");
} else {
        relatedinfo = "No Inspections"
}
 return relatedinfo‍‍‍‍‍‍‍‍‍‍‍‍
XanderBakker
Esri Esteemed Contributor

Thanks for sharing Timothy.Flynn_ocfl . I looks great!

Tweed_ShireAdmin
New Contributor II

Tim,
I am trying to do a similar thing but just wondering where you set the location of the image in arcade expression. I have the image i want to use loaded to AGOL so wanting to know how I i apply what you have done in my situation. 
I am wanting to showthe image of a tick instead of the word Yes for values in the table. 

This is the arcade expression i have but it just returns the text of the url


var display = $feature["COASTAL_WETLANDS"]=='YES'
return IIF(display, 'info.https://tweedsc.maps.arcgis.com/sharing/rest/content/items/ca99d3ee3c7248d5b4757422aedc45fb/data', '')

0 Kudos