Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

Issues with EntityReference in Plugin

$
0
0

I'm not sure what I am doing wrong here. I have used this code over and over again on other plugins with no issues. I'm practically copy pasting code that works and only changing the field names.

Anyway I am getting an error message "Unable to cast object of type Microsoft.Xrm.Sdk.EntityCollection" to type "Microsoft.Xrm.Sdk.EntityReference".

The bolded line seems to be causing the issue. But maybe I'm wrong. Can anyone see what I'm doing wrong this time? Thanks!

[edit] This plugin runs on phone call create. So that when a quick campaign is created and all the phone calls are displayed the state of the account is on the phone call record and we can sort by state to assign out the calls. I'm not sure if the [to] field is what's causing the issue? But I've used the same code for customerid off of the Case entity and it works fine.

Entity entity = (Entity)context.InputParameters["Target"];EntityReference accountId = (EntityReference)entity.Attributes["to"];
                Entity account = service.Retrieve("account", accountId.Id, new ColumnSet("new_state"));
                int state = (int)account.GetAttributeValue<OptionSetValue>("new_state").Value;

                entity["new_callstate"] = new OptionSetValue(state);
P.S. Since I'm here. Is there a way to check the entity type? I only want this to run when the to is an account. I have used if (accountId.LogicalName == "account") but that throws an error as well. Again thanks for any help, much appreciated.



Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>