Thanks! It worked. But I am not able to update a field of a case record. Find the below full code:
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using System.Text;
namespace GridToDescription
{
public class GridToDesc : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Get a reference to the Organization service.
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity objContext = (Entity)context.InputParameters["Target"];
if (objContext.LogicalName != "incident")
{
tracingService.Trace("1");
return;
}
else
{
//Entity SourceCaseID = (Entity)service.Retrieve("incident", context.PrimaryEntityId, new ColumnSet(new string[] { "ticketnumber" }));
//Entity SourceCaseID1 = (Entity)service.Retrieve("incident", context.PrimaryEntityId, new ColumnSet(new string[] { "incidentid" }));
//QueryExpression query1 = new QueryExpression();
//tracingService.Trace("2");
//query1.EntityName = "new_productset";
//query1.ColumnSet =new ColumnSet("new_ProductNames", "new_Quantity") ;
//Guid caseguid = entity.Id;
//tracingService.Trace(SourceCaseID.ToString());
//tracingService.Trace(SourceCaseID1.ToString());
//tracingService.Trace(caseguid.ToString());
//query1.Criteria.AddCondition("new_associatedcase", ConditionOperator.Equal, caseguid); //replace parentcustomerid with parent entity name and guidoftheparentaccount with guid of the customer
//tracingService.Trace("2.3");
// string fetch2 = @"
// <fetch mapping='logical'>
// <entity name='new_productset'>
// <attribute name='accountid'/>
// <attribute name='name'/>
// <link-entity name='systemuser' to='owninguser'>
// <filter type='and'>
// <condition attribute='lastname' operator='ne' value='Cannon' />
// </filter>
// </link-entity>
// </entity>
// </fetch> ";
//tracingService.Trace(entity.LogicalName.ToString());
tracingService.Trace("entry");
tracingService.Trace("entry2");
if (objContext.Attributes.Contains("title"))
{
tracingService.Trace(Convert.ToString(objContext.Attributes["title"]));
}
else
{
throw new InvalidPluginExecutionException("no title");
tracingService.Trace("@1");
}
Guid caseorderId = objContext.Id;
tracingService.Trace("@2");
tracingService.Trace(objContext.Id.ToString());
tracingService.Trace(caseorderId.ToString());
tracingService.Trace("@3");
string fetch3 = @"
<fetch mapping='logical'>
<entity name='new_productset'>
<attribute name='new_productsetid' />
<attribute name='new_name' />
<attribute name='createdon' />
<attribute name='new_productnames' />
<attribute name='new_quantity' />
<filter type='and'>
<condition attribute='new_associatedcase' operator='eq' value='" + caseorderId.ToString() + @"'/></filter>
</entity>
</fetch>";
tracingService.Trace(fetch3.ToString());
try
{
EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetch3));
}
catch (Exception vb)
{
tracingService.Trace(vb.Message);
}
EntityCollection result2 = service.RetrieveMultiple(new FetchExpression(fetch3));
// EntityCollection response;
//var response = service.RetrieveMultiple(query1);
StringBuilder builder = new StringBuilder();
//tracingService.Trace("3");
int count = 0;
//if (response != null && response.Entities.Count > 0)
//{
// tracingService.Trace("4");
if (result2.TotalRecordCount != 0)
{
foreach (var subgridrecord in result2.Entities)
{
tracingService.Trace("5");
++count;
builder.Append(Convert.ToString(count) + ". " + " Product Name" + ": " + Convert.ToString(subgridrecord.FormattedValues["new_productnames"]) + " " + "Product Quantity: " + Convert.ToString(subgridrecord.Attributes["new_quantity"]));
}
}
count = 0;
//}
//else
//{
// tracingService.Trace("6");
// count = 0;
// builder.Append("*End");
//}
tracingService.Trace("7");
// Set the required attributes. For account, only the name is required.
// See the Entity Metadata topic in the SDK documentatio to determine
// which attributes must be set for each entity.
tracingService.Trace("8");
tracingService.Trace(builder.ToString());
objContext.Attributes.Add("new_caseproductset", builder.ToString());
service.Update(objContext);
tracingService.Trace("9");
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in the FollowupPlugin plug-in.", ex);
}
catch (Exception ex)
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
throw;
}
}
}
}