Announcements
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 } } }
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 :) )
André Arnaud de Cal...
293,302
Super User 2025 Season 1
Martin Dráb
232,108
Most Valuable Professional
nmaenpaa
101,156
Moderator