Hello guys
I just started witht programming plugins for our crm and I am trying to create a plugin which should send an Email when a record is beeing updated.
I dont rly know how to add an activity id. Please help me
This is my code:
namespace Hardware_Email_On_Update
{
public class Hardware_Email_on_Update_Pre_OP : IPlugin
{
#region Secure/Unsecure Configuration Setup
private string _secureConfig = null;
private string _unsecureConfig = null;
public Hardware_Email_on_Update_Pre_OP(string unsecureConfig, string secureConfig)
{
_secureConfig = secureConfig;
_unsecureConfig = unsecureConfig;
}
#endregion
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
try
{
//Die Hardware Entity ansprechen
Entity entity = (Entity)context.InputParameters["Target"];
//Alle Attribute holen
ColumnSet gesuchteAttribute = new ColumnSet(new string[] { "nov_f_aktuelleruser", "nov_name", "nov_f_hardware_os" });
//Die Guid von Hardware
Guid entity_Guid = entity.Id;
//Entity mit Informationen füllen
entity = service.Retrieve(entity.LogicalName,entity_Guid,gesuchteAttribute);
string ausgabe = entity["nov_f_aktuelleruser"].ToString();
updateEmailversenden(service,context,entity);
}
catch (Exception e)
{
throw new InvalidPluginExecutionException("=(");
}
}
/// <summary>
/// Diese Methode erstellt eine Email Entity und versendet sie mittels EmailRequest
/// </summary>
/// <param name="service"></param>
/// <param name="context"></param>
/// <param name="entity"></param>
public void updateEmailversenden(IOrganizationService service, IPluginExecutionContext context, Entity entity)
{
Entity email = new Entity("email"); //Das Email Obcekt
Entity fromParty = new Entity("activityparty"); //Der Absender
Entity toParty = new Entity("activityparty"); //Der Empfänger
toParty["partyid"] = new EntityReference("systemuser", context.UserId); //Hier kann man einen beliebigen CRM User einfügen mittels GUID
fromParty["partyid"] = new EntityReference("systemuser", context.UserId);
email["from"] = new Entity[] { fromParty }; //Referenz der Email übergeben (Absender)
email["to"] = new Entity[] { toParty };//Referenz der Email übergeben (Empfänger)
email["subject"] = "email subject - " + DateTime.Now.ToString();//Email Betreff
email["description"] = "email description"; //Vermute hier der Emailtext
email["regardingobjectid"] = new EntityReference("hardware", entity.Id);// Email mit GUID versehen
Guid emailId = service.Create(email); <------------ THE EXCEPTION FIRES HERE since it doesnt generate a GUID (look in the Exception)
SendEmailRequest sendEmailreq = new SendEmailRequest
{
EmailId = emailId,
TrackingToken = "",
IssueSend = true
};
SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);
}
}
}
Detail: <OrganizationServiceFault xmlns="schemas.microsoft.com/.../Contracts" xmlns:i="www.w3.org/.../XMLSchema-instance">
<ActivityId>00000000-0000-0000-0000-000000000000</ActivityId>
<ErrorCode>-2147220970</ErrorCode>
<ErrorDetails xmlns:a="schemas.datacontract.org/.../System.Collections.Generic">
<KeyValuePairOfstringanyType>
<a:key>CallStack</a:key>
<a:value i:type="b:string" xmlns:b="www.w3.org/.../XMLSchema"> bei Hardware_Email_On_Update.Hardware_Email_on_Update_Pre_OP.Execute(IServiceProvider serviceProvider)
bei PluginProfiler.Library.PluginAppDomainProxy.ExecuteCore(Stopwatch watch, ProfilerExecutionReport report, Object instance, Object executionParameter)
bei PluginProfiler.Library.AppDomainProxy.Execute(ProfilerExecutionConfiguration configuration, ProfilerExecutionReport report)</a:value>
</KeyValuePairOfstringanyType>
</ErrorDetails>
<Message>Klappt nicht</Message>
<Timestamp>2018-04-11T09:14:55.9198986Z</Timestamp>
<ExceptionSource i:nil="true" />
<InnerFault i:nil="true" />
<OriginalException i:nil="true" />
<TraceText i:nil="true" />
</OrganizationServiceFault>
*This post is locked for comments