Select to view content in your preferred language

Arcade - 'System.InvalidCastException'

855
9
08-23-2022 03:41 AM
girafee
Emerging Contributor

Hello! I have an expression "var d = Dictionary('Accounting', $feature.AssessmentID)\r\nreturn d" and if the value is null, I get an exception:
Values = '((Esri.ArcGISRuntime.RuntimeDictionary<string, object>)result.Result).Values' threw an exception of type 'System.InvalidCastException'

I researched that I could write like this "$feature.AssessmentID +'' " and no exception. Any other options?

Thanks.

0 Kudos
9 Replies
JohannesLindner
MVP Frequent Contributor

Hey!

This expression returns a dictionary. Are you sure you need a dictionary and not a string or something else?

What do you want to do with this expression? Is it for an Attribute Rule?

 


Have a great day!
Johannes
0 Kudos
girafee
Emerging Contributor

@JohannesLindnerI'm sure. I need to return the dictionary and work with it.
What options do we have?

0 Kudos
JohannesLindner
MVP Frequent Contributor

You could return a json string representation of the dictionary:

var d = Dictionary('Accounting', $feature.AssessmentID)
return Text(d)

 

But the problem isn't with the expression. You want to use the output dictionary somewhere and that is what's causing the error.

Can you tell us what you're trying to do? If we know what you're working with and what you want to accomplish, it's much easier to help.


Have a great day!
Johannes
0 Kudos
girafee
Emerging Contributor
public override async Task ExecuteAsync(Feature feature)
{

Esri.ArcGISRuntime.ArcadeExpression ArcadeEvaluatorExpression = new Esri.ArcGISRuntime.ArcadeExpression("var d = Dictionary('Accounting1', $feature.AssessmentID)\r\nreturn d"); // AssessmentID = null

ArcadeEvaluator ArcadeEvaluator = new ArcadeEvaluator(ArcadeEvaluatorExpression, ArcadeProfile.FormCalculation);

List<KeyValuePair<string, object>> profileVariables = new List<KeyValuePair<string, object>>();

profileVariables.Add(new KeyValuePair<string, object>("$feature", feature));

ArcadeEvaluationResult result = await ArcadeEvaluator.EvaluateAsync(profileVariables);

Dictionary<string, object> retValues = JObject.FromObject(result.Result).ToObject<Dictionary<string, object>>();

if (retValues != null)
{
   foreach (KeyValuePair<string, object> pair in retValues)
   feature.SetAttributeValue(pair.Key, pair.Value);
}

}
0 Kudos
JohannesLindner
MVP Frequent Contributor

The Arcade expression returns a Dictionary, but it seems like JS can't convert that.

Sadly, I have no idea about the JS API. You can probably get some help in the ArcGIS API for JavaScript - Esri Community


Have a great day!
Johannes
0 Kudos
girafee
Emerging Contributor

I want to have a null check in my expression and if it is null, return null as a string: "var d = Dictionary('Accounting', $feature.AssessmentID)\r\nreturn d". For example: "if($feature.AssessmentID == null) $feature.AssesmentID = 'null'".

Thanks.

0 Kudos
JohannesLindner
MVP Frequent Contributor

Like this?

if($feature.AssessmentID == null) {
    return "null"
}
return {"Accounting": $feature.AssessmentID}

 

Or do you want to set the value in the table to "null"?


Have a great day!
Johannes
0 Kudos
girafee
Emerging Contributor

e9ad3c23-a791-42dd-9869-8ace0060e68f.png

Hi! Could you please help me? I have the same problem and I don't know how to solve it. I need to return dictionary from Arcade Expression, and work with it. But if at least one value is null, I get an exception and can't work with other values.

 

public override async Task ExecuteAsync(Feature feature)
{
Esri.ArcGISRuntime.ArcadeExpression ArcadeEvaluatorExpression = new Esri.ArcGISRuntime.ArcadeExpression("var d = Dictionary() \r\n if($feature.AuthorizationNotice == null) \r\n { d['QuantityActual'] = $feature.Status \r\n d['Assessor'] = $feature.DueDate \r\n return d}"); // $feature.DueDate = null

ArcadeEvaluator ArcadeEvaluator = new ArcadeEvaluator(ArcadeEvaluatorExpression, ArcadeProfile.FormCalculation);

List<KeyValuePair<string, object>> profileVariables = new List<KeyValuePair<string, object>>();

profileVariables.Add(new KeyValuePair<string, object>("$feature", feature));

ArcadeEvaluationResult result = await ArcadeEvaluator.EvaluateAsync(profileVariables); // here we have System.InvalidCastException

Dictionary<string, object> retValues = JObject.FromObject(result.Result).ToObject<Dictionary<string, object>>();

if (retValues != null)
{
   foreach (KeyValuePair<string, object> pair in retValues)
   feature.SetAttributeValue(pair.Key, pair.Value);
}

}

 

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

I have the same problem

In fact, it's exactly the same problem, this is your thread...

Sadly, I'm as unqualified as before. I don't know anything about JS. I guess you couldn't find help in the JS Community?

 

I am qualified to look at your Arcade expression (it's different from the one you posted earlier), and I'm not sure what you're trying to achieve. 

var d = Dictionary() 
if($feature.AuthorizationNotice == null) {
    d['QuantityActual'] = $feature.Status
    d['Assessor'] = $feature.DueDate
    return d
}

 

Here's what your expression does:

  • if AuthorizationNotice is null, return a dictionary
  • if AuthorizationNotice is not null, return null

Is this the intended behavior?


Have a great day!
Johannes
0 Kudos