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);
}
- 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.
- 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.
now you can get particualr fileds value, you do something like below
if you want optionset values you do something like below
To set up? what type of data you want to set up, assuming you want to set up optionset value
The INDEX is an int you can look up in your optionset editor (default values are several digit long).