Hello,
I need to retrieve the attachments from notes in the quote entity and attach it to a email and send it through an email using plugin. Also Should it be on postquoteUpdate ??
need help.
Thanks.
*This post is locked for comments
Updated with Code. When an attachment is attached on quote i want that to be attached to email as well, i dont want it to be downloaded to my machine
Entity entity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
if (entity.LogicalName != "quote")
return;
try
{
Quote quote = entity.ToEntity<Quote>();
if (quote.Attributes.Contains("ownerid") && quote.Attributes["ownerid"] != null)
{
ownerId = ((EntityReference)quote.Attributes["ownerid"]).Id;
}
localContext.Trace("ownerid" + ownerId);
if (quote.Attributes.Contains("customerid") && quote.Attributes["customerid"] != null)
{
custId = ((EntityReference)quote.Attributes["customerid"]).Id;
}
localContext.Trace("custid" + custId);
Entity fromParty = new Entity("activityparty");
fromParty["partyid"] = new EntityReference("systemuser", ownerId);
Entity toParty = new Entity("activityparty");
toParty["partyid"] = new EntityReference("account", custId);
Entity Email = new Entity("email");
Email.Attributes["from"] = new Entity[] { fromParty };
Email.Attributes["to"] = new Entity[] { toParty };
Email.Attributes["subject"] = "Annual Sales Report";
Email.Attributes["regardingobjectid"] = new EntityReference("quote", quote.Id);
Email.Attributes["ownerid"] = new EntityReference("systemuser", ownerId);
localContext.Trace("Creating email records" );
Guid EmailId = localContext.OrganizationService.Create(Email);
localContext.Trace("EmailId =" + EmailId);
QueryExpression queryExpressionForAnnotation = new QueryExpression()
{
EntityName = Annotation.EntityLogicalName,
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression{
AttributeName = "ObjectId",
Operator = ConditionOperator.Equal,
Values ={quote.Id}
},
new ConditionExpression{
AttributeName = "statecode",
Operator = ConditionOperator.Equal,
Values ={"Active"}
}
}
},
TopCount = 1,
Orders ={
new OrderExpression{
AttributeName = "createdon",
OrderType = OrderType.Descending
}
}
};
localContext.Trace("Annnotation records " );
List<Entity> Annotationcollection = localContext.OrganizationService.RetrieveMultiple(queryExpressionForAnnotation).Entities.ToList();
Annotationcollection.ForEach(a =>
{
Annotation annotation = a.ToEntity<Annotation>();
ActivityMimeAttachment ama = new ActivityMimeAttachment();
ama.Subject = annotation.Subject;
string filename = "Report.pdf";
ama.FileName = filename;
byte[] filestream = new byte[] { };
ama.Body = Convert.ToBase64String(filestream);
ama.MimeType = "text/plain";
ama.AttachmentNumber = 1;
ama.ObjectId = new EntityReference(Email.LogicalName, EmailId);
localContext.Trace("creating Attachments");
Guid? guid = localContext.OrganizationService.Create(ama);
localContext.Trace("Attachment"+ama);
});
SendEmailRequest req = new SendEmailRequest();
req.EmailId = EmailId;
req.IssueSend = true;
req.TrackingToken = "";
SendEmailResponse res = (SendEmailResponse)localContext.OrganizationService.Execute(req);
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("error", ex);
}
//error
[Microsoft.Xrm.Sdk.Workflow: Microsoft.Xrm.Sdk.Workflow.Activities.UpdateEntity]
[UpdateStep5]
Able to trace till 'Creating email records'
after that its failing.
When you want to do it ? at the time of attachment is done ?? then you need to write a plugin on annotation (notes) and need to check for regardingobjectid if it is related to quote then you can download and send email with attachment.
Hello,
Refer the following links:
mahenderpal.wordpress.com/.../sending-email-with-entity-attachment-from-notes
community.dynamics.com/.../how-to-download-the-attachment-of-a-note-within-a-plugin-in-dynamics-crm
Hope this helps.
Regards,
R.Rajkumar
"Please mark my answer as verified if you found it helpful"
Please refer the following link it will give you the idea for downloading the attachments
msdn.microsoft.com/.../gg328429.aspx
thanks
Anil
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,134 Super User 2024 Season 2
Martin Dráb 229,928 Most Valuable Professional
nmaenpaa 101,156