简体   繁体   中英

Passing values from Dynamics CRM entity form to external web service through plug-in execution method

I want to pass to external web service some values of an entity (Case/incident) while new record is going to be created.

I have a model for preparing data which have to be sent to web service as below:

public class TicketViewModel
{
    public string CaseID { get; set; }
    public string Subject { get; set; }
    public string Description { get; set; }
    public string CreateTime { get; set; }
    public string Owner { get; set; }
    public string States { get; set; }
    public string Assigned { get; set; }
}

Here is my code inside Execute() method:

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                try
                {
                    var entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName != "incident") // The logical name for Case entity
                        return;

                    Guid recordID = entity.Id;

                    var ticket = new CaseViewModel
                    {
                        // Retrieving Intended Fields Value
                    };

                    BasicHttpBinding httpBinding = new BasicHttpBinding();
                    httpBinding.Name = "HttpBinding_Service";
                    httpBinding.Security.Mode = BasicHttpSecurityMode.None;
                    httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                    httpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
                    httpBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

                    EndpointAddress epa = new EndpointAddress(@"webservice/url/address");
                    CallChamberPortalSoapClient tcClient = new CallChamberPortalSoapClient(httpBinding, epa);

                    var res = tcClient.addTicket(//Passing Intended Fields Value);

                    entity["X"] = res.ToString();
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException("Failed to register ticket by this error: " + ex.Message);
                }
  1. My first question is how to retrieve intended fields value on Create new record? I have used entity["X"] to get value of "X" field but nothing returned.
  2. My second question is how to set value of a field on Update a record? Using same expression ( entity["X"] = "NewValue" ) not worked for me.

Note: sample static data has sent to web service successfully and it returned true as result.

EDIT:

I tried to get values as below but have error in CRM create record event.

ColumnSet cs = new ColumnSet(new string[] {
   "ticketnumber", "title", "description", "createdon", "customerid", "new_peygiriii", "createdby" });
    Entity wholeCase = service.Retrieve("incident", recordID, cs);

Owner = wholeCase.GetAttributeValue<EntityReference>("customerid").ToString();

Error:

Unable to cast object of type Microsoft.Xrm.Sdk.OptionSetValue to type Microsoft.Xrm.Sdk.EntityReference

Thanks.

First, You should register your plugin in Dynamics as Post operation (create). Reason once the record is created in System, you will get it's Guid and so on. This is best way and in addition make your plugin Asynchronous (syn only if it is a real must for your use case).

Now when you create a recrod in crm plugin will get it's context as you are doing.

 var entity = (Entity)context.InputParameters["Target"];

now you can get particualr fileds value, you do something like below

  if(entity.contains("field name")){
    var recordName=entity.GetAttributeValue<string>("field name");
    }

if you want optionset values you do something like below

if(entity.contains("optionset field name")){
    int selectedTopic = entity.GetAttributeValue<OptionSetValue>("optionset field name").Value
String text = entity.FormattedValues["optionset field name"].ToString();


    }

To set up? what type of data you want to set up, assuming you want to set up optionset value

entity["X"] = new OptionSetValue(INDEX)

The INDEX is an int you can look up in your optionset editor (default values are several digit long).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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