Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Make a Plugin for Pre Create contact to fill in a lookup field based on another lookup field

(0) ShareShare
ReportReport
Posted on by 37

i have a Contact entity that has two lookup fields;

City lookup

country lookup

(a country has a few cities - what country to put is known based on what city it is)

country lookup should be filled based on the city lookup

how do i do that? 

using Microsoft.Xrm.Sdk;
using myOrg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PluginTest
{
    public class PreCreateStudent :IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            //obtain tracing service (logging)
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            //obtain execution context from service provider
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            //impersonation use
            IOrganizationService service = serviceFactory.CreateOrganizationService(null);

            Contact target = ((Entity)context.InputParameters["Target"]).ToEntity();

            target.opu_country = //here im supposed to fill this field based on opu_city lookup field
        }
    }
}

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at
    RE: Make a Plugin for Pre Create contact to fill in a lookup field based on another lookup field

    Hi,

    First you need to get city value from the contact and then need to query city and fill country, so the code should be something like below (Not tested)

    if(target.Contains("<citylookuponcontact>")

    {

    Entity cityEntity=service.Retrieve("<cityEntitylogicalname>",target.GetAttributeValue<EntityReference>("<citylookuponcontact>").Id,new Columnset("<countrylookuplogicalnameonCity>"));

    if(cityEntity.Contains("<countrylookuplogicalnameonCity>")

    {

    target.Attribute.Add("<Countrylookuplogicalnameoncontact>",cityEntity.GetAttributeValue<EntityReference>("<countrylookuplogicalnameonCity>"));

    }

    }

    You also have other options like you can use Javascript, or workflow/Power Automate (easy option no code low code :) )

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,302 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,108 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans