Hi,
I am trying to trigger a plugin on save of a record and use the newly created entity id to create a new lookup record(Document location). But I am seeing record Id does not exist error on this line:
iService.Create(spDocLoc);
Here is the snippet of my pluign:
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext iContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory iServFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService iService = iServFactory.CreateOrganizationService(iContext.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (iContext.InputParameters.Contains("Target") && iContext.InputParameters["Target"] is Entity && iContext.MessageName.ToLower() == "create")
{
Entity entAccount = (Entity)iContext.InputParameters["Target"];
if (entAccount.LogicalName == "new_document")
{
if (entAccount.Attributes.Contains("new_typeofdocument"))
{
EntityReference documenttypeId = (EntityReference)entAccount.Attributes["new_typeofdocument"];
Entity newdocumentType = new Entity("new_documenttype");
newdocumentType.Attributes = new AttributeCollection();
Entity documentTypeName = iService.Retrieve("new_documenttype", documenttypeId.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet("new_documenttype"));
typeofDocument = documentTypeName.Attributes["new_documenttype"].ToString();
}
}
string strURL = GetLocation(iService,typeofDocument,entAccount.Id);
}
}
private static string GetLocation(IOrganizationService iService, string FolderName, Guid entAccountID)
{
Guid _spParentLocId = new Guid("3F381014-5723-E611-80DF-0050569C2FED");
SharePointDocumentLocation spDocLoc = new SharePointDocumentLocation
{
Name = "Documents on Default Site 1",
Description = null,
RegardingObjectId = new EntityReference("new_document", entAccountID),
ParentSiteOrLocation = new EntityReference(SharePointDocumentLocation.EntityLogicalName, _spParentLocId),
RelativeUrl = FolderName
};
Guid _spDocLocId = iService.Create(spDocLoc);
// Console.WriteLine("{0} created.", spDocLoc.Name);
return _spDocLocId;
}
I've registered plugin on create of document entity and post operation(synchronous).
The record id does not get created in the database unless this plugin executes successfully. Since its throwing error, the record does not get saved. But I need to use the id to create document location immediately after saving document entity. How do I get all this working? What am I missing? Please guide me through this.
Thank you.
*This post is locked for comments
I have the same question (0)