I am trying to build an email in a custom workflow activity in CRM 2011
However when I run the workflow I get this error message:
System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Cannot create activity party: either partyid or addressused field should be present (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).
My code is below:
using System; using System.IO; using System.Text; using System.ServiceModel; using System.ServiceModel.Description; using System.Xml.Serialization; using System.Activities; using System.Collections.Generic; using System.Linq; using System.Diagnostics; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Discovery; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Crm.Sdk; using Microsoft.Crm.Sdk.Messages; using System.Globalization; using System.Net; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Web.Services.Protocols; namespace AutoSubmitOrderEmail { public class SendEmail : CodeActivity { protected override void Execute(CodeActivityContext executionContext) { try { //Create the IWorkflowContext and the IOrganizationService for communication with CRM // Create the tracing service //ITracingService tracingService = executionContext.GetExtension<ITracingService>(); IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); // Get the target entity from the context //Entity target = (Entity)context.InputParameters["Target"]; EntityReference quote = myQuote.Get<EntityReference>(executionContext); // Access the input parameter declared above Guid QuoteId = new Guid(); QuoteId = myQuote.Get(executionContext).Id; Write("TEST_1 QuoteId=" + QuoteId.ToString()); // Access the input parameter declared above Guid CustomerPrimaryContact = Guid.Empty;
Guid AEId = new Guid(); AEId = myQuote.Get(executionContext).Id; Write("TEST_2 AEId=" + AEId.ToString()); Guid CSEId = new Guid(); CSEId = myQuote.Get(executionContext).Id; Write("TEST_3 CSEId=" + CSEId.ToString()); Guid IAMId = new Guid(); IAMId = myQuote.Get(executionContext).Id; Write("TEST_4 IAMId=" + IAMId.ToString()); try { //Execute Stored Proc using (SqlConnection oConn = new SqlConnection(connectionString)) { oConn.Open(); //Delete Commit link SQL statement using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = oConn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "<storedproc here>"; cmd.Parameters.AddWithValue("@QuoteId", QuoteId); using (SqlDataReader reader = cmd.ExecuteReader()) { if (reader != null) { int intCounter = 0; while (reader.Read()) { if (intCounter == 0) { GPOrderNumber = reader["GTRI_GPOrderNumber"].ToString(); CustomerIdName = reader["CustomerIdName"].ToString(); CustomerPrimaryContact = (Guid)reader["GTRI_CustomerPrimaryContactId"]; ConfirmationContact1 = reader["gtri_ConfirmationContact1"].ToString(); ConfirmationContact2 = reader["gtri_ConfirmationContact2"].ToString(); ConfirmationContact3 = reader["gtri_ConfirmationContact3"].ToString(); } intCounter++; //end of rea } reader.Close(); } } } oConn.Close(); } } catch (Exception ex) { Write(ex); } //From user crm //Create Email Entity email = new Entity("email"); //Build From: email Entity fromParty = new Entity("activityparty"); fromParty.Attributes.Add("partyid", new EntityReference("quote", IAMId)); Write("TEST_6 fromParty=" + fromParty.ToString()); //Buid To: email //Create the "TO:" activity party for the email Entity toParty = new Entity("activityparty"); toParty["partyid1"] = new EntityReference("quote", CustomerPrimaryContact); //toParty["partyid3"] = new EntityReference("quote", toPartyEmailConfirmationContact1); //toParty["partyid4"] = new EntityReference("quote", toPartyEmailConfirmationContact2); //toParty["partyid5"] = new EntityReference("quote", toPartyEmailConfirmationContact3); Write("TEST_7 toParty=" + toParty.ToString() + CustomerPrimaryContact.ToString()); //Build CC: email Entity ccParty = new Entity("activityparty"); ccParty["partyid1"] = new EntityReference("quote", AEId); ccParty["partyid2"] = new EntityReference("quote", CSEId); Write("TEST_8 ccParty=" + ccParty.ToString()); //Build Body: email //String emailBody = TextBody.Get<String>(executionContext); //emailBody["partyid1"] = new EntityReference("quote", QuoteNumber); //Build Email: email email["from"] = new Entity[] { fromParty }; email["to"] = new Entity[] { toParty }; email["cc"] = new Entity[] { ccParty }; email["regardingobjectid"] = new EntityReference("quote", quote.Id); email["directioncode"] = true; email["subject"] = "Your order has been placed."; //Build body: email //email["body"] = "Quote: " + QuoteNumber; //email["body"] = "Est. Ship Date: " + EstShipDate; //email["body"] = "GTRI Order Number: " + GTRINumber; //email["body"] = "GP Order Number : " + GPOrderNumber; //email["body"] = "GTRI Number: " + GTRINumber; //email["body"] = "Serial Number: " + SerialNumber; //email["body"] = "Tracking Number: " + TrackingNumbers; //email["body"] = "Maintenance Number: " + MaintenanceNumber; //email["body"] = "Purchase Order: " + PurchaseOrderId; //Write("TEST_9 EmailBody=" + QuoteNumber.ToString() + EstShipDate.ToString() + GTRINumber.ToString() + GPOrderNumber.ToString() + GTRINumber.ToString() // + SerialNumber.ToString() + TrackingNumbers.ToString() + MaintenanceNumber.ToString() + PurchaseOrderId.ToString()); Guid emailId = service.Create(email); //Send email
SendEmailRequestreqSendEmail = newSendEmailRequest();
reqSendEmail.EmailId = emailId;
//ID of created mail
reqSendEmail.TrackingToken = "";
reqSendEmail.IssueSend =
true;
SendEmailResponseres = (SendEmailResponse)service.Execute(reqSendEmail)
} catch(Exception ex) { Write(ex); } } #region CRMInput // Initialize the input parameter [Input("Purchase Order")] [ReferenceTarget("gtri_purchaseorder")] public InArgument<EntityReference> purchaseOrderReference { get; set; } // Initialize the input parameter [Input("Account Executive")] [ReferenceTarget("systemuser")] public InArgument<EntityReference> accountExecutiveReference { get; set; } [Input("Text Body")] public InArgument<string> TextBody { get; set; } [Input("Quote")] [ReferenceTarget("quote")] public InArgument<EntityReference> myQuote { get; set; } #endregion } }
newjeep