简体   繁体   English

CRM v9-通过C#代码将数据保存在CRM实体中

[英]CRM v9 - Saving data in CRM entity through C# code

I have created a new CRM entity with 2 fields Name and Value . 我创建了一个新的CRM实体,其中包含2个字段NameValue In my C# code I am fetching the same two values with their names from Microsoft Unified Service Desk. 在我的C#代码中,我从Microsoft Unified Service Desk提取了两个相同的值及其名称。 Now I have these records in my C# Code and now i want to save these in the CRM Entity. 现在,我在C#代码中有这些记录,现在我想将它们保存在CRM实体中。 I am using below code to fetch the values for now, 我现在使用下面的代码来获取值,

 string QuestionAnswer = string.Empty;

        if (dialog.QuestionControlValidate)
        {
            if (ContextParameterName != string.Empty)
            {
                var ctx = Context.GetContext();

                if (dialog.QuestionControlType == "TextBox")
                    QuestionAnswer = dialog.QuestionControlTextQuestionAnswer.ToString();
                else if (dialog.QuestionControlType == "RadioButton")
                    QuestionAnswer = dialog.QuestionControlRadioButtonSelectionAnswer.ToString();

                var updatedContext = new Context(ctx)
                {
                    [ContextParameterName] = QuestionAnswer
                };

// Code to be added here
            }

I am getting the Name inside ContextParameterName and Value inside QuestionAnswer . 我在ContextParameterName内获取名称 ,在QuestionAnswer内获取 I want to store the these records inside CRM Entity which has the name and value fields. 我想将这些记录存储在具有名称和值字段的CRM实体中。

Your code to create a Record in CRM would be something like below 您在CRM中创建记录的代码如下所示

Entity customEntity = new Entity 实体customEntity =新实体

 Entity abc = new Entity()
                {
                    LogicalName = "new_EntityName",
                    ["fieldName"] = ContextParameterName,
                    ["fieldValue"] = QuestionAnswer

                };
                Guid newEntityRecordGuid = CrmConn.Create(abc);

newEntityRecordGuid will have newly created Record Guid newEntityRecordGuid将具有新创建的记录向导

I am getting the data from Microsoft USD inside C# code and then wanted to save it in CRM Entity. 我正在C#代码中从Microsoft USD获取数据,然后将其保存在CRM实体中。 And since in USD the connection is created automatically with the CRM environment therefore I achieved it without explicitly defining the connection. 而且由于以美元为单位,因此该连接是使用CRM环境自动创建的,因此我无需明确定义连接即可实现该连接。

                dictionary.Add("name", new CrmDataTypeWrapper(ContextParameterName, CrmFieldType.Raw));
                dictionary.Add("value", new CrmDataTypeWrapper(QuestionAnswer, CrmFieldType.Raw));
                dictionary.Add("id", new CrmDataTypeWrapper(updatedContext["Id"], CrmFieldType.Raw));

                Guid EvntId = _client.CrmInterface.CreateNewRecord("historicaldata",dictionary,"",false,new System.Guid());

The CreateNewRecord method creates a new activity against the target entity type CreateNewRecord方法针对目标实体类型创建一个新活动

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

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