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

Update field of Parent Entity from Child Entity

$
0
0

Hi everyone!

How can i do this? I want to update the User's Site value (which is a lookup field) , from the Service Activity form's Site Value.

So i choose a Resource and a Site  from Service Activity -> update the Site of the User in Resource. This should happen before clicking save

I found this code . Is it sufficient ?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using Microsoft.Crm.Sdk;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Sdk;

using Microsoft.Crm.Sdk.Messages;

namespace UpdateAssociatedRecord

{

public class Update : IPlugin

{

//Message: Update

//Entity: Account

//Event: Post-Operation, Post-Image

public void Execute(IServiceProvider serviceProvider)

{

IPluginExecutionContext context = (IPluginExecutionContext)

serviceProvider.GetService(typeof(IPluginExecutionContext));

if (context.PostEntityImages.Contains(“Target”))

{

Entity postMessageImage = (Entity)context.PostEntityImages[“Target”];

if (postMessageImage.Attributes.Contains(“name”) == true)

{

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

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

EntityReference customer = postMessageImage.GetAttributeValue<EntityReference>(“primarycontactid”);

Entity contact = service.Retrieve(customer.LogicalName, customer.Id,new ColumnSet(true));

Contact.Attributes[“email”] = postMessageImage.GetAttributeValue<string>(“email”);  //String Field

Contact.Attributes[“address1_addresstypecode”] = postMessageImage.GetAttributeValue<OptionSetValue>(” address1_addresstypecode”); //Optionset Field

Contact.Attributes[“lastusedincampaign”] = postMessageImage.GetAttributeValue<OptionSetValue>(“lastusedincampaign”); //Datetime Field

Contact.Attributes[“originatingleadid”] = postMessageImage.GetAttributeValue<EntityReference>(” originatingleadid”); //Lookup Field

service.Update(postMessageImage);

}}}

}

}


 


Viewing all articles
Browse latest Browse all 79901

Trending Articles