Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 79901

System.Collections.Generic.KeyNotFoundException err

$
0
0

Hi Communities,

I created simple plugin : Whenever account updated that primarycontact fields also updated.

but I encountered the below business process error.

Please give me best solution.

coding:

namespace updatecontact
{
public class UpdateContact : IPlugin
{

public void Execute(IServiceProvider serviceProvider)
{

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") & context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];

if (entity.LogicalName == "account")

try
{

EntityReference contactId = (EntityReference)entity.Attributes["primarycontactid"];
Guid primaryContactGUID = contactId.Id;

Entity contact = new Entity("contact");

// Update the postal code attribute.
contact["address1_city"] = "Texas";

// The address 2 postal code was set accidentally, so set it to null.
contact["emailaddress1"] = "john334@gmail.com


contact["contactid"] = primaryContactGUID;
// Update the account.
service.Update(contact);
}


catch (Exception ex)
{
tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}


Viewing all articles
Browse latest Browse all 79901

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>