locked
Plug In Not Working Properly RRS feed

  • Question

  • Hi all,

    I have installed a plug in for data auditing. Its Working on Account, Contact, Lead and Opportunity.
    But In Opportunity Its Not working when I create a new opportunity Its giving an error

    Web Service Plug-in failed in OrganizationId: 519e36fd-b57a-439f-9ba9-ddf1e4fcfc2e; SdkMessageProcessingStepId: 65de5dc5-1aa7-40a6-80e9-49b9618897ad; EntityName: opportunity; Stage: 50; MessageName: Create; AssemblyName: CRM.Auditing.Plugin.Create, CRM.Auditing.Plugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d04c6ac2be447b7f; ClassName: CRM.Auditing.Plugin.Create; Exception: Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.

    at CRM.Auditing.Plugin.Utilities.GetAttributeName(String sAttbName)

    at CRM.Auditing.Plugin.Utilities.AddAttributeToList(String value, String list)

    at CRM.Auditing.Plugin.Create.Execute(IPluginExecutionContext context)

    at Microsoft.Crm.Extensibility.PluginStep.Execute(PipelineExecutionContext context)

    .

    For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

    Pls let me know.


    G. Goyal
    • Moved by Mathias Schiffer Wednesday, June 3, 2009 8:49 AM English Language Post (From:Microsoft Dynamics CRM)
    • Moved by Donna EdwardsMVP Tuesday, June 30, 2009 4:12 PM (From:CRM)
    Wednesday, June 3, 2009 6:19 AM

Answers

  • Now its working .
    Some Date and Time related field that is CRM Field was creating some problem.
    G. Goyal
    • Marked as answer by G Goyal Thursday, September 24, 2009 6:27 AM
    Thursday, September 24, 2009 6:26 AM

All replies

  • Hi,

    the error message says it, your Index i outside the bounds of the array. Check your array and the code to fill it.


    Viele Grüße

    Michael Sulz
    axcentro GmbH
    • Proposed as answer by Michael Sulz Wednesday, June 3, 2009 6:26 AM
    Wednesday, June 3, 2009 6:26 AM
  • I have checked it. Its working fine with all entities but only when I create new opportunity It gives above mentions error.
    G. Goyal
    Wednesday, June 3, 2009 7:11 AM
  • Goyal, You will need to post the code to see what is causing the error for opportunity.

    H.

    Hassan Hussain | http://hassanhussain.wordpress.com/
    Wednesday, June 3, 2009 8:01 PM
  • This is code :



    public

    class Create : IPlugin

    {

     

    string m_config;

     

    string m_secureConfig;

     

    private string _relationshipAttribute = "";

     

    private string _primaryAttribute = "";

     

    public string Config

    {

     

    get { return m_config; }

     

    set { m_config = value; }

    }

     

    public string SecureConfig

    {

     

    get { return m_secureConfig; }

     

    set { m_secureConfig = value; }

    }

     

    public Create(string config, string secureConfig)

    {

    m_config = config;

    m_secureConfig = secureConfig;

     

    if (! String.IsNullOrEmpty( config))

    {

     

    string[] configSettings = config.Split(';');

     

    for (int i = 0; i < configSettings.Length; i++)

    {

     

    string[] values = configSettings[i].Split('=');

     

    switch (values[0].ToString().ToLower())

    {

     

    case "primaryattribute":

    _primaryAttribute = values[1].ToString().Trim().ToLower();

     

    break;

     

    case "relationshipattribute":

    _relationshipAttribute = values[1].ToString().Trim().ToLower();

     

    break;

    }

    }

    }

    }

     

    public void Execute(IPluginExecutionContext context)

    {

     

    try

    {

     

    string attributes = "";

     

    Audit audit = new Audit();

     

    DynamicEntity PreImage = new DynamicEntity();

     

    DynamicEntity PostImage = new DynamicEntity();

     

    audit.Service = context.CreateCrmService(

    true);

     

     

     

    Utilities.CRMService = context.CreateCrmService(true);

     

    Utilities.MetaService = context.CreateMetadataService(true);

     

    Utilities.sEntityName = context.PrimaryEntityName;

     

    if(String.IsNullOrEmpty(_relationshipAttribute))

    _relationshipAttribute =

    "new_" + context.PrimaryEntityName + "auditid";

     

    audit.EntityName = context.PrimaryEntityName;

     

    ///<summary>

     

    ///Changes on 01-06-2009

     

    ///Raising an error from Utilities.GetEntityName

     

    ///</summary>

    audit.EntityLabel = context.PrimaryEntityName;

    //Utilities.GetEntityName(context.PrimaryEntityName);

    audit.Type = context.MessageName;

    audit.OrgName = context.OrganizationName;

     

    //audit.Name = Utilities.GetEntityName(context.PrimaryEntityName) + " : " + context.MessageName + " By : " + Utilities.GetUserName(context.InitiatingUserId, audit.Service);

    audit.Name = GetName( audit.EntityLabel , context.MessageName ,

    Utilities.GetUserName(context.InitiatingUserId, audit.Service));

    audit.CreatedBy =

    Utilities.GetUserName(context.InitiatingUserId, audit.Service); //There is not user information available in the moniker. Set this in case the PreImage isn't configured.

     

    if (context.InputParameters.Properties.Contains("Target") &&

    context.InputParameters.Properties[

    "Target"] is Moniker)

    {

     

    Moniker moniker = (Moniker)context.InputParameters.Properties["Target"];

    audit.RecordID = moniker.Id.ToString();

    }

     

    else if (context.InputParameters.Properties.Contains("EntityMoniker") &&

    context.InputParameters.Properties[

    "EntityMoniker"] is Moniker)

    {

     

    Moniker moniker = (Moniker)context.InputParameters.Properties["EntityMoniker"];

    audit.RecordID = moniker.Id.ToString();

    }

     

    else if (context.InputParameters.Properties.Contains("Target") &&

    context.InputParameters.Properties[

    "Target"] is DynamicEntity)

    {

     

    //if the transaction is create then get the ID from the OutputParameters

     

    if (context.OutputParameters.Contains("id"))

    audit.RecordID = context.OutputParameters.Properties[

    "id"].ToString();

     

    //pull the KeyProperty from the entity.

     

    foreach (Property prop in ((DynamicEntity)context.InputParameters.Properties["Target"]).Properties)

    {

     

    if (prop.GetType() == typeof(KeyProperty))

    {

    audit.RecordID = ((

    KeyProperty)prop).Value.Value.ToString();

     

    break;

    }

    }

    }

     

    //Get the Pre and Post Images

     

    if (context.PreEntityImages.Properties.Contains("Images") && context.PreEntityImages.Properties["Images"] is DynamicEntity)

    {

    PreImage = (

    DynamicEntity)context.PreEntityImages.Properties["Images"];

    audit.For =

    Utilities.GetFor(context.PrimaryEntityName, PreImage);

    }

     

    if (context.PostEntityImages.Properties.Contains("Images") && context.PostEntityImages.Properties["Images"] is DynamicEntity)

    {

    PostImage = (

    DynamicEntity)context.PostEntityImages.Properties["Images"];

    audit.For =

    Utilities.GetFor(context.PrimaryEntityName, PostImage);

    }

     

    audit.AuditDifferences = Utilities.AddDifferences(PreImage, PostImage);

     

    for (int i = 0; i < audit.AuditDifferences.Count; i++)

    {

     

    AuditDifference diff = audit.AuditDifferences[i];

     

     

    if (diff.CurrentValue != diff.PreviousValue)

    attributes =

    Utilities.AddAttributeToList(diff.AttributeName, attributes);

    }

     

    if (audit.RecordID.Trim() != "")

    {

     

    string sTmp = "new_" + context.PrimaryEntityName + "_custom_audit";

     

    if (Utilities.IsRelationshipExist(sTmp ) == false)

    _relationshipAttribute =

    "";

    }

     

    if (_relationshipAttribute.Trim() != "" && audit.RecordID.Trim() != "" && context.MessageName != "Delete")

    audit.Relationship =

    new EntityRelationship(_relationshipAttribute, new Guid(audit.RecordID));

    audit.Attributes = attributes;

     

    if (!context.PreEntityImages.Properties.Contains("Images") && !context.PostEntityImages.Properties.Contains("Images"))

    audit.Create();

     

    else

    {

     

    if (attributes.Trim() != "")

    audit.Create();

    }

    }

     

    catch (System.Web.Services.Protocols.SoapException ex)

    {

     

    Utilities.Log(ex.Message + "\n" + ex.StackTrace);

    }

    }

     

    private string GetFor(string sEntityName, string sAttributeName, string sPrevVal)

    {

     

    string sReturnValue = "";

     

     

    switch (sEntityName)

    {

     

    case "account":

     

    if(sAttributeName=="name")

    sReturnValue=sPrevVal ;

     

    break;

     

    case "contact":

     

    if(sAttributeName=="fullname")

    sReturnValue = sPrevVal;

     

    break;

     

    case "lead":

     

    if(sAttributeName=="subject")

    sReturnValue = sPrevVal;

     

    break;

     

    case "opportunity":

     

    if(sAttributeName=="name")

    sReturnValue = sPrevVal;

     

    break;

     

    case "opportunityclose":

     

    if (sAttributeName == "subject")

    sReturnValue = sPrevVal;

     

    break;

    }

     

    return sReturnValue;

    }

     

    private string GetName(string sEntityName,string sMessageName,string sUserName)

    {

     

    string sVal = "";

     

    switch (sMessageName)

    {

     

    case "Assign":

    sVal +=

    "Assigned By";

     

    break;

     

    case "Create":

    sVal +=

    "Created By";

     

    break;

     

    case "Delete":

    sVal +=

    "Deleted By";

     

    break;

     

    case "GrantAccess":

    sVal +=

    "Granted Access By";

     

    break;

     

    case "ModifyAccess":

    sVal +=

    "Modify Accessed By";

     

    break;

     

    case "Retrieve":

    sVal +=

    "Retrieved By";

     

    break;

     

    case "RetrieveMultiple":

    sVal +=

    "Retrieved Multiple By";

     

    break;

     

    case "RetrieveSharedPrincipalsAndAccess":

    sVal +=

    "Retrieved Shared Principals And Access By";

     

    break;

     

    case "RevokeAccess":

    sVal +=

    "Revoked Access By";

     

    break;

     

    case "SetState":

    sVal +=

    "State Set By";

     

    break;

     

    case "Update":

    sVal +=

    "Updated By";

     

    break;

     

    case "Merge":

    sVal +=

    "Merged By";

     

    break;

     

    case "RemoveRelated":

    sVal +=

    "Removed Related By";

     

    break;

     

    case "RetrievePrincipalAccess":

    sVal +=

    "Retrieved Principal Access By";

     

    break;

     

    case "SetRelated":

    sVal +=

    "Set Related By";

     

    break;

     

    case "SetStateDynamicEntity":

    sVal +=

    "Set State Dynamic Entity By";

     

    break;

     

    case "Lose":

    sVal +=

    "Lose By";

     

    break;

     

    case "Win":

    sVal +=

    "Won By";

     

    break;

    }

    sVal = sVal.ToLower();

     

     

    return sEntityName + " entity " + sVal + " " + sUserName;

    }

    }


    G. Goyal
    Thursday, June 4, 2009 5:05 AM
  • This is IInd Code


    public

    class Audit

    {

    public AuditDifferenceCollection AuditDifferences;

    public EntityRelationship Relationship = null;

    private ICrmService _service;

    private IMetadataService _MService;

    private string _recordid = "";

    private string _audittype = "";

    private string _attributes = "";

    private string _entityname = "";

    private string _auditname = "";

    private string _createdby = "";

    private string _for = "";

    private string _OrgName = "";

    private string _entitylabel = "";

    public ICrmService Service

    {

    set { _service = value; }

    get { return _service; }

    }

    public IMetadataService MService

    {

    set { _MService = value; }

    get { return _MService; }

    }

    public string OrgName

    {

    get { return _OrgName; }

    set { _OrgName = value; }

    }

    public string Name

    {

    get { return _auditname; }

    set { _auditname = value; }

    }

    public string Type

    {

    get { return _audittype; }

    set { _audittype = value; }

    }

    public string EntityLabel

    {

    get { return _entitylabel; }

    set { _entitylabel = value; }

    }

    public string RecordID

    {

    get { return _recordid; }

    set { _recordid = value; }

    }

    public string Attributes

    {

    get { return _attributes; }

    set { _attributes = value; }

    }

    public string EntityName

    {

    get { return _entityname; }

    set { _entityname = value; }

    }

    public string CreatedBy

    {

    get { return _createdby; }

    set { _createdby = value; }

    }

    public string For

    {

    get { return _for ; }

    set { _for = value; }

    }

    public Audit()

    {

    AuditDifferences = new AuditDifferenceCollection();

    }

    public void Create()

    {

    DynamicEntity entity = new DynamicEntity();

    entity.Properties = new PropertyCollection();

     

    entity.Name = "custom_audit";

    StringProperty propName = Utilities.strProperty("custom_name", _auditname);

    StringProperty propType = Utilities.strProperty("custom_type", _audittype);

    StringProperty propEntityName = Utilities.strProperty("custom_entityname", _entityname);

    StringProperty propRecordID = Utilities.strProperty("custom_recordid", _recordid);

    StringProperty propAttributes = Utilities.strProperty("custom_attributes", _attributes);

    StringProperty propCreatedBy = Utilities.strProperty("custom_createdby", _createdby);

    StringProperty propFor = Utilities.strProperty("custom_for", _for);

    StringProperty propEntityLabel = Utilities.strProperty("custom_entitylabel", _entitylabel );

     

     

    if (Relationship != null)

    {

    LookupProperty propEntityRelationship = Utilities.lkpProperty(Relationship.Name, Relationship.ID);

    entity.Properties.Add(propEntityRelationship);

     

    }

    entity.Properties.Add(propName);

    entity.Properties.Add(propEntityName);

    entity.Properties.Add(propType);

    entity.Properties.Add(propRecordID);

    entity.Properties.Add(propAttributes);

    entity.Properties.Add(propRecordID);

    entity.Properties.Add(propCreatedBy);

    entity.Properties.Add(propFor);

    entity.Properties.Add(propEntityLabel);

    //Create the target.

    TargetCreateDynamic targetCreate = new TargetCreateDynamic();

    targetCreate.Entity = entity;

    //Create the request object.

    CreateRequest request = new CreateRequest();

    //Set the properties of the request object.

    request.Target = targetCreate;

    //Execute the request

    CreateResponse response = (CreateResponse)_service.Execute(request);

     

    foreach (AuditDifference dif in AuditDifferences)

    {

    if (dif.CurrentValue != dif.PreviousValue)

    {

    dif.AuditId = response.id;

    dif.Create(ref _service);

    }

    }

    }

    }

    public class AuditDifference

    {

    private string _attributename;

    private string _previousvalue;

    private string _currentvalue;

    private Guid _auditid;

    public AuditDifference()

    {

    }

    public AuditDifference(string attributename, string previousvalue, string currentvalue)

    {

    _attributename = attributename;

    _previousvalue = previousvalue;

    _currentvalue = currentvalue;

    }

    public Guid AuditId

    {

    get { return _auditid; }

    set { _auditid = value; }

    }

    public string AttributeName

    {

    get { return _attributename; }

    set { _attributename = value; }

    }

    public string PreviousValue

    {

    get { return _previousvalue; }

    set { _previousvalue = value; }

    }

    public string CurrentValue

    {

    get { return _currentvalue; }

    set { _currentvalue = value; }

    }

    public void Create(ref ICrmService service)

    {

    DynamicEntity entity = new DynamicEntity();

    string sAttributeDisplayName=string.Empty ;

    entity.Properties = new PropertyCollection();

    entity.Name = "custom_difference";

    sAttributeDisplayName= Utilities.GetAttributeName(_attributename);

    //Create the properties of the difference entry custom_attributelogicalname

    StringProperty propLogicalName = Utilities.strProperty("custom_attributelogicalname", _attributename );

    StringProperty propName = Utilities.strProperty("custom_attributename", sAttributeDisplayName);

    StringProperty propCurrent = Utilities.strProperty("custom_currentvalue", _currentvalue);

    StringProperty propPrev = Utilities.strProperty("custom_previousvalue", _previousvalue);

    LookupProperty propRecordID = Utilities.lkpProperty("custom_auditdifferenceid",_auditid);

    entity.Properties.Add(propLogicalName);

    entity.Properties.Add(propName);

    entity.Properties.Add(propCurrent);

    entity.Properties.Add(propPrev);

    entity.Properties.Add(propRecordID);

    //Create the target.

    TargetCreateDynamic targetCreate = new TargetCreateDynamic();

    targetCreate.Entity = entity;

    //Create the request object.

    CreateRequest request = new CreateRequest();

    //Set the properties of the request object.

    request.Target = targetCreate;

    //Execute the request

    CreateResponse response = (CreateResponse)service.Execute(request);

    }

    }

    public class AuditDifferenceCollection: CollectionBase

    {

    private Hashtable attributes = new Hashtable();

     

    public AuditDifferenceCollection()

    {

     

    }

     

    public AuditDifferenceCollection(AuditDifferenceCollection coll)

    {

    this.InnerList.AddRange(coll);

    }

     

    public AuditDifference this[int index]

    {

    get { return (AuditDifference)List[index]; }

    set { List[index] = value; attributes.Add(value.AttributeName, index); }

    }

    public AuditDifference this[string attributeName]

    {

    get { return (AuditDifference)List[(int)attributes[attributeName]]; }

    set { List[(int)attributes[attributeName]] = value;}

    }

    public virtual void Add(AuditDifference auditdifference)

    {

    List.Add(auditdifference);

    attributes.Add(auditdifference.AttributeName, attributes.Count);

    }

     

    public virtual void Remove(AuditDifference auditdifference)

    {

    List.Remove(auditdifference);

    attributes.Remove(auditdifference.AttributeName);

    }

     

    public bool Contains(AuditDifference auditdifference)

    {

    return List.Contains(auditdifference);

    }

    public bool Contains(string attributeName)

    {

    return attributes.Contains(attributeName);

    }

     

    public int IndexOf(AuditDifference auditdifference)

    {

    return List.IndexOf(auditdifference);

    }

    }

     


    G. Goyal
    Thursday, June 4, 2009 5:06 AM
  • This is IIrd Code

    //===========================================================

    static

    class Utilities

    {

    public static IMetadataService MetaService;

    public static ICrmService CRMService;

    public static string sEntityName;

    public static StringProperty strProperty(string name, string value)

    {

    StringProperty prop = new StringProperty();

    prop.Name = name;

    prop.Value = value;

    return prop;

    }

    public static LookupProperty lkpProperty(string name, Guid value)

    {

    LookupProperty prop = new LookupProperty();

    prop.Name = name;

    prop.Value = new Lookup();

    prop.Value.Value = value;

    return prop;

    }

    public static PicklistProperty pckProperty(string name, string value)

    {

    PicklistProperty prop = new PicklistProperty();

    prop.Name = name;

    prop.Value = new Picklist();

    prop.Value.Value = int.Parse(value);

    return prop;

    }

    public static string GetPropertyValue(object property)

    {

    try

    {

    if (property.GetType() == typeof(CrmBoolean))

    return ((CrmBoolean)property).Value.ToString();

    else if (property.GetType() == typeof(CrmDateTime))

    return ((CrmDateTime)property).UserTime.ToString();

    else if (property.GetType() == typeof(Owner))

    return ((Owner)property).Value.ToString();

    else if (property.GetType() == typeof(Lookup))

    return ((Lookup)property).Value.ToString();

    else if (property.GetType() == typeof(Picklist))

    return ((Picklist)property).Value.ToString();

    else if (property.GetType() == typeof(StringProperty))

    return ((StringProperty)property).Value.ToString();

    else if (property.GetType() == typeof(LookupProperty))

    return ((LookupProperty)property).Value.name.ToString();

    else if (property.GetType() == typeof(OwnerProperty))

    return ((OwnerProperty)property).Value.name.ToString();

    else if (property.GetType() == typeof(PicklistProperty))

    return ((PicklistProperty)property).Value.name.ToString();

    else if (property.GetType() == typeof(CrmDateTimeProperty))

    return ((CrmDateTimeProperty)property).Value.UserTime.ToString();

    else if (property.GetType() == typeof(KeyProperty))

    return ((KeyProperty)property).Value.Value.ToString();

    else if (property.GetType() == typeof(CrmBooleanProperty))

    return ((CrmBooleanProperty)property).Value.Value.ToString();

    else if (property.GetType() == typeof(CrmDecimalProperty))

    return ((CrmDecimalProperty)property).Value.Value.ToString();

    else if (property.GetType() == typeof(CrmFloatProperty))

    return ((CrmFloatProperty)property).Value.Value.ToString();

    else if (property.GetType() == typeof(CrmMoneyProperty))

    return ((CrmMoneyProperty)property).Value.Value.ToString();

    else if (property.GetType() == typeof(CrmNumberProperty))

    return ((CrmNumberProperty)property).Value.Value.ToString();

    else if (property.GetType() == typeof(CustomerProperty))

    return ((CustomerProperty)property).Value.name.ToString();

    else if (property.GetType() == typeof(CustomerProperty))

    return ((CustomerProperty)property).Value.name.ToString();

    else if (property.GetType() == typeof(StatusProperty))

    return ((StatusProperty)property).Value.name.ToString();

    else if (property.GetType() == typeof(StateProperty))

    return ((StateProperty)property).Value.ToString();

    else if (property.GetType() == typeof(UniqueIdentifierProperty))

    return ((UniqueIdentifierProperty)property).Value.Value.ToString();

    else if (property == null)

    return "";

    else

    return property.ToString();

    }

    catch

    { return ""; }

    }

    public static AuditDifferenceCollection AddDifferences(DynamicEntity preImage, DynamicEntity postImage)

    {

    AuditDifferenceCollection col = new AuditDifferenceCollection();

    //For any Message that has a Pre and/or Post Image. I will add the values to the Audit Differences.

    foreach (Property prop in preImage.Properties)

    {

    if(prop.GetType()!= typeof(KeyProperty) && prop.Name!="modifiedby" && prop.Name != "modifiedon" && prop.Name != "createdby" && prop.Name != "createdon")

    {

    col.Add(new AuditDifference(prop.Name, Utilities.GetPropertyValue(prop), ""));

    }

    }

    foreach (Property prop in postImage.Properties)

    {

    if(prop.GetType()!= typeof(KeyProperty) && prop.Name!="modifiedby" && prop.Name != "modifiedon" && prop.Name != "createdby" && prop.Name != "createdon")

    {

    if (col.Contains(prop.Name))

    {

    AuditDifference diff = col[prop.Name];

    diff.CurrentValue = Utilities.GetPropertyValue(prop);

    col[prop.Name] = diff;

    }

    else

    {

    col.Add(new AuditDifference(prop.Name, "", Utilities.GetPropertyValue(prop)));

    }

    }

    }

    return col;

    }

    public static string AddAttributeToList(string value, string list)

    {

    string sAttbName = GetAttributeName(value);

    if (list.IndexOf(value) == -1)

    {

    if (list == "")

    list = sAttbName ;//list = value;

    else

    list += ", " + sAttbName ;//list += "," + value;

    }

    return list;

    }

    public static string GetUserName(Guid id, ICrmService service)

    {

    Microsoft.Crm.Sdk.Query.ColumnSet cols = new Microsoft.Crm.Sdk.Query.ColumnSet();

    cols.Attributes.Add("fullname");

     

    systemuser user = (systemuser)service.Retrieve(EntityName.systemuser.ToString(), id, cols);

    return user.fullname;

    }

    public static string GetAttributeName(string sAttbName)

    {

    try

    {

    Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveAttributeRequest attributeRequest = new Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveAttributeRequest();

    attributeRequest.EntityLogicalName = sEntityName;

    attributeRequest.LogicalName = sAttbName;

    attributeRequest.RetrieveAsIfPublished = false;

     

    Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveAttributeResponse attributeResponse = (Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveAttributeResponse)MetaService.Execute(attributeRequest);

    return attributeResponse.AttributeMetadata.DisplayName.LocLabels[0].Label;

    }

    catch (System.Web.Services.Protocols.SoapException ex)

    {

    Utilities.Log(ex.Message + "\n" + ex.StackTrace);

    return sAttbName;

    }

    }

    public static string GetEntityName(string sName)

    {

    try

    {

    Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveEntityRequest attributeRequest = new Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveEntityRequest();

    attributeRequest.LogicalName = sName;

    attributeRequest.RetrieveAsIfPublished = false;

    Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveEntityResponse attributeResponse = (Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveEntityResponse)MetaService.Execute(attributeRequest);

    return attributeResponse.EntityMetadata.DisplayName.LocLabels[0].Label;

    }

    catch (System.Web.Services.Protocols.SoapException ex)

    {

    Utilities.Log(ex.Message + "\n" + ex.StackTrace);

    return sName;

    }

    }

    public static bool IsRelationshipExist(string sRelationShipName)

    {

    try

    {

    Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveRelationshipRequest relationshipRequest = new Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveRelationshipRequest();

    relationshipRequest.Name = sRelationShipName;

    relationshipRequest.RetrieveAsIfPublished = false;

    Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveRelationshipResponse relationshipResponse = (Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveRelationshipResponse)MetaService.Execute(relationshipRequest);

    Microsoft.Crm.Sdk.Metadata.OneToManyMetadata retrievedRelationship = (Microsoft.Crm.Sdk.Metadata.OneToManyMetadata)relationshipResponse.RelationshipMetadata;

    if (String.IsNullOrEmpty(retrievedRelationship.SchemaName.ToString()))

    return false;

    else

    return true;

    }

    catch (System.Web.Services.Protocols.SoapException ex)

    {

    Utilities.Log(ex.Message + "\n" + ex.StackTrace);

    return false;

    }

    }

    //public static string GetFor(string sID)

    //{

    // try

    // {

    // string sReturnValue = "";

    // switch (sEntityName)

    // {

    // case "account":

    // sReturnValue = GetFor_Account(sID);

    // break;

    // case "contact":

    // sReturnValue = GetFor_Contact(sID);

    // break;

    // case "lead":

    // sReturnValue = GetFor_Lead(sID);

    // break;

    // case "opportunity":

    // sReturnValue = GetFor_Opportunity(sID);

    // break;

    // case "opportunityclose":

    // sReturnValue = GetFor_OpportunityClose(sID);

    // break;

    // }

    // return sReturnValue;

    // }

    // catch (System.Web.Services.Protocols.SoapException ex)

    // {

    // Utilities.Log(ex.Message + "\n" + ex.StackTrace);

    // return sEntityName ;

    // }

    //}

    //public static object FindEntityByID(string sID ,string sEntityName )

    //{

    // Guid ogID = new Guid(sID);

    // Microsoft.Crm.Sdk.Query.AllColumns oCol = new Microsoft.Crm.Sdk.Query.AllColumns();

    // return (object)CRMService.Retrieve(sEntityName, ogID, oCol);

    //}

    //public static string GetFor_Account(string sID)

    //{

    // account acc =(account) FindEntityByID(sID, EntityName.account.ToString());

    // return acc.name.ToString();

    //}

    //public static string GetFor_Contact(string sID)

    //{

    // contact cnt = (contact )FindEntityByID(sID, EntityName.contact.ToString());

    // return cnt.fullname.ToString();

    //}

    //public static string GetFor_Lead(string sID)

    //{

    // lead l = (lead )FindEntityByID(sID, EntityName.lead.ToString());

    // return l.subject.ToString();

    //}

    //public static string GetFor_Opportunity(string sID)

    //{

    // opportunity o = (opportunity)FindEntityByID(sID, EntityName.opportunity.ToString());

    // return o.name ;

    //}

    //public static string GetFor_OpportunityClose(string sID)

    //{

    // opportunityclose o = (opportunityclose )FindEntityByID(sID, EntityName.opportunityclose.ToString());

    // return o.subject ;

    //}

     

    public static void Log(string sMSg)

    {

    //string LOGFILE = @"C:\CRMAudit\pluginlog.txt";

    //using (System.IO.TextWriter writer = System.IO.TextWriter.Synchronized(System.IO.File.AppendText(LOGFILE)))

    //{

    // string prefix = DateTime.Now.ToString() + ": ";

    // writer.WriteLine(prefix + sMSg );

    // writer.WriteLine("\n\n");

    //}

    }

    public static string GetForMessages_Lead(DynamicEntity preImage)

    {

    string sMessage = string.Empty;

    sMessage = Utilities.GetPropertyValue(preImage.Properties["lastname"]) ;

    if (sMessage.ToUpper() == "NONE" || sMessage.ToUpper() == "UNKNOWN" || sMessage == "-" || sMessage.ToUpper() == "BLANK" || sMessage == "")

    {

    sMessage = Utilities.GetPropertyValue(preImage.Properties["companyname"]);

    }

    else

    sMessage += " of "+ Utilities.GetPropertyValue(preImage.Properties["companyname"]);

    return sMessage;

    }

    public static string GetForMessages_Account(DynamicEntity preImage)

    {

    string sMessage = string.Empty;

    sMessage = Utilities.GetPropertyValue(preImage.Properties["name"]) ;

    return sMessage;

    }

    public static string GetForMessages_Contact(DynamicEntity preImage)

    {

    string sMessage = string.Empty;

    sMessage = Utilities.GetPropertyValue(preImage.Properties["fullname"]) ;

     

    return sMessage;

    }

    public static string GetForMessages_Opportunity(DynamicEntity preImage)

    {

    string sMessage = string.Empty;

    sMessage = Utilities.GetPropertyValue(preImage.Properties["name"]) ;

     

    return sMessage;

    }

    public static string GetFor(string sEntityName,DynamicEntity dE)

    {

    try

    {

    string sReturnValue = "";

    switch (sEntityName)

    {

    case "account":

    sReturnValue = GetForMessages_Account(dE);

    break;

    case "contact":

    sReturnValue = GetForMessages_Contact(dE);

    break;

    case "lead":

    sReturnValue = GetForMessages_Lead(dE);

    break;

    case "opportunity":

    sReturnValue = GetForMessages_Opportunity(dE);

    break;

    }

    return sReturnValue;

    }

    catch (System.Web.Services.Protocols.SoapException ex)

    {

    Utilities.Log(ex.Message + "\n" + ex.StackTrace);

    return "";

    }

    }

    }






    G. Goyal
    Thursday, June 4, 2009 5:07 AM
  • Now its working .
    Some Date and Time related field that is CRM Field was creating some problem.
    G. Goyal
    • Marked as answer by G Goyal Thursday, September 24, 2009 6:27 AM
    Thursday, September 24, 2009 6:26 AM