Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Disable the notes from custom form.

Posted on by Microsoft Employee

Hi,

I am new in microsoft dynamic CRM.

I am using annotation entity on custom entity. 

I want to attach only one file in notes. If one attachment had done then it disable for current form.  

*This post is locked for comments

  • Piotr Kalwasinski Profile Picture
    Piotr Kalwasinski 240 on at
    RE: Disable the notes from custom form.

    Hi,

    Please use my code from the example above, I have build it and tested it before posting it.

    In the meanwhile, I will comment on your code as I can see you are adopting bad habits, which I always fight to the tooth - nothing personal.

    documentQuery.ColumnSet = new ColumnSet(true);

    This mean you will retrieve all properties of Note, including attached data. If you omit this line, your query will retrieve only ID, and since you are only interested in check weather the note has already been attached.

    documentCondition.Values.Add(target["new_documenttestid"].ToString());

    When you're using target[""] to access properties, there is a chance the field may not be there, so in this strategy you need to handle potential exception. "new_documenttestid" is not going to be the ID of object to which you attach a document, this is a new custom field.

    if (Convert.ToInt32(documentCollection.Entities.Count.ToString()) > 1)

    You are creating overhead in your plugin performance, please note you are first converting already an integer Count to string (no longer a primitive data type), just so you can convert it back to bigger integer of 32 bits.

    Let me know if there is anything wrong with my previous solution, and if it works for you, please mark it as resolved.

    Thanks,

    Piotr

    public void Execute(IServiceProvider serviceProvider)

           {

               // Obtain the execution context from the service provider.

               var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               var service = serviceFactory.CreateOrganizationService(context.UserId);

               try

               {

                   if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))

                   {

                       throw new InvalidPluginExecutionException("Plugin configuration is invalid. Please contact adminsitrator with the issue.");

                   }

                   Entity target = (Entity)context.InputParameters["Target"];  

                   QueryExpression documentQuery = new QueryExpression("annotation");

                   documentQuery.ColumnSet = new ColumnSet(true);

                   ConditionExpression documentCondition = new ConditionExpression();

                   documentCondition.AttributeName = "objectid";

                   documentCondition.Operator = ConditionOperator.Equal;

      =>       documentCondition.Values.Add(target["new_documenttestid"].ToString());  

                   documentQuery.Criteria.Conditions.Add(documentCondition);

                   OrderExpression documentOrder = new OrderExpression("createdon", OrderType.Ascending);

                   documentQuery.Orders.Add(documentOrder);

                   EntityCollection documentCollection = (EntityCollection)service.RetrieveMultiple(documentQuery);

                   if (Convert.ToInt32(documentCollection.Entities.Count.ToString())>1)

                   {

                       throw new  Exception("Attach only single document");

                   }

                  // throw new InvalidPluginExecutionException(target.LogicalName.ToString()+ ">>@@"+ documentCollection.Entities.Count.ToString()+ "@@");

               }

               catch (Exception e)

               {

                   throw new InvalidPluginExecutionException(e.Message.ToString());

               }

           }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Disable the notes from custom form.

    Thanks Alagu nellaikumar.S

    I am using below plugin code and plugin registration tool setting.

    public void Execute(IServiceProvider serviceProvider)

           {

               // Obtain the execution context from the service provider.

               var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               var service = serviceFactory.CreateOrganizationService(context.UserId);

               try

               {

                   if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))

                   {

                       throw new InvalidPluginExecutionException("Plugin configuration is invalid. Please contact adminsitrator with the issue.");

                   }

                   Entity target = (Entity)context.InputParameters["Target"];  

                   QueryExpression documentQuery = new QueryExpression("annotation");

                   documentQuery.ColumnSet = new ColumnSet(true);

                   ConditionExpression documentCondition = new ConditionExpression();

                   documentCondition.AttributeName = "objectid";

                   documentCondition.Operator = ConditionOperator.Equal;

      =>       documentCondition.Values.Add(target["new_documenttestid"].ToString());  

                   documentQuery.Criteria.Conditions.Add(documentCondition);

                   OrderExpression documentOrder = new OrderExpression("createdon", OrderType.Ascending);

                   documentQuery.Orders.Add(documentOrder);

                   EntityCollection documentCollection = (EntityCollection)service.RetrieveMultiple(documentQuery);

                   if (Convert.ToInt32(documentCollection.Entities.Count.ToString())>1)

                   {

                       throw new  Exception("Attach only single document");

                   }

                  // throw new InvalidPluginExecutionException(target.LogicalName.ToString()+ ">>@@"+ documentCollection.Entities.Count.ToString()+ "@@");

               }

               catch (Exception e)

               {

                   throw new InvalidPluginExecutionException(e.Message.ToString());

               }

           }

    Message: Create

    Entity: Annotation or Notes

    Operation: Pre-operation

    Now, When i am add file on annotation entity then it throw exception(The given key was not present in the dictionary.)  at the line of code marks as  => .

  • Suggested answer
    Alagunellaikumar Profile Picture
    Alagunellaikumar 6,210 on at
    RE: Disable the notes from custom form.

    Hi
    You can use plugin registration tool to register the plugin

    Message: Create
    Entity: Annotation or Notes
    Operation: Pre-operation


    Please refer the below URL
    alagunellaikumar.blogspot.in/.../how-to-register-plugin-in-crm.html

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Disable the notes from custom form.


    Thanks @Piotr Kalwasinski

    How can i trigger the plugin on creation of note .

  • Suggested answer
    Piotr Kalwasinski Profile Picture
    Piotr Kalwasinski 240 on at
    RE: Disable the notes from custom form.

    Hi,

    I have put together simple code for you to work with (below). I agree with guys before - you should not use unsupported code, please use this plugin code instead. Please register the plugin for Create on Annotation Pre-Opperation.

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using System;

    namespace plugins
    {
    public class Test : IPlugin
    {
    public void Execute(IServiceProvider serviceProvider)
    {
    // Obtain the execution context from the service provider.
    var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

    var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    var service = serviceFactory.CreateOrganizationService(context.UserId);

    try
    {
    if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))
    {
    throw new InvalidPluginExecutionException("Plugin configuration is invalid. Please contact adminsitrator with the issue.");
    }

    Entity target = (Entity)context.InputParameters["Target"];

    QueryExpression query = new QueryExpression("annotation");
    query.Criteria.AddCondition("objectid", ConditionOperator.Equal, target.GetAttributeValue<EntityReference>("objectid").Id);
    EntityCollection result = service.RetrieveMultiple(query);

    if (result.Entities.Count > 0 )
    {
    throw new InvalidPluginExecutionException("This record already has an attachment.");
    }
    }
    catch (Exception e)
    {
    throw;
    }
    }
    }
    }

    Thanks,

    Piotr

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Disable the notes from custom form.

    Hi,

    You should not do unsupported code in CRM. They will be painful during updates/migration/maintenance of CRM project. The only option that makes sense here is a plugin that checks if there is already a note on the record and if yes, then it should throw exception.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Disable the notes from custom form.

    I am using following code on form onload()  

    function DisableNotes()

    {                

        $(“#notescontrol a[title=’NOTES’]”).attr(‘disabled’, ‘disabled’);

    }

    but it not work , because i did not know how to jquery code work with javascript in CRM

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Disable the notes from custom form.

    This post is to disable Notes for CRM 2011 version, I have to check how to disable Notes for crm 2016 version.

    Please try this one: https://mahenderpal.wordpress.com/2014/03/17/disable-notes-section-in-ms-crm-2013-quick-tip/

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Disable the notes from custom form.

    I am follow below link.

    ms-crm-2011-beta.blogspot.sg/.../disabling-notes-for-deactivated-records.html

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Disable the notes from custom form.

    @1213214033 Which one did you try to disable Notes or odata query and which one failed

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans