Hello Pravin,
I have developed the custom code. but it's again not updating the field value.
could you please help me ? It's not giving any error
Below code-
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SetClassId
{
public class SetClassIdToAssociatedOpp : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// 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);
ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
EntityReference target = null;
EntityReference initialTargetEntity = null;
EntityReferenceCollection relatedEntities;
string relationshipName;
EntityReference initialRelatedEntity;
if ((context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference) && (context.InputParameters.Contains("RelatedEntities") && context.InputParameters["RelatedEntities"] is EntityReferenceCollection))
{
initialTargetEntity = (EntityReference)context.InputParameters["Target"];
relatedEntities = context.InputParameters["RelatedEntities"] as EntityReferenceCollection;
relationshipName = context.InputParameters["Relationship"].ToString();
initialRelatedEntity = relatedEntities[0];
//throw new Exception("" + initialTargetEntity.LogicalName + "::" + initialTargetEntity.Id);
}
// if (context.Depth < 1)
// return;
string fetch = @"<fetch distinct = 'true' mapping = 'logical'> " +
"<entity name='hnny_class'>" +
"<attribute name='hnny_classid'/>" +
"<attribute name='hnny_name'/>" +
"<attribute name='createdon'/>" +
"<order descending='false' attribute='hnny_name'/>" +
"<link-entity name='hnny_class_opportunity' intersect='true' visible='false' to='hnny_classid' from='hnny_classid'>" +
"<link-entity name='opportunity' to='opportunityid' from='opportunityid' alias='ab'>" +
"<filter type='and'>" +
"<condition attribute = 'opportunityid' operator= 'eq' value = '" + initialTargetEntity.Id + "' />" +
"</filter>" +
"</link-entity>" +
"</link-entity>" +
"</entity>" +
"</fetch>";
FetchExpression exep = new FetchExpression(fetch);
EntityCollection collection = service.RetrieveMultiple(exep);
if (collection.TotalRecordCount > 1)
{
string classNames = "";
foreach (Entity classes in collection.Entities)
{
if (classNames == "")
classNames = classes.Attributes.Contains("hnny_name") ? (String)classes["hnny_name"] : string.Empty;
else
classNames += classes.Attributes.Contains("hnny_name") ? (String)classes["hnny_name"] : string.Empty;
}
Entity opp = new Entity(initialTargetEntity.LogicalName, initialTargetEntity.Id);
opp["hnny_classes"] = classNames; // use classfiled logcal name
service.Update(opp);
}
}
}
}