Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Retrieve attachments from Quote and send it through Email : Plugin

Posted on by Microsoft Employee

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve attachments from Quote and send it through Email : Plugin

    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
    Mahendar Pal 45,089 on at
    RE: Retrieve attachments from Quote and send it through Email : Plugin

    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
    Rajkumar Rajaraman 18,108 on at
    RE: Retrieve attachments from Quote and send it through Email : Plugin

    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
    anilambadan 1,160 on at
    RE: Retrieve attachments from Quote and send it through Email : Plugin

    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

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,134 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,928 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans