my current goal is to write a Dynamics plugin, which triggers on update in the opportunity form and gets the account(lookup-contact) of the current contact(lookup-opportunity) and sets it to the account(lookup-opportunity) in the opportunity form.
My problem right now is, that dynamics is not accepting my data, but I can't find the error.
The debugger says, that the entity contains valid data and I've also checked the GUID's for correct references (and the GUID's in the excel table exports).
The plugin triggers at PreValidation.
Maybe someone sees the error or can lead me to the correct way?
Edit: The main goal is to apply the company of the contact in the opportunity form automatically, if a company is linked to the contact.
Thank you!
using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using System; namespace GetAccountFromContact { public class GetAccountFromContact : IPlugin { public void Execute(IServiceProvider serviceProvider) { try { IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); if (context.Depth > 1) return; Entity entity = (Entity)context.InputParameters["Target"]; if (entity.Contains("parentcontactid")) { EntityReference pcontact = (EntityReference)entity.Attributes["parentcontactid"]; var pcontactid = pcontact.Id; ColumnSet cols = new ColumnSet(new string[] { "parentcustomerid" }); Entity paccount = (Entity)service.Retrieve("contact", pcontactid, cols); EntityReference paccountref = (EntityReference)paccount.Attributes["parentcustomerid"]; var lookupid = paccountref.Id; var logicalname = paccountref.LogicalName; entity["parentaccountid"] = new EntityReference(logicalname, lookupid); service.Update(entity); } } catch (Exception ex) { throw new InvalidPluginExecutionException("Plugin Error: " ex); } } } }
Hey Bipin,
thank you for your answer.
I've tried your solution and deleted the service update line and changed to pre-operation, but then i get the following error:
Unhandled Exception: System.ArgumentException: Unable to parse the OrganizationServiceFault. Parametername: serializedReport bei PluginProfiler.Library.ProfilerUtility.ExtractReport(String serializedReport) bei PluginProfiler.Library.ProfilerUtility.DeserializeProfilerReport(String assemblyFilePath, String logFilePath, Boolean isCrmDataStream) bei PluginProfiler.Library.ProfilerExecutionUtility.RetrieveReport(String logFilePath, Boolean isCrmDataStream) bei Microsoft.Crm.Tools.PluginRegistration.CommonControls.Helper.ParseReportOrShowError(Window window, FileBrowserView profilePathControl, Boolean requireReportParse, ProfilerPluginReport& report) Inner Exception: System.InvalidOperationException: File does not contain a valid serialized OrganizationServiceFault. bei PluginProfiler.Library.ProfilerUtility.ConvertFaultToStream(String serializedFault) bei PluginProfiler.Library.ProfilerUtility.ExtractReport(String serializedReport)
Edit: Found the solution, I had to deactivate the profiler after this error and now its working.
Thank you very much Bipin!
HI,
YOU DON'T NEED BELOW LINE OF CODE FROM YOUR ATTACHED CODE.
service.update(entity);
Remove this line from your code and check if it works.
You can also change your step pipeline to pre-operation from pre-validation
Thanks,
Bipin
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156