简体   繁体   English

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

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

I am trying to use the DotNetSDataClient library to add a new contact in Infor CRM. 我正在尝试使用DotNetSDataClient库在Infor CRM中添加新联系人。 I have attempted to follow this documentation under the Create section. 我试图按照“ 创建”部分下的说明文档进行操作。 When I run the example code, I receive the error "The Contact's Account is required." 当我运行示例代码时,收到错误消息“需要联系人的帐户”。 This makes sense, because I believe each contact in the database must be associated with an account. 这是有道理的,因为我认为数据库中的每个联系人都必须与一个帐户关联。 I modified the code to specify an existing account, but now I receive the error "We're sorry, you've encountered an error. If applicable, please try again." 我修改了代码以指定现有帐户,但现在收到错误消息“很抱歉,您遇到了错误。如果适用,请重试。” with the innerexception, "The remove server returned an error: (500) Internal Server Error." 带有内部异常,“删除服务器返回错误:(500)内部服务器错误。”

Here is my code. 这是我的代码。

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; }
}

Does anyone have any ideas about what I am doing wrong? 有人对我在做什么错有任何想法吗?

I also posted this question on GitHub and Ryan Farley provided the answer. 我还将这个问题发布在GitHub上,Ryan Farley提供了答案。 I wanted to include it here in case anyone else needs this solution. 我想在这里包括它,以防其他人需要此解决方案。

The issue is with how the library serializes the data. 问题在于库如何序列化数据。 Here is Ryan's response: 这是Ryan的回应:

"The real issue here is that the POCO for Account has a collection of contacts. The DotNetSDataClient is serializing that as null and the SData server doesn't like that. Even setting that collection to new List() will fail because it will get serialized as []. where it should look like “这里真正的问题是Account的POCO具有联系人集合。DotNetSDataClient正在将其序列化为null,而SData服务器则不喜欢这样。即使将该集合设置为new List()也会失败,因为它将被序列化as []。应该看起来像什么

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

When doing a POST with an existing parent or related entity, SData expects to only receive the $key and nothing else anyway. 与现有的父级或相关实体进行POST时,SData期望仅接收$ key,而不会收到其他任何消息。 So, when the Contact class gets serialized, what you should be sending with the contact data is 因此,当Contact类被序列化时,应该与联系数据一起发送的是

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

and nothing more, but that is not the case, the library is serializing and sending everything." 仅此而已,但事实并非如此,该库正在序列化并发送所有内容。”

At this point, the solution is to create an account class that only has an id (key) property. 此时,解决方案是创建一个仅具有id(键)属性的帐户类。

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

At some point the DotNetSDataClient library may be updated to handle this situation, but for now this is the solution. 在某些时候,可能会更新DotNetSDataClient库以处理这种情况,但是现在这是解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM