Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Update field of Parent Entity from Child Entity

Posted on by Microsoft Employee

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);

}}}

}

}


 

*This post is locked for comments

  • Suggested answer
    M.Azwar Alam Profile Picture
    M.Azwar Alam on at
    RE: Update field of Parent Entity from Child Entity

    You can update parent record through child record by calling CRM SDK from JavaScript

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update field of Parent Entity from Child Entity

    Yes but can i update the value of a parent record through a child record using javascript?

    Meaning , can i update the Site of User based on the Site selected in a Service Activity?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update field of Parent Entity from Child Entity

    I want to trigger this action on changing the value of a field before clicking save

    Workflow will only run on Save

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans