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
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());
}
}
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 => .
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
Thanks @Piotr Kalwasinski
How can i trigger the plugin on creation of note .
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
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.
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
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/
I am follow below link.
ms-crm-2011-beta.blogspot.sg/.../disabling-notes-for-deactivated-records.html
@1213214033 Which one did you try to disable Notes or odata query and which one failed
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156