Hi,
i am unable to update or set the "CityName" in City LookUpField in lead entity.
Ex:CityName: Bangalore once create a lead entity.
City (LookUp Field). I am unable to update this name in to City . in Custom Plugin
Plugin :
Message Name: Create
Target-entity : Lead
Post-Operation
synchronous operation
/// <param name="serviceProvider"></param>
public void Execute(IServiceProvider serviceProvider)
{
Entity targetLeadRecord = null;
string strCity = string.Empty;
List<string> AllCitiesName = new List<string>();
string strCityname = string.Empty;
//objtain the Context reference
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//Entity _StudentLead = new Entity("lead");
if (context.Depth > 1)
{
}
// Check if the input parameters property bag contains a target of the create operation and that target is of type Entity.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Verify that the entity represents a lead.
if (context.MessageName != MessageName.Create)
{
return;
}
else
{
try
{
//objtain the organization service reference
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
targetLeadRecord = (Entity)context.InputParameters["Target"];// Lead Entity as InPut Parametr
// To get The All City Names Based on City Field in Composit Address Field
//address1_composite
Guid LeadId = targetLeadRecord.Id;
Guid _CityId = Guid.Empty;
String strCityName = string.Empty;
if (LeadId == Guid.Empty || LeadId == null)
{
return;
}
// To Get The City Value from AddressCopmsoite Field
#region To Reterive The CityNames and Upadate address1_city
if (targetLeadRecord.Attributes.Contains("address1_city"))
{
//dr["City"] = leads.Attributes["address1_city"].ToString();
strCityname = targetLeadRecord.Attributes["address1_city"].ToString();
}
else
{
strCityname = "B1";
}
//Entity _lead = new Entity("lead");
//Entity _Citynames = new Entity("mage_citynames");
//_Citynames["mage_name"] = strCityname;
//service.Update(_Citynames);
#endregion
var fetchCityNamesXml =
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
"<entity name='mage_city'>" +
"<attribute name='mage_cityid'/>" +
"<attribute name='mage_name'/>" +
"<order attribute='mage_name' descending='false'/>" +
"<link-entity name='lead' from='mage_city' to='mage_cityid' alias='ae'>" +
"<filter type='and'>" +
"<condition attribute='address1_city' operator='eq' value='" + strCityname + "'/>" +
"</filter>" +
"</link-entity>" +
"</entity>" +
"</fetch>";
EntityCollection Address_City = service.RetrieveMultiple(new FetchExpression(fetchCityNamesXml));
if (Address_City != null)
{
string CityName = string.Empty;
foreach (Entity city in Address_City.Entities)
{
if (city.Contains("mage_name"))
{
CityName = city.Attributes["mage_name"].ToString();
if(targetLeadRecord.Attributes.Contains("mage_city"))
{
targetLeadRecord["mage_city"] = new EntityReference { LogicalName = city.LogicalName, Id = city.Id };// _CityLookup.Id;// city.Attributes["mage_name"];//new EntityReference { LogicalName = city.LogicalName, Id = city.Id };
service.Update(targetLeadRecord);
}
}
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}