web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

how to retrieve account name from contact lookup using plugin

(0) ShareShare
ReportReport
Posted on by 20

Hi

On create of a case record,i want to retrieve contact lookup value from case form  and get the Account name associated with that contact.

then set that account name to a custom company_lookup field using Plugin only.

1781.1376.Capture.PNG 

Please help!!

Thank You!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at

    Hi,

    You need to write pre-create plugin, and you can get contact id from plugin input parameters..

    then using organization service you can retrieve company lookup and set that value to in plugin context.

  • Tanvi Profile Picture
    20 on at

    Hi Mahadeo,

    Thanks for your response!

    Actually I am new to plugins. Could you please give me some reference ?

    As per my Requirement,Case is  automatically getting created from an  Email activity.And i need to set company name of contact(sender) on case form using Plugin.

     So I am registering the plugin for 'Case' entity,on Create message.

    After getting the contact lookup field Guid from case form, how can i retrieve company lookup of that contact in my Plugin.

    Any Example?

  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at
  • Tanvi Profile Picture
    20 on at

    After getting the contact lookup field Guid from case form, How can i retrieve company lookup of that contact in my Plugin.????

    How can I do that?

  • Verified answer
    Mahadeo Matre Profile Picture
    17,021 on at

    public void Execute(IServiceProvider serviceProvider)

       {

           try

           {

               IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               IOrganizationService service = factory.CreateOrganizationService(context.UserId);

               IOrganizationService systemService = factory.CreateOrganizationService(null);

               if (context.Depth > 1)

                   return;

               if (context.InputParameters.Contains("contactid"))

               {

                   Guid contactid = new Guid(context.InputParameters["contactid"].ToString());

                   ColumnSet cols = new ColumnSet(new string[] { "accountid" });

                   //retrieve contact record to get account.  

                   Entity ocontact = (Entity)service.Retrieve("contact", contactid, cols);

                   EntityReference refAccount = (EntityReference)ocontact["accountid"];

                   context.OutputParameters["accountid"] = refAccount;

               }

           }

           catch (Exception ex)

           {

               throw new InvalidPluginExecutionException("Plugin Error: " + ex.Message);

           }

       }

  • Tanvi Profile Picture
    20 on at

    Thank you sir :)

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans