web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieve attachments from Quote and send it through Email : Plugin

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Community Member Profile Picture
    on at

    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.

  • Mahendar Pal Profile Picture
    45,095 on at

    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.

  • Rajkumar Rajaraman Profile Picture
    on at

    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"

  • anilambadan Profile Picture
    1,160 on at

    Please refer the following link it will give you the idea for downloading the attachments

    msdn.microsoft.com/.../gg328429.aspx

    thanks

    Anil

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Shidin Haridas Profile Picture

Shidin Haridas 2

#2
Abdullah13 Profile Picture

Abdullah13 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans