Friday, August 7, 2009

Function to get value from dyanamic entity field

public static Guid GetKeyValue(Microsoft.Crm.Sdk.DynamicEntity de, string val)
{
foreach (KeyProperty ent in de.Properties)
{
if (ent.Name == val)
return ent.Value.Value;
}
return Guid.Empty;

}

Enjoy,
Rami Heleg

Function to get value from dyanmic entity

public static string GetValueFromProperty(object property)
{
switch (property.GetType().Name)
{
case "String":
return ((String)property);
case "Lookup":
return ((Microsoft.Crm.Sdk.Lookup)property).IsNull == true ? "" : ((Microsoft.Crm.Sdk.Lookup)property).Value.ToString();
case "Picklist":
return ((Microsoft.Crm.Sdk.Picklist)property).IsNull == true ? "" : ((Microsoft.Crm.Sdk.Picklist)property).name.ToString();
case "Customer":
return ((Microsoft.Crm.Sdk.Customer)property).IsNull == true ? "" : ((Microsoft.Crm.Sdk.Customer)property).name.ToString();
case "CrmNumber":
return ((Microsoft.Crm.Sdk.CrmNumber)property).IsNull == true ? "" : ((Microsoft.Crm.Sdk.CrmNumber)property).Value.ToString();
case "CrmFloat":
return ((Microsoft.Crm.Sdk.CrmFloat)property).IsNull == true ? "" : ((Microsoft.Crm.Sdk.CrmFloat)property).Value.ToString();
case "CrmMoney":
return ((Microsoft.Crm.Sdk.CrmMoney)property).IsNull == true ? "" : ((Microsoft.Crm.Sdk.CrmMoney)property).Value.ToString();
case "CrmBoolean":
return ((Microsoft.Crm.Sdk.CrmBoolean)property).IsNull == true ? "" : ((Microsoft.Crm.Sdk.CrmBoolean)property).Value ? "1" : "0";
case "CrmDateTime":
return ((Microsoft.Crm.Sdk.CrmDateTime)property).IsNull == true ? "" : ((Microsoft.Crm.Sdk.CrmDateTime)property).Value;
case "Status":
return ((Microsoft.Crm.Sdk.Status)property).IsNull == true ? "" : ((Microsoft.Crm.Sdk.Status)property).Value.ToString();
case "State":
return "0";
case "Key":
return ((Microsoft.Crm.Sdk.Key)property).Value.ToString();
case "Owner":
return ((Microsoft.Crm.Sdk.Owner)property).IsNull == true ? "" : ((Microsoft.Crm.Sdk.Owner)property).name.ToString();
}
return "";
}

Enjoy,
Rami Heleg

Function to create CrmCustomer

public static Customer GetCrmCustomer(string entityTypeName, Guid value) {
Customer rslt = new Customer();
if (value == Guid.Empty)
rslt.IsNull = rslt.IsNullSpecified = true;
else {
rslt.type = entityTypeName;
rslt.Value = value;
}
return rslt;
}
Enjoy,
Rami Heleg

Function to create CrmPickList

public static Picklist GetCrmPicklist(int value) {
Picklist rslt = new Picklist();
if (value == 0)
rslt.IsNull = rslt.IsNullSpecified = true;
else
rslt.Value = value;
return rslt;
}
Enjoy,
Rami Heleg

Function to create CrmOwner

public static Owner GetCrmOwner(Guid ownerID) {
Owner rslt = new Owner();
if (ownerID == Guid.Empty)
rslt.IsNull = rslt.IsNullSpecified = true;
else {
rslt.Value = ownerID;
rslt.type = "systemuser";
}
return rslt;
}
Enjoy,
Rami Heleg

Function to create CrmStatus

public static Status GetCrmStatus(int value) {
Status rslt = new Status();
rslt.Value = value;
return rslt;
}

Enjoy,
Rami Heleg.

Function to create CrmMoney

public static CrmMoney GetCrmMoney(decimal value) {
CrmMoney rslt = new CrmMoney();
rslt.Value = value;
return rslt;
}
Enjoy,
Rami Heleg

Function to create CrmDecimal

public static CrmDecimal GetCrmDecimal(decimal value, bool isNull) {
CrmDecimal rslt = new CrmDecimal();
if (isNull)
rslt.IsNull = rslt.IsNullSpecified = true;
else
rslt.Value = value;
return rslt;
}

Enjoy,
Rami Heleg

Function to create CrmFloat

public static CrmFloat GetCrmFloat(float value, bool isNull) {
CrmFloat rslt = new CrmFloat();
if (isNull)
rslt.IsNull = rslt.IsNullSpecified = true;
else
rslt.Value = value;
return rslt;
}

Enjoy,
Rami Heleg

Function to create CrmBoolean

public static CrmBoolean GetCrmBoolean(bool value) {
CrmBoolean rslt = new CrmBoolean();
rslt.Value = value;
return rslt;
}
Enjoy,
Rami Heleg

Function to create crmdatetime

public static CrmDateTime GetCrmDateTime(DateTime value) {
CrmDateTime rslt = new CrmDateTime();
rslt.Value = value.ToString("yyyy-MM-ddTHH:mm:ss");
return rslt;
}

Enjoy
Rami Heleg