Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Activities owner changing

Posted on by 155

I have two BU's - A,B where A is the parent and B is the child BU.

My contact records are owned by a user belonging to parent BU 'A' and the activities are owned by user from child BU 'B'.

I have a lookup to account entity on contact form and a plugin(synchronous) that updates the address fields on contact form whenever there is an update to address fields on parent account.

The issue is when i'm updating address on account, it is updating all the child contact records as expected but also changing the contact activities owner to the parent contact owner.

I'm not using any owner fields in my plugin code but seeing this weird issue. 

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Activities owner changing

    Hi Itz_Me_Ram,

    If you solved the problem or received help from an answer, please mark it as verified to encourage the community to provide more and more a better support.

    Thank you.

    Francesco

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Activities owner changing

    Hi Itz_Me_Ram,

    the error seems clear: you update the whole entity instead of updating only needed attributes.

    Doing so you do a "reparent" and probably system reassign all related entities.

    Modify your code as follows:

    foreach (var c in res)
    {
       Entity ec = (Entity)c;
    
       Entity e = new Entity(ec.LogicalName);
       e.Id = ec.Id;
    
       if (oldStreet1 != newStreet1)
    
    	   e["address1_line1"] = newStreet1;
    
       if (oldStreet2 != newStreet2)
    
    	   e["address1_line2"] = newStreet2;
    
       if (oldCity != newCity)
    
    	   e["address1_city"] = newCity;
    
       if (oldZip != newZip)
    
    	   e["address1_postalcode"] = newZip;
    
       if (oldStateorProvince != newStateorProvince)
    
    	   e["address1_stateorprovince"] = newStateorProvince;
    
       if (oldCountry != newCountry)
    
    	   e["address1_country"] = newCountry;
    
       //ServiceContext.Attach(e);
    
       ServiceContext.UpdateObject(e);
    
    }

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Itz_Me_Ram Profile Picture
    Itz_Me_Ram 155 on at
    RE: Activities owner changing

    I verified and there is no workflow.

    Here is my code :

    public class PostAccountUpdateContacts : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               // Obtain the execution context from the service provider.

               IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               // Get a reference to the Organization service.

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

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

               //create the service context

               var ServiceContext = new OrganizationServiceContext(service);

               string oldStreet1 = "";

               string newStreet1 = "";

               string oldStreet2 = "";

               string newStreet2 = "";

               string oldCity = "";

               string newCity = "";

               string oldStateorProvince = "";

               string newStateorProvince = "";

               string oldZip = "";

               string newZip = "";

               string oldCountry = "";

               string newCountry = "";

               // The InputParameters collection contains all the data passed in the message request.

               if (context.InputParameters.Contains("Target") &&

               context.InputParameters["Target"] is Entity)

               {

                   // Obtain the target entity from the input parmameters.

                   Entity entity = (Entity)context.InputParameters["Target"];

                   Entity preImageEntity = (Entity)context.PreEntityImages["preImageAlias"];

                   Entity postImageEntity = (Entity)context.PostEntityImages["postImageAlias"];

                   if (preImageEntity.Attributes.Contains("address1_line1"))

                   {

                       oldStreet1 = (string)preImageEntity.Attributes["address1_line1"];

                   }

                   if (postImageEntity.Attributes.Contains("address1_line1"))

                   {

                       newStreet1 = (string)postImageEntity.Attributes["address1_line1"];

                   }

                   if (preImageEntity.Attributes.Contains("address1_line2"))

                   {

                       oldStreet2 = (string)preImageEntity.Attributes["address1_line2"];

                   }

                   if (postImageEntity.Attributes.Contains("address1_line2"))

                   {

                       newStreet2 = (string)postImageEntity.Attributes["address1_line2"];

                   }

                   if (preImageEntity.Attributes.Contains("address1_city"))

                   {

                       oldCity = (string)preImageEntity.Attributes["address1_city"];

                   }

                   if (postImageEntity.Attributes.Contains("address1_city"))

                   {

                       newCity = (string)postImageEntity.Attributes["address1_city"];

                   }

                   if (preImageEntity.Attributes.Contains("address1_stateorprovince"))

                   {

                       oldStateorProvince = (string)preImageEntity.Attributes["address1_stateorprovince"];

                   }

                   if (postImageEntity.Attributes.Contains("address1_stateorprovince"))

                   {

                       newStateorProvince = (string)postImageEntity.Attributes["address1_stateorprovince"];

                   }

                   if (preImageEntity.Attributes.Contains("address1_postalcode"))

                   {

                       oldZip = (string)preImageEntity.Attributes["address1_postalcode"];

                   }

                   if (postImageEntity.Attributes.Contains("address1_postalcode"))

                   {

                       newZip = (string)postImageEntity.Attributes["address1_postalcode"];

                   }

                   if (preImageEntity.Attributes.Contains("address1_country"))

                   {

                       oldCountry = (string)preImageEntity.Attributes["address1_country"];

                   }

                   if (postImageEntity.Attributes.Contains("address1_country"))

                   {

                       newCountry = (string)postImageEntity.Attributes["address1_country"];

                   }

                   if (oldStreet1 != newStreet1 || oldStreet2 != newStreet2 || oldCity != newCity || oldZip != newZip || oldStateorProvince != newStateorProvince || oldCountry != newCountry)

                   {

                       try

                       {

                           //Create query to get the related contacts

                           var res = from c in ServiceContext.CreateQuery("contact")

                                     where c["ch_direct"].Equals(entity.Id)

                                     select c;

                           foreach (var c in res)

                           {

                               Entity e = (Entity)c;

                               if (oldStreet1 != newStreet1)

                                   e["address1_line1"] = newStreet1;

                               if (oldStreet2 != newStreet2)

                                   e["address1_line2"] = newStreet2;

                               if (oldCity != newCity)

                                   e["address1_city"] = newCity;

                               if (oldZip != newZip)

                                   e["address1_postalcode"] = newZip;

                               if (oldStateorProvince != newStateorProvince)

                                   e["address1_stateorprovince"] = newStateorProvince;

                               if (oldCountry != newCountry)

                                   e["address1_country"] = newCountry;

                               //ServiceContext.Attach(e);

                               ServiceContext.UpdateObject(e);

                           }

                           ServiceContext.SaveChanges();

                       }

                       catch (FaultException<OrganizationServiceFault> ex)

                       {

                           throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);

                       }

                   }

               }

           }

       }

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Activities owner changing

    Hi Itz_Me_Ram,

    please share your plugin code.

    Please also check there isn't any workflow that fire on the same event.

    Looking forward to hearing from you.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans