如何使用 DotNetSDataClient 在 Saleslogix Infor CRM 中创建新联系人

How to create a new contact in Saleslogix Infor CRM using DotNetSDataClient

我正在尝试使用 Create 部分下的 DotNetSDataClient library to add a new contact in Infor CRM. I have attempted to follow this documentation。当我 运行 示例代码时,我收到错误 "The Contact's Account is required." 这是有道理的,因为我相信数据库中的每个联系人都必须与一个帐户相关联。我修改了代码以指定一个现有帐户,但现在我收到错误 "We're sorry, you've encountered an error. If applicable, please try again." 和 innerexception "The remove server returned an error: (500) Internal Server Error."

这是我的代码。

public void someFunction(){
    var client = new SDataClient("https://domain/sdata/slx/dynamic/-/")
    {
        UserName = "username",
        Password = "password"
    };

    var contact = new Contact
    {
        Account = new Account
        {
            AccountName = "accountName",
            Id = "accountId"
        },
        Address = new Address
        {
            Address1 = "1234 Address",
            City = "someCity",
            PostalCode = "12345",
            State = "ST"
        },
        FirstName = "John",
        LastName = "Doe"
    };

    var contactOptions = new SDataPayloadOptions { Include = "Address" };

    try
    {
        contact = client.Post(contact, null, contactOptions);
    }
    catch (Exception ex)
    {
        var error = ex.Message;
    }
}

[SDataPath("accounts")]
public class Account
{
    [SDataProtocolProperty(SDataProtocolProperty.Key)]
    public string Id { get; set; }

    public string AccountName { get; set; }
    public List<Contact> Contacts { get; set; }
    public string Status { get; set; }
    public string Type { get; set; }
}

[SDataPath("contacts")]
public class Contact
{
    [SDataProtocolProperty(SDataProtocolProperty.Key)]
    public string Id { get; set; }

    public Account Account { get; set; }
    public Address Address { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string FullName { get; set; }
    public string LastName { get; set; }
    public DateTime? ModifyDate { get; set; }
    public string Status { get; set; }
}

[SDataPath("addresses")]
public class Address
{
    [SDataProtocolProperty]
    public string Key { get; set; }
    public string Address1 { get; set; }
    public string Address3 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string CountryCode { get; set; }
    public string Description { get; set; }
    public string PostalCode { get; set; }
    public string State { get; set; }
    public string Street { get; set; }
}

有人知道我做错了什么吗?

我还在 GitHub 上发布了这个问题,Ryan Farley 提供了答案。我想把它包括在这里,以防其他人需要这个解决方案。

问题在于库如何序列化数据。这是 Ryan 的回复:

“这里真正的问题是 Account 的 POCO 有一个联系人集合。DotNetSDataClient 将其序列化为 null 而 SData 服务器不喜欢这样。即使将该集合设置为 new List() 也会失败因为它将被序列化为 []。它应该看起来像

"Contacts": { "$resources": [] }

当对现有的父实体或相关实体执行 POST 时,SData 预计只会收到 $key 而不会收到其他任何东西。因此,当联系人 class 被序列化时,您应该与联系人数据一起发送的是

"Account": { "$key":"AXXX00000001" }

仅此而已,但事实并非如此,库正在序列化并发送所有内容。"

此时,解决方案是创建一个只有一个id(key)属性的账户class。

[SDataPath("accounts")]
public class Account
{
    [SDataProtocolProperty(SDataProtocolProperty.Key)]
    public string Id { get; set; }
}

在某些时候,DotNetSDataClient 库可能会更新以处理这种情况,但目前这是解决方案。