by Carmel Schvartzman
- This is the Building Block C# code to SAFELY COPY SOURCE VALUES TO TARGET FIELDS in a CRM 2013 C# Workflow. When using the late binding approach , you would probably be faced with web services errors related to types mismatch . Usually , you would need to waste much time looking for the specific field which caused the error . The goal of this tutorial is to avoid falling into that kind of problems when developing CRM plugins with the late binding approach .
- The present code relates to a custom Plugin / Workflow in Dynamics CRM 2011/2013. Once you have developed your custom workflow, you deploy the assembly containing the plugin in the CRM web server, and register it with the Plugin Registration Tool of the CRM 2011 SDK.
- After you set up the Xrm Organization Service that you need to interact with the CRM organization, you can use the following code to COPY values fetched through the CRM Web Service, which we call here "sourceEntity", to your local "targetEntity" . We include here 4 scenarios : an INT field, STRING, DECIMAL & DATETIME : each one of them will be copied SAFELY, meaning that because we are using late binding here, we will avoid causing web service exceptions :
SAFELY COPY SOURCE VALUES TO TARGET FIELDS IN PLUGIN WORKFLOW
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (sourceEntity.Attributes.ContainsKey("source_field"))
{
targetEntity["target_field"] = Convert.ToString(sourceEntity.Attributes["source_field"]);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// CASE #2 ) INT : /////////////////////////////////
if (sourceEntity.Attributes.ContainsKey("source_field") )
{
int iCode = 0;
if (Int32.TryParse(Convert.ToString(sourceEntity.Attributes["source_field"] ), out iCode))
{
targetEntity.Attributes["target_field"] = iCode;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
///////////////// CASE # 3 ) MONEY - DECIMAL : //////////////////////
if (sourceEntity.Attributes.ContainsKey("source_field") )
{
decimal mValue = 0.0M;
if ( Decimal.TryParse(Convert.ToString(sourceEntity.Attributes["source_field"] ), out mValue ))
{
targetEntity.Attributes["target_field"] = mValue;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
////////// CASE #4 ) DATETIME : /////////////////////////////////
if (sourceEntity.Attributes.ContainsKey("source_field") )
{
decimal mValue = 0.0M;
if ( Decimal.TryParse(Convert.ToString(sourceEntity.Attributes["source_field"]), out mValue ))
{
targetEntity.Attributes["target_field"] = Convert.ToDateTime( sourceEntity.Attributes["source_field"]);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
The present article is about how to SAFELY COPY SOURCE VALUES TO TARGET FIELDS in a CRM 2013 C# Workflow.
That's all...Enjoy Dynamics CRM 2013!!!
כתב: כרמל שוורצמן
No comments:
Post a Comment