I need to run my plugin when user is opened the Account record.
I used Registration Tool to Register New Assembly.
I created a step:
Message - Retrieve
Primary Entity - account
Event Pipeline Stage of Execution - Pre-operation.
By when I open the account - the plugin doesn't run (
What should I do to solve it?
my code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Messages;
namespace FormLoadCRM
{
public class SetForm : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext pluginContext =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory servicefactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService client = servicefactory.CreateOrganizationService(pluginContext.UserId);
//throw new InvalidPluginExecutionException("Execute Method called");
SetcForm(client, pluginContext.UserId, tracingService);
}
private void SetcForm(IOrganizationService service, Guid userId, ITracingService tracingService)
{
try
{
// throw new InvalidPluginExecutionException("Last form viwed");
RetrieveEntityRequest request = new RetrieveEntityRequest();
request.LogicalName = "Account";
// Retrieve the MetaData.
RetrieveEntityResponse response = (RetrieveEntityResponse)service.Execute(request);
int objecttypecode = response.EntityMetadata.ObjectTypeCode.Value;
var query = new QueryExpression("userentityuisettings");
query.Criteria.AddCondition("ownerid", ConditionOperator.Equal, userId);
query.Criteria.AddCondition("objecttypecode", ConditionOperator.Equal, objecttypecode);
query.ColumnSet.AddColumn("lastviewedformxml");
EntityCollection encol = service.RetrieveMultiple(query);
string last1 = encol[0].Attributes["lastviewedformxml"].ToString();
// throw new InvalidPluginExecutionException("Last form viwed: " + last1);
string idFormToUse = string.Empty;
var query2 = new QueryExpression("systemform");
query2.Criteria.AddCondition("name", ConditionOperator.Equal, "Account");
query2.ColumnSet = new ColumnSet("formid");
EntityCollection form = service.RetrieveMultiple(query2);
if (form != null)
{
string guidformateado = form[0].Id.ToString();
idFormToUse = string.Format("", guidformateado);
string last = encol[0].Attributes["lastviewedformxml"].ToString();
if (!idFormToUse.Equals(last, StringComparison.InvariantCultureIgnoreCase))
{
Entity userUI = new Entity("userentityuisettings");
userUI.Id = encol[0].Id;
userUI.Attributes["lastviewedformxml"] = idFormToUse;
//just for unit testing.
//throw new InvalidPluginExecutionException("Last form viwed: " + last + " form to use: " + idFormToUse);
service.Update(userUI);
}
}
else
{
return;
}
}
catch (Exception ex)
{
tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
}
}
}
}