Nachdem ein neues Buch erstellt wurde, möchte ich, dass der Preis des Buchs auf der Serverseite über ein Plugin um eine GST von 10 % erhöht wird.
Ich weiß, dass dies normalerweise auf der Seite vor dem Speichern passieren würde, indem ich versuche herauszufinden, wie die serverseitige Logik funktioniert.
Ich habe einen postOperation-Schritt (synchron) für die „Create“-Nachricht erstellt, um das Plugin aufzurufen Execute()-Methode. Meiner Lektüre nach sollte dies geschehen, NACHDEM der Datensatz in der Datenbank gespeichert wurde.
Ich habe auch eine Post-Image-Entität, auf die ich zugreife.
In der Execute-Methode versuche ich, über die PostMessageEntity auf den gespeicherten Datensatz zuzugreifen, um den Preis zu aktualisieren, aber ich erhalte eine Ausnahme, die besagt, dass der Datensatz nicht vorhanden ist, basierend auf der Datensatz-ID, die ich erhalten habe. Ich kann bestätigen, dass der Datensatz nie im System erstellt wurde, die postOperation jedoch aufgerufen wurde.
Wie greife ich auf den gerade gespeicherten Datensatz im Plugin zu, damit ich den Preis aktualisieren kann?
Mein Code:
Code: Select all
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
// create a trace log so you can see where in the code it breaks
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// create access to service
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
tracingService.Trace("have reached execute event in plugin.");
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
tracingService.Trace("We have a target and it is an entity.");
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "new_books")
{
tracingService.Trace("the entity id of the record that was created is .." + entity.Attributes["new_booksid"].ToString());
// do we have a post update image of the new_books entity
if (context.PostEntityImages.Contains("newbookpostImage") && context.PostEntityImages["newbookpostImage"] is Entity)
{
tracingService.Trace("we have a postEntityImage.");
// // yep lets grab it.
Entity postMessageEntity = (Entity)context.PostEntityImages["newbookpostImage"];
// get book price as just saved to db
decimal bookPrice = ((Money)postMessageEntity.Attributes["new_price"]).Value;
// get id of the the record we have
Guid RecordID = ((Guid)postMessageEntity.Attributes["new_booksid"]);
tracingService.Trace("we have a post update bookprice.");
tracingService.Trace("the entity id of the post image entity is ..." + postMessageEntity.Attributes["new_booksid"].ToString());
Entity created_book = new Entity("new_books");
// use service to access a field of the current record as it is in the database and column we want to update.
created_book = service.Retrieve(created_book.LogicalName, RecordID, new ColumnSet(true));
//And the last line is where it dies and tells me new_books with id with d7bfc9e2 - 2257 - ec11 - 8f8f - 00224814e6e0 does not exist.
}
}
}
}
Mobile version