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”, “ 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.

Thanks so much for the post… I’ve been struggling for a few hours to get exactly this working.
LikeLike