Updating value in CRM entity in Callout written for Update event


 

Well we had a requriement in our case where we wanted to update a particular attribute value in our Crm’s opportunity entity in our update callout.

While looking for the same I came to know that if we are updating any attribute’s value in the entity against which we have attached our update callout, we will be lost in infinite loop.

Only way to do this is in the PreUpdate and there is a sample code for the same in sdk as well.(Unsupported)

public override PreCalloutReturnValue PreUpdate(CalloutUserContext userContext,CalloutEntityContext entityContext, ref string entityXml,ref string errorMessage)

Here we can see that the entityXml is passed to us as a reference. So any modification made to the entityXml would reflect back in that entity’s form as well.

Suppose this is the entityXml which we receive in our PreUpdate event

<?xml version=1.0 encoding=utf-16?>

<BusinessEntity xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema xsi:type=DynamicEntity Name=opportunity xmlns=http://schemas.microsoft.com/crm/2006/WebServices>

<Properties>

<Property xsi:type=KeyProperty Name=opportunityid>

<Value>57330ec3-6f5d-dd11-acd2-00164145e126</Value>

</Property>

<Property xsi:type=CrmFloatProperty Name=new_estimatedvalue>

<Value>1</Value>

</Property>

<Property xsi:type=CrmMoneyProperty Name=estimatedvalue>

<Value>77.5</Value>

</Property>

</Properties>

</BusinessEntity>

Now after running this code in our preUpdate event handler we can add a new property tag itself, which would be refering to the attribute we would like to update

// creating an xmlDocument

XmlDocument entityDoc = new XmlDocument();

// loading the entityXml in it

entityDoc.LoadXml(entityXml);

// the attribute which we would like to update

XmlElement totatForecastedValue = null;

// get the list of properties

XmlNodeList propertyList = entityDoc.GetElementsByTagName(“Property”);

XmlElement properties = (XmlElement)entityDoc.GetElementsByTagName(“Properties”)[0];

// creating an element with property tag

XmlElement totalForecastedElement = entityDoc.CreateElement(“Property”);

// creating an attribute type for the property tag

XmlAttribute typeAttrib = entityDoc.CreateAttribute(“type”);

totalForecastedElement.SetAttribute(“type”, http://www.w3.org/2001/XMLSchema-instance&#8221;, CrmFloatProperty”);

totalForecastedElement.SetAttribute(“Name”, “new_totalbilling”);

totatForecastedValue = entityDoc.CreateElement(“Value”);

// setting the value for the attribute

totatForecastedValue.InnerText = “10”;

totalForecastedElement.AppendChild(totatForecastedValue);

properties.AppendChild(totalForecastedElement);

 

//saving the output

StringWriter output = new StringWriter();

entityDoc.Save(output);

 

// assigning the same output to the entityXml

entityXml = output.ToString();

 

// Remove extra XML that will confuse CRM.

entityXml = entityXml.Replace(“xmlns=\”\””, “”);

entityXml = entityXml.Replace(“<?xml version=\”1.0\” encoding=\”utf-16\”?>”, “”);

The entityXml gets modified with the above code in this manner

<BusinessEntity xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema xsi:type=DynamicEntity Name=opportunity xmlns=http://schemas.microsoft.com/crm/2006/WebServices>

<Properties>

<Property xsi:type=KeyProperty Name=opportunityid>

<Value>57330ec3-6f5d-dd11-acd2-00164145e126</Value>

</Property>

<Property xsi:type=CrmFloatProperty Name=new_estimatedvalue>

<Value>1</Value>

</Property>

<Property xsi:type=CrmMoneyProperty Name=estimatedvalue>

<Value>77.5</Value>

</Property>

<Property xsi:type=CrmFloatProperty Name=new_totalbilling >

<Value>10</Value>

</Property>

</Properties>

</BusinessEntity>

 

And this way our entity gets updated as well.

And the same code in CRM 3.0 callout  works properly in case of upgrading the CRM 3.0 server to CRM 4.0 .

 

Bye..

 

 


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

One thought on “Updating value in CRM entity in Callout written for Update event”

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.