locked
CRM 2011 - Plugin adding related entities RRS feed

  • Question

  • I'm trying to add some related entities to an entity on create/update in the pre-execution stage. Its my understanding that if I add the entities to the RelatedEntities property of the Target entity then it should get saved along with the rest of the attributes. Am I incorrect in assuming this? Here's the code I'm using to add the related objects:

    var items = new EntityCollection(new List<Entity>())
    {
    	EntityName = pareto_organizationpackage.EntityLogicalName
    };
    foreach (var package in packages)
    {
    	var p = package;
    	TracingManager.Trace("Package: {0}"package.pareto_name);
     
    	//Entry already exists so don't create it
    	if (osc.pareto_organizationpackageSet.Where(
    			op => op.pareto_PackageId != null
    				&& op.pareto_PackageId.Id == p.Id
    				&& op.pareto_OrganizationId != null
    				&& op.pareto_OrganizationId.Id == organization.Id
    			).ToList().Any())
    		continue;
     
    	TracingManager.Trace("Package ID:      {0}"package.Id);
    	TracingManager.Trace("Organization ID: {0}", organization.Id);
     
    	var orgpack = new Entity("pareto_organizationpackage");
    	orgpack["pareto_name"] = organization.pareto_uniquename;
    	orgpack["pareto_organizationid"] = new EntityReference
    		{
    			Id = organization.Id,
    			LogicalName = organization.LogicalName
    		};
    	orgpack["pareto_packageid"] = new EntityReference
    		{
    			Id = package.Id,
    			LogicalName = package.LogicalName
    		};
    	items.Entities.Add(orgpack);
    }
    var orgpackrelationship = new Relationship("pareto_organization_pareto_organizationpa"); //This is not a typo
    organization.RelatedEntities[orgpackrelationship] = items;

    When the execution of the plugin completes the associated entities are not there. Any suggestions would be greatly appreciated


    Thank you in advance for your help. If you think you may be able to help with any of my unanswered threads please look at them here
    Monday, July 4, 2011 6:06 PM

Answers

  • Got it! Ended up being able to switch to a post operation (the object will be available in the DB at that point) so I can just use the context

     

    using (var osc = new OrganizationServiceContext(organizationService))
    {
    	osc.Attach(organization);
    	var orgpackrelationship = new Relationship("pareto_organization_pareto_organizationpa");
    	foreach (var package in packages)
    	{
    		var p = package;
    		TracingManager.Trace("Package: {0}"package.pareto_name);
    		//Entry already exists so don't create it
    		if (osc.pareto_organizationpackageSet.Where(
    				op => op.pareto_PackageId != null
    					&& op.pareto_PackageId.Id == p.Id
    					&& op.pareto_OrganizationId != null
    					&& op.pareto_OrganizationId.Id == organization.Id
    				).ToList().Any())
    			continue;
     
    		TracingManager.Trace("Package ID:      {0}"package.Id);
    		TracingManager.Trace("Organization ID: {0}", organization.Id);
     
    		var orgpack = new Entity("pareto_organizationpackage");
    		orgpack["pareto_name"] = organization.pareto_uniquename;
    		orgpack["pareto_packageid"] = package.ToEntityReference();
    		osc.AddRelatedObject(organization, orgpackrelationship, orgpack);
    	}
    	osc.SaveChanges();
    	osc.Detach(organization);
    }


    Thank you in advance for your help. If you think you may be able to help with any of my unanswered threads please look at them here
    Tuesday, July 5, 2011 6:14 PM

All replies

  • Got it! Ended up being able to switch to a post operation (the object will be available in the DB at that point) so I can just use the context

     

    using (var osc = new OrganizationServiceContext(organizationService))
    {
    	osc.Attach(organization);
    	var orgpackrelationship = new Relationship("pareto_organization_pareto_organizationpa");
    	foreach (var package in packages)
    	{
    		var p = package;
    		TracingManager.Trace("Package: {0}"package.pareto_name);
    		//Entry already exists so don't create it
    		if (osc.pareto_organizationpackageSet.Where(
    				op => op.pareto_PackageId != null
    					&& op.pareto_PackageId.Id == p.Id
    					&& op.pareto_OrganizationId != null
    					&& op.pareto_OrganizationId.Id == organization.Id
    				).ToList().Any())
    			continue;
     
    		TracingManager.Trace("Package ID:      {0}"package.Id);
    		TracingManager.Trace("Organization ID: {0}", organization.Id);
     
    		var orgpack = new Entity("pareto_organizationpackage");
    		orgpack["pareto_name"] = organization.pareto_uniquename;
    		orgpack["pareto_packageid"] = package.ToEntityReference();
    		osc.AddRelatedObject(organization, orgpackrelationship, orgpack);
    	}
    	osc.SaveChanges();
    	osc.Detach(organization);
    }


    Thank you in advance for your help. If you think you may be able to help with any of my unanswered threads please look at them here
    Tuesday, July 5, 2011 6:14 PM
  • Although I'd still like to find out how to do this as a pre-operation plugin!
    Thank you in advance for your help. If you think you may be able to help with any of my unanswered threads please look at them here
    Tuesday, July 5, 2011 7:04 PM