Hi, this is a continuation of another thread. I am trying to send an email once a custom entity is created. The plugin is registered on Create and Post. my code is below:
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity travelDetail = (Entity)context.InputParameters["Target"]; if (travelDetail.LogicalName != "gg_traveldetail") { return; } try { EntityReference sConRef = travelDetail.GetAttributeValue<EntityReference>("gg_consultantstaffingrole"); EntityReference sNurRef = travelDetail.GetAttributeValue<EntityReference>("gg_nursestaffingrole"); if (travelDetail.Attributes.Contains("gg_bookingcomplete")) { if (travelDetail.GetAttributeValue<bool>("gg_bookingcomplete")) { Guid userId = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId; Guid travelregardingobjectid = Guid.Empty; // new Guid(context.OutputParameters["id"].ToString()); string regardingobject = string.Empty;//"gg_traveldetail"; string emailSubject = "A travel detail has just been completed"; string emailBody = "Plesae refer to the completed travel that has just been completed by either a consultant or a nurse"; if (context.OutputParameters.Contains("id")) { travelregardingobjectid = new Guid(context.OutputParameters["id"].ToString()); regardingobject = "gg_traveldetail"; } if (sConRef != null) { //Entity image = (Entity)context.PreEntityImages["ImageDist"]; Entity conRef = service.Retrieve(sConRef.LogicalName, sConRef.Id, new ColumnSet(true)); EntityReference con = conRef.GetAttributeValue<EntityReference>("new_consultant"); Entity consultant = service.Retrieve(con.LogicalName, con.Id, new ColumnSet(new string[] { "new_name", "emailaddress" })); //Entity email = new Entity("email"); SendEmail(service, consultant, consultant.Id, userId, travelregardingobjectid, emailBody, emailSubject, regardingobject); }
The plugin throws the error when SendEmail() (the last line) method is called. Error: "gg_traveldetail with id xxxxxxxxx Does not exist". The id in this case is gotten from the context.OutputParemeter. The email method code is below
private void SendEmail (IOrganizationService service,Entity receiever, Guid recieverUserId, Guid senderUserId, Guid regardingObjectId, string emailBody, string emailSubject, string entity) { Entity email = new Entity(); email.LogicalName = "email"; //Set regarding object property (i.e. The entity record, which u want this email associated with) EntityReference regardingObject = new EntityReference(entity, regardingObjectId); email.Attributes.Add("regardingobjectid", regardingObject); Entity fromParty = new Entity("activityparty"); fromParty["addressused"] = "victor@co.uk"; EntityReference from = new EntityReference("systemuser", senderUserId); fromParty.Attributes.Add("partyid", from); //Derive to party Entity toParty = new Entity("activityparty"); toParty["addressused"] = "victor@co.uk"; EntityReference to = new EntityReference(receiever.LogicalName, recieverUserId); toParty.Attributes.Add("partyid", to); EntityCollection collFromParty = new EntityCollection(); collFromParty.EntityName = "systemuser"; collFromParty.Entities.Add(fromParty); EntityCollection collToParty = new EntityCollection(); collToParty.EntityName = "systemuser"; collToParty.Entities.Add(toParty); email.Attributes.Add("from", collFromParty); email.Attributes.Add("to", collToParty); //Set subject & body properties email.Attributes.Add("subject", emailSubject); email.Attributes.Add("description", emailBody); //Create email activity Guid emailID = service.Create(email); //Sending email SendEmailRequest reqSendEmail = new SendEmailRequest(); reqSendEmail.EmailId = emailID;//ID of created mail //reqSendEmail.TrackingToken = ""; reqSendEmail.IssueSend = true; SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail); } } }
Thanks Guys for helping.
*This post is locked for comments