I am getting exception while creating new account in Dynamics 365 CRM via Microsoft.PowerPlatform.Dataverse.Client

huangapple go评论66阅读模式
英文:

I am getting exception while creating new account in Dynamics 365 CRM via Microsoft.PowerPlatform.Dataverse.Client

问题

我正在使用以下代码创建新帐户:

string cs = "connection";

ServiceClient serviceClient = new(cs);

Entity account = new("account");
account.Attributes.Add("name", "name");
account.Id = Guid.NewGuid();
account["custom_attribute1"] = "custom_attribute1";
account.Attributes.Add(new KeyValuePair<string, object>("custom_attribute2", custom_attribute2.Admin));

// serviceClient.Update(account);
serviceClient.Create(account);

我正在重构旧解决方案,在那里我们有一些自定义属性,其值是枚举值。出于某种原因,当我像这样添加它时,它不起作用。我尝试使用整数和字符串值,但没有任何变化。

我收到以下错误消息:

未处理的异常。System.ArgumentNullException:值不能为空(参数 'value')
在 Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.Create(Entity entity)

异常消息远不如我希望的那样清晰,所以我在调试方面遇到困难。如果有人能够帮助我,我将不胜感激。

英文:

I am creating new account using this code:

string cs = &quot;connection&quot;;

ServiceClient serviceClient = new(cs);

Entity account = new(&quot;account&quot;);
account.Attributes.Add(&quot;name&quot;, &quot;name&quot;);
account.Id = Guid.NewGuid();
account[&quot;custom_attribute1&quot;] = &quot;custom_attribute1&quot;;
account.Attributes.Add(new KeyValuePair&lt;string, object&gt;(&quot;custom_attribute2&quot;, custom_attribute2.Admin));

// serviceClient.Update(account);
serviceClient.Create(account);

I am refactoring old solution and there we had some custom attributes where value is Enum value. For some reason when I add it like this it's not working. I tried to use integer and string values but nothing changed.

I get this error:

> Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'value')
> at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.Create(Entity entity)

Exception message is by far not clear as I wanted it to be so I am struggling with debugging. I'd appreciate it if somebody can assist me

答案1

得分: 1

首先,您需要检查连接是否正常工作,通常在初始化ServiceClient之后执行WhoAmIRequest来进行检查:

WhoAmIResponse response = serviceClient.Execute(new WhoAmIRequest());

如果这部分代码不引发异常,那么连接正常工作。

要创建一个帐户,最简单的代码如下:

Entity account = new Entity("account");
account["name"] = "TEST";
serviceClient.Create(account);

如果您在更新特定属性时遇到问题,需要检查属性类型。例如,如果是OptionSet/Choice(您提到Enum,它包含一个数值),语法将如下所示(假设要设置的值为2):

account["custom_attribute1"] = new OptionSetValue(2);
英文:

First of all you need to check if the connection works correctly, normally you do a WhoAmIRequest after you initialize the ServiceClient

WhoAmIResponse response = serviceClient.Execute(new WhoAmIRequest());

if this piece doesn't throw an exception the connection works.

In order to create an account the simplest code is:

Entity account = new Entity(&quot;account&quot;);
account[&quot;name&quot;] = &quot;TEST&quot;;
serviceClient.Create(account);

if you have problems to update a specific attribute, you need to check the attribute type, if for example is an OptionSet/Choice (you mentioned Enum that holding a numeric value can be connected) the syntax would be (assuming the value you want to put is 2):

account[&quot;custom_attribute1&quot;] = new OptionSetValue(2);

huangapple
  • 本文由 发表于 2023年3月31日 20:02:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定