Entity of CRM rapid development

Source: Internet
Author: User

When we operate entity in the background code, it is basically written as follows:

/* Creator: the blog of caidao jushi
* Creation date: January 1, July 5, 2014
*/

Namespace net. CRM. Entity
{
Using system;
Using Microsoft. xrm. SDK;

/// <Summary>
/// Basic mode --- entity
/// </Summary>
Public class entitydemo
{
Public void run (entity)
{
If (isnotnull (entity, "new_int "))
{
// Obtain the value of an int Field
Int new_int = entity. getattributevalue <int> ("new_int ");
}
If (isnotnull (entity, "new_string "))
{
// Obtain the value of a string field
String new_string = entity. getattributevalue <string> ("new_string ");
}
If (isnotnull (entity, "new_float "))
{
// Obtain the value of a float Field
Float new_float = entity. getattributevalue <float> ("new_float ");
}
If (isnotnull (entity, "new_money "))
{
// Obtain the value of the money Field
Decimal new_money = entity. getattributevalue <money> ("new_money"). value;
}
If (isnotnull (entity, "new_picklist "))
{
// Obtain the value of a field of the optionsetvalue type (drop-down box ).
Int new_picklist = entity. getattributevalue <optionsetvalue> ("new_picklist"). value;
}
If (isnotnull (entity, "new_lookup "))
{
// Obtain the value of an entityreference field.
Entityreference new_lookup = entity. getattributevalue <entityreference> ("new_lookup ");
}
}

/// <Summary>
/// Determine whether a field of the object is null
/// </Summary>
/// <Param name = "entity"> entity </param>
/// <Param name = "name"> Field name </param>
Public bool isnotnull (entity, string name)
{
Return entity. Contains (name) & entity. attributes [name]! = NULL;
}
}
}

 

It is correct to write in this way, but it is very complicated. The following is a quick writing method:

/* Creator: the blog of caidao jushi
* Creation date: January 1, July 5, 2014
*/

Namespace net. CRM. Entity
{
Using system;
Using Microsoft. xrm. SDK;

/// <Summary>
/// Quick development --- entity
/// </Summary>
Public class entityquickdemo
{
Public void run (entity)
{
If (entity. isnotnull ("new_int "))
{
// Obtain the value of an int Field
Int new_int = entity. toint ("new_int ");
}
If (entity. isnotnull ("new_string "))
{
// Obtain the value of a string field
String new_string = entity. tostring ("new_string ");
}
If (entity. isnotnull ("new_float "))
{
// Obtain the value of a float Field
Float new_float = entity. tofloat ("new_float ");
}
If (entity. isnotnull ("new_money "))
{
// Obtain the value of the money Field
Decimal new_money = entity. tomoney ("new_money ");
}
If (entity. isnotnull ("new_picklist "))
{
// Obtain the value of a field of the optionsetvalue type (drop-down box ).
Int new_picklist = entity. toopint ("new_picklist ");
}
If (entity. isnotnull ("new_lookup "))
{
// Obtain the value of an entityreference field.
Entityreference new_lookup = entity. toer ("new_lookup ");
}
}
}

/// <Summary>
/// Extension Method
/// </Summary>
Public static class entityfunction
{
/// <Summary>
/// Int
/// </Summary>
Public static int toint (this entity, string name)
{
Return entity. getattributevalue <int> (name );
}

/// <Summary>
/// String
/// </Summary>
Public static string tostring (this entity, string name)
{
Return entity. getattributevalue <string> (name );
}

/// <Summary>
/// Float
/// </Summary>
Public static float tofloat (this entity, string name)
{
Return entity. getattributevalue <float> (name );
}

/// <Summary>
/// Money
/// </Summary>
Public static decimal tomoney (this entity, string name)
{
Return entity. getattributevalue <money> (name). value;
}

/// <Summary>
/// Optionsetvalue
/// </Summary>
Public static int toopint (this entity, string name)
{
Return entity. getattributevalue <optionsetvalue> (name). value;
}

/// <Summary>
/// Entityreference
/// </Summary>
Public static entityreference toer (this entity, string name)
{
Return entity. getattributevalue <entityreference> (name );
}


Public static t getattributevalue <t> (this entity, string name)
{
If (entity. isnotnull (name ))
{
Return entity. getattributevalue <t> (name );
}
Return default (t );
}

/// <Summary>
/// Determine whether a field of the object is null
/// </Summary>
/// <Param name = "entity"> entity </param>
/// <Param name = "name"> Field name </param>
Public static bool isnotnull (this entity, string name)
{
Return entity. Contains (name) & entity. attributes [name]! = NULL;
}
}
}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.