Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Answered

Set lookup from another lookup in current form not working

Posted on by 5

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);
            }
        }
    }
}

  • dg17 Profile Picture
    dg17 5 on at
    RE: Set lookup from another lookup in current form not working

    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!

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Set lookup from another lookup in current form not working

    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans