CRM 2011 Custom Workflow - plugins

I GO THE PLUGIN WORKING, AND I HAVE UPDATED THE WORKING CODE HERE. HOPE IT HELPS!!
I'm creating a workflow which has a plugin to retrieve a contact entity from a "FROM" field of an Email record. I'm trying to see if that email exists in the contact entity. The input is the "FROM" email and the output will return the Contacts entity. I have not been able to get this code working, I've received different errors or no errors, but I know its not working. Please help! Thanks in advance!
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using System;
namespace GetSenderPlugin
{
public class GetSenderPlugin : CodeActivity
{
protected override void Execute(CodeActivityContext executionContext)
{
//Create the tracing service
ITracingService trace = executionContext.GetExtension<ITracingService>();
trace.Trace("*****Tracing Initiated*****");
//Create the IWorkflowContext and the IOrganizationService for communication with CRM
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
trace.Trace("*****IOrganizationService created*****");
trace.Trace("*****Entity logical Name: " + Email.Get<EntityReference>(executionContext).LogicalName + "*****");
trace.Trace("*****Entity ID: " + Email.Get<EntityReference>(executionContext).Id + "*****");
if (Email != null && string.Compare(Email.Get<EntityReference>(executionContext).LogicalName, "email", false) == 0)
{
EntityReference retrieveEmail = new EntityReference();
retrieveEmail.Id = Email.Get<EntityReference>(executionContext).Id;
retrieveEmail.LogicalName = Email.Get<EntityReference>(executionContext).LogicalName;
retrieveEmail.Name = Email.Get<EntityReference>(executionContext).Name;
string[] strArrays = new string[1];
strArrays[0] = "from";
ColumnSet columnSet = new ColumnSet();
columnSet.AddColumn(strArrays[0]);
RetrieveRequest retrieveRequest = new RetrieveRequest();
retrieveRequest.Target = retrieveEmail;
retrieveRequest.ColumnSet = columnSet;
trace.Trace("*****Retrieve Request declared*****");
RetrieveResponse retrieveResponse = (RetrieveResponse)service.Execute(retrieveRequest);
trace.Trace("*****Retrieve Response executed*****");
Email businessEntity = (Email)retrieveResponse.Entity;
trace.Trace("*****businessEnitity retrieved*****");
//ActivityParty activitypartyArray = (ActivityParty)businessEntity.From.FirstOrDefault;
foreach (ActivityParty activityParty in businessEntity.From)
{
trace.Trace("*****Activity Party Name: " + activityParty.PartyId.LogicalName + "*****");
trace.Trace("*****Activity Party Id: " + activityParty.PartyId.Id + "*****");
if (activityParty != null && activityParty != null && activityParty.PartyId != null)
{
string str = activityParty.PartyId.LogicalName;
if (str.CompareTo("contact") != 0)
{
trace.Trace("*****Not Contact*****");
if (str.CompareTo("account") != 0)
{
trace.Trace("*****Not Account*****");
if (str.CompareTo("lead") != 0)
{
trace.Trace("*****Not Lead*****");
if (str.CompareTo("systemuser") != 0)
{
trace.Trace("*****Not System User*****");
if (str.CompareTo("queue") == 0)
{
Queue.Set(executionContext, activityParty.PartyId);
Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
}
else
{
trace.Trace("*****User not found*****");
Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
}
}
else
{
User.Set(executionContext, activityParty.PartyId);
Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
}
}
else
{
Lead.Set(executionContext, activityParty.PartyId);
Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
}
}
else
{
Account.Set(executionContext, activityParty.PartyId);
Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
}
}
else
{
trace.Trace("*****Contact assigned*****");
Contact.Set(executionContext, activityParty.PartyId);
Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
}
}
break;
}
}
else
{
trace.Trace("*****Email is null*****");
Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
}
}
#region Properties
[Input("E-mail")]
[ReferenceTarget("email")]
public InArgument<EntityReference> Email { get; set; }
[Output("Account")]
[ReferenceTarget("account")]
public OutArgument<EntityReference> Account { get; set; }
[Output("Contact")]
[ReferenceTarget("contact")]
public OutArgument<EntityReference> Contact { get; set; }
[Output("Lead")]
[ReferenceTarget("lead")]
public OutArgument<EntityReference> Lead { get; set; }
[Output("Queue")]
[ReferenceTarget("queue")]
public OutArgument<EntityReference> Queue { get; set; }
[Output("User")]
[ReferenceTarget("systemuser")]
public OutArgument<EntityReference> User { get; set; }
#endregion
}
}
So we are upgrading our internal CRM system from 4.0 to 2011, and this was a plugin that was on a workflow. I didn't have the original source code so I didn't really know what the code was doing. But I decompiled the original .dll file after I exported the solution. I tried to rewrite the code for CRM 2011, and here is what I have. After I tested the workflow, I get error saying: "Expected non-empty Guid." The code goes to the very last line after the loop then gives me the error. Here is the trace:
Workflow paused due to error: Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Expected non-empty Guid.Detail:
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
<ErrorCode>-2147220989</ErrorCode>
<ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
<Message>Expected non-empty Guid.</Message>
<Timestamp>2013-02-21T23:46:37.0376093Z</Timestamp>
<InnerFault>
<ErrorCode>-2147220970</ErrorCode>
<ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
<Message>System.ArgumentException: Expected non-empty Guid.
Parameter name: id</Message>
<Timestamp>2013-02-21T23:46:37.0376093Z</Timestamp>
<InnerFault i:nil="true" />
<TraceText i:nil="true" />
</InnerFault>
<TraceText>[Microsoft.Xrm.Sdk.Workflow: Microsoft.Xrm.Sdk.Workflow.Activities.RetrieveEntity]
[RetrieveEntity]
*****Tracing Initiated*****
*****IOrganizationService created*****
*****Entity logical Name: email*****
*****Entity ID: c49e4c7c-8724-de11-86ce-000c290f83d7*****
*****Retrieve Request declared*****
*****Retrieve Response executed*****
*****businessEnitity retrieved*****
*****Activity Party Name: contact*****
*****Activity Party Id: 79ed3a33-8eb9-dc11-8edd-00c09f226ebb*****
*****Activity Party not null*****
*****Contact assigned*****
*****foreach ended*****</TraceText>
</OrganizationServiceFault>
at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Retrieve(String entityName, Guid id, ColumnSet columnSet, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
at Microsoft.Crm.Extensibility.InprocessServiceProxy.RetrieveCore(String entityName, Guid id, ColumnSet columnSet)
at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Retrieve(String entityName, Guid id, ColumnSet columnSet)
at Microsoft.Crm.Workflow.Services.RetrieveActivityService.<>c__DisplayClass1.<RetrieveInternal>b__0(IOrganizationService sdkService)
at Microsoft.Crm.Workflow.Services.ActivityServiceBase.ExecuteInTransactedContext(ActivityDelegate activityDelegate)
at Microsoft.Crm.Workflow.Services.RetrieveActivityService.ExecuteInternal(ActivityContext executionContext, RetrieveEntity retrieveEntity)
at Microsoft.Crm.Workflow.Services.RetrieveActivityService.Execute(ActivityContext executionContext, RetrieveEntity retrieveEntity)
at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

I've received different errors or no errors, but I know its not working
First, I suggest you to list errors in your question. It will be much easier to answer you and to avoid -1s. :) Second, use tracing service in workflow.
ITracingService tracingService = context.GetExtension<ITracingService>();
tracingService.Trace("I'm tracing something....");
What should happen if Contact with specified email is not found? You have to handle this case. Probably it fails here. Post error log in question, to check this.

If I'm to give a shot-from-the-hip answer, I'd guess this is the problem is that you in some cases don't get a match and filter out everything. Then, you get null somewhere (or try to refer to an empty guid, or try to select an attribute that hasn't been entered so that, although it's defined, it's not served).
What exact errors do you get? When do you get them and when don't you?
Also, some issues with the code that I've noticed (not the solution but still might be improved).
Typo in the name. I think you wanted ContactReference.
[Output("output")]
[ReferenceTarget("contact")]
public OutArgument<EntityReference> ContactRefernce { get; set; }
I'd design the query in a bit different way. Since you know what address you want to match, you might want to filter precisely that. Also, You seem only to be needing the guid for the match based on the email so you only need to fetch that.
private Guid MatchSenderWithExistingContact(
IOrganizationService service, String fromAddress)
{
QueryExpression query = new QueryExpression
{
EntityName = "contact",
ColumnSet = new ColumnSet("emailaddress1"),
Criteria = new FilterExpression
{
Filters =
{
new FilterExpression
{
Conditions =
{
new ConditionExpression(
"emailaddress1", ConditionOperator.Equal, fromAddress)
}
}
}
}
};
EntityCollection retrieveMultipleRequest = service.RetrieveMultiple(query);
IEnumerable<Entity> entities = retrieveMultipleRequest.Entities;
return entities.FirstOrDefault().Id;
}
You might want to put the declaration of the query on step-by-step syntax. I prefer it this way because it's more convenient when creating advanced queries. However, the part when you iterated and used break is definitely better off as shown above.
Happy coding!
It might behoove you to use the tracing. Usually, I declare the tracing object as a member variable in the plugin class and then write to it at every operation. That way, at least I know where manure hits the AC and can log the value of the variables right before that.
private ITracingService trace;
public void Execute(IServiceProvider serviceProvider)
{
trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
_trace.Trace("Tracing successful!");
throw new Exception("Intentional! Nice.");
}
Just keep it mind that the trace won't show unless an uncaught exception occurs. In fact, I've intentionally crashed the execution on occasion just to see what's in the variables. So remember that if you've got a global try-catch, you'll need to re-throw the exception manually.
Upside of this method is that you can tracing both on-premise and on-line. It works for all plugin registrations except the asynchronous. There are work-arounds for that too but that wasn't the topic of your question and I've digressed enough already.

Related

Working on pre-operation plug-in to update "Modified By" field in MSCRM -- Need help fixing code

I am trying to update the "Modified By" field based on a text field called "Prepared By", which contains the name of a user. I've created a pre-operation plug-in to do this and believe I am close to done. However, the "Modified By" field is still not successfully getting updated. I am relatively new to coding and CRM, and could use some help modifying the code and figuring out how I can get this to work.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Linq;
namespace TimClassLibrary1.Plugins
{
public class CreateUpdateContact : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = factory.CreateOrganizationService(context.UserId);
tracingService.Trace("Start plugin");
tracingService.Trace("Validate Target");
if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))
return;
tracingService.Trace("Retrieve Target");
var target = (Entity)context.InputParameters["Target"];
String message = context.MessageName.ToLower();
SetCreatedByAndModifiedBy(tracingService, service, target, message);
}
private void SetCreatedByAndModifiedBy(ITracingService tracingService, IOrganizationService service, Entity target, string message)
{
tracingService.Trace("Start SetPriceList");
tracingService.Trace("Validate Message is Create or Update");
if (!message.Equals("create", StringComparison.OrdinalIgnoreCase) && !message.Equals("update", StringComparison.OrdinalIgnoreCase))
return;
tracingService.Trace("Retrieve Attributes");
var createdByReference = target.GetAttributeValue<EntityReference>("new_createdby");
var modifiedByReference = target.GetAttributeValue<EntityReference>("new_modifiedby");
tracingService.Trace("Retrieve And Set User for Created By");
RetrieveAndSetUser(tracingService, service, target, createdByReference, "createdby");
tracingService.Trace("Retrieve And Set User for Modified By");
RetrieveAndSetUser(tracingService, service, target, modifiedByReference, "modifiedby");
}
private void RetrieveAndSetUser(ITracingService tracingService, IOrganizationService service, Entity target, EntityReference reference, string targetAttribute)
{
tracingService.Trace("Validating Reference");
if (reference == null)
return;
tracingService.Trace("Retrieving and Validating User");
var user = RetrieveUserByName(service, reference.Name, new ColumnSet(false));
if (user == null)
return;
tracingService.Trace("Setting Target Attribute");
target[targetAttribute] = user.ToEntityReference();
}
private Entity RetrieveUserByName(IOrganizationService service, string name, ColumnSet columns)
{
var query = new QueryExpression
{
EntityName = "systemuser",
ColumnSet = columns,
Criteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "fullname",
Operator = ConditionOperator.Equal,
Values = { name }
}
}
}
};
var retrieveResponse = service.RetrieveMultiple(query);
if (retrieveResponse.Entities.Count == 1)
{
return retrieveResponse.Entities.FirstOrDefault();
}
else
{
return null;
}
}
}
}
If you do get use from method Retreiveusernyname then you have to use below code
target[“modifiedby”] = new EntityRefrence(user.logicalname,user.id);
I don't see anything obviously wrong with your update, however you are taking a complicated and unnecessary step with your RetrieveUserByName() method. You already have EntityReference objects from your new_createdby and new_modifiedby fields, you can simply assign those to the target:
if (message.Equals("create", StringComparison.OrdinalIgnoreCase))
{
target["createdby"] = target["new_createdby];
}
else if (message.Equals("update", StringComparison.OrdinalIgnoreCase))
{
target["modifiedby"] = target["new_modifiedby];
}
If new_createdby and new_modifiedby are not entity references, then that would explain why your existing code does not work, if they are, then use my approach.

Camel mongodb - MongoDbProducer multiple inserts

I am trying to do a multiple insert using the camel mongo db component.
My Pojo representation is :
Person {
String firstName;
String lastName;
}
I have a processor which constructs a valid List of Person pojo and is a valid json structure.
When this list of Person is sent to the mongodb producer , on invocation of createDoInsert the type conversion to BasicDBObject fails. This piece of code below looks to be the problem. Should it have more fall backs / checks in place to attempt the list conversion down further below as it fails on the very first cast itself. Debugging the MongoDbProducer the exchange object being received is a DBList which extends DBObject. This causes the singleInsert flag to remain set at true which fails the insertion below as we get a DBList instead of a BasicDBObject :
if(singleInsert) {
BasicDBObject insertObjects = (BasicDBObject)insert;
dbCol.insertOne(insertObjects);
exchange1.getIn().setHeader("CamelMongoOid", insertObjects.get("_id"));
}
The Camel MongoDbProducer code fragment
private Function<Exchange, Object> createDoInsert() {
return (exchange1) -> {
MongoCollection dbCol = this.calculateCollection(exchange1);
boolean singleInsert = true;
Object insert = exchange1.getIn().getBody(DBObject.class);
if(insert == null) {
insert = exchange1.getIn().getBody(List.class);
if(insert == null) {
throw new CamelMongoDbException("MongoDB operation = insert, Body is not conversible to type DBObject nor List<DBObject>");
}
singleInsert = false;
insert = this.attemptConvertToList((List)insert, exchange1);
}
if(singleInsert) {
BasicDBObject insertObjects = (BasicDBObject)insert;
dbCol.insertOne(insertObjects);
exchange1.getIn().setHeader("CamelMongoOid", insertObjects.get("_id"));
} else {
List insertObjects1 = (List)insert;
dbCol.insertMany(insertObjects1);
ArrayList objectIdentification = new ArrayList(insertObjects1.size());
objectIdentification.addAll((Collection)insertObjects1.stream().map((insertObject) -> {
return insertObject.get("_id");
}).collect(Collectors.toList()));
exchange1.getIn().setHeader("CamelMongoOid", objectIdentification);
}
return insert;
};
}
My route is as below :
<route id="uploadFile">
<from uri="jetty://http://0.0.0.0:9886/test"/>
<process ref="fileProcessor"/>
<unmarshal>
<csv>
<header>fname</header>
<header>lname</header>
</csv>
</unmarshal>
<process ref="mongodbProcessor" />
<to uri="mongodb:mongoBean?database=axs175&collection=insurance&operation=insert" />
and the MongoDBProcessor constructing the List of Person Pojo
#Component
public class MongodbProcessor implements Processor {
#Override
public void process(Exchange exchange) throws Exception {
ArrayList<List<String>> personlist = (ArrayList) exchange.getIn().getBody();
ArrayList<Person> persons = new ArrayList<>();
for(List<String> records : personlist){
Person person = new Person();
person.setFname(records.get(0));
person.setLname(records.get(1));
persons.add(person);
}
exchange.getIn().setBody(persons);
}
}
Also requested information here - http://camel.465427.n5.nabble.com/Problems-with-MongoDbProducer-multiple-inserts-tc5792644.html
This issue is now fixed via - https://issues.apache.org/jira/browse/CAMEL-10728

MS CRM 2015, plugins don't seem to fire

I'm having some trouble with the new MS CRM 2015 as I cannot make my plugins fire.
I even tried to take some of the very simple plugin samples from the 2015 SDK and register them 'non sandbox' but the result is the same.
The only thing I cant actually get to do anything on the triggering event is the plugin profiler, but that doesn't really help me much.
Has anyone else had this problem?
I could really use some advice on what to try/check next because Google doesn't appear to be my friend in this case?
The current deployment is on-premise by the way but there is no Visual Studio available on the server.
Here is the code from sample which I have altered just a tiny bit to only trigger on 1 specific account.
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
namespace Microsoft.Crm.Sdk.Samples
{
public class FollowupPlugin: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//</snippetFollowupPlugin1>
//<snippetFollowupPlugin2>
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
//</snippetFollowupPlugin2>
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName != "account")
return;
try
{
if (entity.Attributes.ContainsKey("accountid"))
{
if (entity.GetAttributeValue<Guid>("accountid").ToString().ToLower() == "ee03d883-5b18-de11-a0d1-000c2962895d") // specific test account
{
// Create a task activity to follow up with the account customer in 7 days.
Entity followup = new Entity("task");
followup["subject"] = "Send e-mail to the new customer.";
followup["description"] = "Follow up with the customer. Check if there are any new issues that need resolution.";
followup["scheduledstart"] = DateTime.Now.AddDays(7);
followup["scheduledend"] = DateTime.Now.AddDays(7);
followup["category"] = context.PrimaryEntityName;
// Refer to the account in the task activity.
if (context.OutputParameters.Contains("id"))
{
Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
string regardingobjectidType = "account";
followup["regardingobjectid"] = new EntityReference(regardingobjectidType, regardingobjectid);
}
//<snippetFollowupPlugin4>
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//</snippetFollowupPlugin4>
// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("FollowupPlugin: Creating the task activity.");
service.Create(followup);
}
}
}
//<snippetFollowupPlugin3>
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in the FollupupPlugin plug-in.", ex);
}
//</snippetFollowupPlugin3>
catch (Exception ex)
{
tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}
Registration screenshots:
Try to use following code:
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
namespace Microsoft.Crm.Sdk.Samples
{
public class FollowupPlugin: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//</snippetFollowupPlugin1>
//<snippetFollowupPlugin2>
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
//</snippetFollowupPlugin2>
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName != "account")
return;
try
{
if (entity.Id.Equals(new Guid("ee03d883-5b18-de11-a0d1-000c2962895d")) // specific test account
{
// Create a task activity to follow up with the account customer in 7 days.
Entity followup = new Entity("task");
followup["subject"] = "Send e-mail to the new customer.";
followup["description"] = "Follow up with the customer. Check if there are any new issues that need resolution.";
followup["scheduledstart"] = DateTime.Now.AddDays(7);
followup["scheduledend"] = DateTime.Now.AddDays(7);
followup["category"] = context.PrimaryEntityName;
followup["regardingobjectid"] = entity.ToEntityReference();
//<snippetFollowupPlugin4>
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//</snippetFollowupPlugin4>
// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("FollowupPlugin: Creating the task activity.");
service.Create(followup);
}
}
//<snippetFollowupPlugin3>
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in the FollupupPlugin plug-in.", ex);
}
//</snippetFollowupPlugin3>
catch (Exception ex)
{
tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}

CRM 2011 Online Plugin works fine but no Update committed

I'm a new developper in CRM 2011. I've made that simple code for tests.
It appears that the plugin works fine (checked with the ITracingService) but it seems that the attributes doesn't take the new values.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;
namespace AgeUpdatePlugin
{
public class AgeUpdatePlugin:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
try
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (tracingService == null)
throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
tracingService.Trace("Plugin has started..");
//l'expression de condition
ConditionExpression condition = new ConditionExpression("new_datenaissance", ConditionOperator.NotNull);
FilterExpression filter = new FilterExpression();
filter.Conditions.Add(condition);
ColumnSet cols = new ColumnSet();
//cols.AddColumn("Id");
cols.AddColumn("new_candidatid");
cols.AddColumn("new_age");
cols.AddColumn("new_datenaissance");
QueryExpression query = new QueryExpression();
query.Criteria = filter;
query.EntityName = "new_candidat";
query.ColumnSet = cols;
tracingService.Trace("appel retrieve multiple with date=" + DateTime.Now.ToString() + "/"+DateTime.Now.ToLocalTime().ToString());
var retrieve = service.RetrieveMultiple(query).Entities;
foreach (var c in retrieve)
{
var dt =(DateTime)c.Attributes["new_datenaissance"];
tracingService.Trace(dt.ToString());
if (dt.Month == DateTime.Now.Month && dt.Day == DateTime.Now.Day)
{
tracingService.Trace((DateTime.Now.Year - DateTime.Parse(c["new_datenaissance"].ToString()).Year).ToString());
c.Attributes["new_age"] = DateTime.Now.Year - DateTime.Parse(c["new_datenaissance"].ToString()).Year;
tracingService.Trace(c.Attributes["new_candidatid"].ToString() +" - "+c.Attributes["new_age"].ToString());
service.Update(c);
tracingService.Trace("updated");
}
}
tracingService.Trace("Plugin done working");
throw new InvalidPluginExecutionException("This is from a plugin that Mehdi has created ");
}
catch (Exception exc)
{
throw exc;
}
}
}
}
Whenever you throw an exception within a plugin the transaction is aborted so you won't see your changes applied. Remove the following line:
throw new InvalidPluginExecutionException("This is from a plugin that Mehdi has created ");
I assume you added this to see the trace output which is only available when an exception is thrown. With CRM Online your best bet for tracing is to write to a custom trace entity.

Prevent sending email and show message via plug-in

I am writing crm2011 plugin in "Email" entity with "Send" Message of Pre_operation. What i want to do is when i click "Send" button in email entity, I do the necessary checking before send. If the checking is not correct, I want to prevent and stop the sending email and show "the alert message" and stop the second plugin(this plugin send email and create the associated entity to convert "Case"). Please give me some suggestion for that plugin?
Should i use pre-Validation stage or Pre_operation state? And how can I return false to stop plugin.
public void Execute(IServiceProvider serviceProvider)
{
try
{
string message = null;
_serviceProvider = serviceProvider;
_context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
_serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
_currentUser = _context.UserId;
message = _context.MessageName.ToLower();
if (message == "send")
{
if (_context.InputParameters != null && _context.InputParameters.Contains("EmailId"))
{
object objEmailId = _context.InputParameters["EmailId"];
if (objEmailId != null)
{
_emailId = new Guid(objEmailId.ToString());
FindEmailInfo();
if (_email != null)
{
if (_email.Attributes.Contains("description") && _email.Attributes["description"] != null)//Email descritpion is not null
{
string emaildescription = StripHTML();
//Find KB Article prefix no in system config entity
serviceguideprefix = "ServiceGuidesPrefix";
QueryByAttribute query = new QueryByAttribute("ppp_systemconfig");
query.ColumnSet = new ColumnSet(true);
query.AddAttributeValue(sysconfig_name, serviceguideprefix);
EntityCollection sysconfig = _service.RetrieveMultiple(query);
if (sysconfig.Entities.Count > 0)
{
Entity e = sysconfig.Entities[0];
if (e.Attributes.Contains("ppp_value"))
{
ppp_value = e.Attributes["ppp_value"].ToString();
}
}
if (ppp_value != null && ppp_value != string.Empty)
{
//var matches = Regex.Matches(emaildescription, #"KBA-\d*-\w*").Cast<Match>().ToArray();
var matches = Regex.Matches(emaildescription, ppp_value + #"-\d*-\w*").Cast<Match>().ToArray();
//ReadKBNo(emaildescription);
foreach (Match kbnumber in matches)
{
EntityCollection kbarticlecol = FindKBArticleIds(kbnumber.ToString());
if (kbarticlecol.Entities.Count > 0)
{
Entity kbariticle = kbarticlecol.Entities[0];
if (kbariticle.Attributes.Contains("mom_internalkm"))
{
bool internalserviceguide = (bool)kbariticle.Attributes["mom_internalkm"];
if (internalserviceguide) found = true;
else found = false;
}
else found = false;
}
}
}
if (found)
{
//-----
}
}
}
}
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message, ex);
}
}
Well stopping the plugin is dead easy you just throw InvalidPluginException, the message you give it will be shown to the user in a alert window. You will have to do this on the pre of the send. In this case I don't think it will matter if its pre-validation or pre-operation.
Edit:
Yes, you should throw an InvalidPluginException even if no exception has happened in code. I accept this isnt what we would normally do, but its the way its meant to work. Msdn has more details: http://msdn.microsoft.com/en-us/library/gg334685.aspx
So for example the code would look like:
public void Execute(IServiceProvider serviceProvider)
{
try
{
//This is where we validate the email send
if(emailIsOkay)
{
//Do something
}
else if(emailIsNotOkay)
{
//Throw and exception that will stop the plugin and the message will be shown to the user (if its synchronous)
throw new InvalidPluginExecutionException("Hello user, your email is not correct!!");
}
}
catch (InvalidPluginExecutionException invalid)
{
//We dont to catch exception for InvalidPluginExecution, so just throw them on
throw;
}
catch (Exception ex)
{
//This exception catches if something goes wrong in the code, or some other process.
throw new InvalidPluginExecutionException(ex.Message, ex);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace SendEmail
{
public class Email : IPlugin
{
public void Execute(IServiceProvider serviceprovider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext));
if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity))
return;
//entity
Entity ent = (Entity)context.InputParameters["Target"];
if (ent.LogicalName != "entityName")//EntityName
throw new InvalidPluginExecutionException("Not a Service Request record! ");
//service
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService _service = serviceFactory.CreateOrganizationService(context.UserId);
string Email="";
if (ent.Contains("emailidfiled"))
Email = (string)ent["emailidfiled"];
#region email template
QueryExpression query = new QueryExpression()
{
EntityName = "template",
Criteria = new FilterExpression(LogicalOperator.And),
ColumnSet = new ColumnSet(true)
};
query.Criteria.AddCondition("title", ConditionOperator.Equal, "templateName");
EntityCollection _coll = _service.RetrieveMultiple(query);
if (_coll.Entities.Count == 0)
throw new InvalidPluginExecutionException("Unable to find the template!");
if (_coll.Entities.Count > 1)
throw new InvalidPluginExecutionException("More than one template found!");
var subjectTemplate = "";
if (_coll[0].Contains("subject"))
{
subjectTemplate = GetDataFromXml(_coll[0]["subject"].ToString(), "match");
}
var bodyTemplate = "";
if (_coll[0].Contains("body"))
{
bodyTemplate = GetDataFromXml(_coll[0]["body"].ToString(), "match");
}
#endregion
#region email prep
Entity email = new Entity("email");
Entity entTo = new Entity("activityparty");
entTo["addressused"] =Email;
Entity entFrom = new Entity("activityparty");
entFrom["partyid"] = "admin#admin.com";
email["to"] = new Entity[] { entTo };
email["from"] = new Entity[] { entFrom };
email["regardingobjectid"] = new EntityReference(ent.LogicalName, ent.Id);
email["subject"] = subjectTemplate;
email["description"] = bodyTemplate;
#endregion
#region email creation & sending
try
{
var emailid = _service.Create(email);
SendEmailRequest req = new SendEmailRequest();
req.EmailId = emailid;
req.IssueSend = true;
GetTrackingTokenEmailRequest wod_GetTrackingTokenEmailRequest = new GetTrackingTokenEmailRequest();
GetTrackingTokenEmailResponse wod_GetTrackingTokenEmailResponse = (GetTrackingTokenEmailResponse)
_service.Execute(wod_GetTrackingTokenEmailRequest);
req.TrackingToken = wod_GetTrackingTokenEmailResponse.TrackingToken;
_service.Execute(req);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("Email can't be saved / sent." + Environment.NewLine + "Details: " + ex.Message);
}
#endregion
}
private static string GetDataFromXml(string value, string attributeName)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
XDocument document = XDocument.Parse(value);
// get the Element with the attribute name specified
XElement element = document.Descendants().Where(ele => ele.Attributes().Any(attr => attr.Name == attributeName)).FirstOrDefault();
return element == null ? string.Empty : element.Value;
}
}
}