Showing posts with label Dynamics CRM Custom Add-ons. Show all posts
Showing posts with label Dynamics CRM Custom Add-ons. Show all posts

Thursday, July 18, 2019

Dynamics - PDF Generator


In 2017, a third party provide a solution that use to generate PDF from CRM templates.














In recently, Dynamics 365 organization I have seen this solution as a part of Microsoft out-of-box solution list. And this solution is free.










Also Microsoft still not taken this feature as they done for Resco field service module.

So we can see all entity, web resource etc. solution component with third party prefix.


















We always need to send PDF to customer for quotation, Sales order or Invoices etc. In On-Premise CRM organization it was easy because we can use register plugin is none Isolation Mode and access other iText like assemblies.








But in Online Dynamics 365 it is not possible.

You can refer below link to get User Guide.

Working:-

Click on “Generate PDF Configuration” button top of solution lists and it will open “hcl_pdf_GeneratePDFConfigurator” web resource for us. We can configure/Enable list of Entity having document templates.
Form here we can remove the generate PDF feature for an entity and template whenever needed.


















After configuration is done, we can see a new button configured entity. However this button also visible on create form, which have no use until record is created.









Click on Generate PDF button it will open “hcl_pdf_GeneratePDF” web resource with some query string information from there we can generate our PDF and download or save in note section for selected document template. I have seen it worked of Word Templates.
























However I have not seen preview as showing in user guide. But download and add to note working fine.







We can also use this solution for sending email with PDF with some customization and configuration.

  1. Create a new workflow named. Ex-Customer Invoice.
  2. Create Email with all create information.
  3. Create custom workflow to get record (Invoice, Quote etc.) Guid.
public class GetRecordIDFromURL : CodeActivity
    {
        #region variable used
        [RequiredArgument]
        [Input("RecordURL")]
        public InArgument<string> RecordURL { get; set; }

        [RequiredArgument]
        [Output("RecordId")]
        public OutArgument<string> RecordId { get; set; }
        string traceMessage = string.Empty;
        #endregion
        protected override voidExecute(CodeActivityContext executionContext)
        {
            //Create the tracing service
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();
            //Create the context
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            traceMessage = "Workflow started.";
            tracingService.Trace(traceMessage);
            if (RecordURL.Get<string>(executionContext) != null)
            {
                RecordId.Set(executionContext, RecordURL.Get<string>(executionContext).Substring(RecordURL.Get<string>(executionContext).IndexOf("&id=")+4, 36));

            }
        }
      
    }
4. Call Action “Generate PDF Action”. It will return pdf data in  PDFBinaryData” parameter.
















5. Create custom workflow to add “activitymimeattachment” to email.
   var email = context.InputParameterOrDefault<EntityReference>("Email");
   var output = context.InputParameterOrDefault<String>("output");
  if (email!=null)//CreateEmailAttachment
                    {
                        Microsoft.Xrm.Sdk.Entity ActivityMimeAttachment = new Microsoft.Xrm.Sdk.Entity("activitymimeattachment");
                        ActivityMimeAttachment["subject"] = report.GetAttributeValue<string>("name");
                        ActivityMimeAttachment["filename"] = "Invoice.pdf";
                        ActivityMimeAttachment["body"] = output;
                        ActivityMimeAttachment["attachmentnumber"] = 1;
                        ActivityMimeAttachment["objectid"] = email;
                        ActivityMimeAttachment["objecttypecode"] = "email";
                        service.Create(ActivityMimeAttachment);
                    }

6. Create custom workflow to send that Email.
public class SendEmail: CodeActivity
    {
        private string traceMessage = string.Empty;

        [RequiredArgument]
        [Input("Email")]
        [ReferenceTarget("email")]
        public InArgument<EntityReference> Email
        {
            get;
            set;
        }

        #region Execute  function
        protected override voidExecute(CodeActivityContext executionContext)
        {
            //Create the tracing service
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();
            //Create the context
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            try
            {
                traceMessage = "Workflow started.";
                tracingService.Trace(traceMessage);
                if (Email.Get<EntityReference>(executionContext) != null)
                {
                    SendEmailRequest SendEmailRequest = newSendEmailRequest();
                    SendEmailRequest.EmailId=(Email.Get<EntityReference>(executionContext).Id);
                    SendEmailRequest.TrackingToken="";
                    SendEmailRequest.IssueSend=(true);
                    SendEmailResponse Response =(SendEmailResponse) service.Execute(SendEmailRequest);

                }
                else
                    traceMessage += " No Record found to update.";
            }
            catch (Exception ex)
            {
                tracingService.Trace(traceMessage);
                throw newInvalidPluginExecutionException("error occured in SendEmail workflow: " + ex.Message.ToString());
            }

        }
        #endregion
    }











Update action as per requirement.











Once this workflow will run an email with Invoice PDF will sent to customer.

You can also test this Action from XRMToolBox.

























In result you will see PDF content in binary format that can direct use in email or Note attachment.

Please share your feedback.

Tuesday, November 6, 2018

Dynamics CRM: Sent Email Template With Conditional Image


In Dynamics CRM we use Email activity a lot to send all kind of notification and approval.  CRM also provide Email Template that we can use to send customize email.
But some time we need to extend this capability.

Sample Case: - We need to send email to customer based on case origin type with below conditions.
  • We need to use embedded image. We cannot use global image hosted on public domain.
  • We need to use web resource images of CRM.
  • Need to use Email template.
  • Image need to change based on case origin value.


Solution: - After lot of research and try different solution. We conclude that we need to perform below task to achieve our requirement.
  • Need base64 string with html image tag to insert in email.
  • Need to create an email to set sender and receiver value.
  • We need email template information not to send just to get body and subject value.
  • Need to create our custom placeholder and replace these placeholder as per our requirement.

To achieve this we need some custom workflow activity for:
  • Convert web resource to base64 image string
  • Get email template subject and description at run time and set these values in our email
  • To replace our placeholders
  • Send Email

Steps: -
  1.  Create PNG Web resource to store images.







      2. Create an action to get web resource to base64 image string based on condition. Use Getbase64Image custom workflow.




























   3. Create placeholder in your Email template. I have use [url] as placeholder.

















4. Create Workflow to complete our requirement and send email
·         Call previously create action to get embedded image.
·         In next step create an email set to and from value.
·         In next step call update email from email template custom workflow. This workflow will use Instantiate Template Request because do not need to send this email template. We need it’s Subject and description value.

InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest
                {
                    TemplateId = EmailTemplate.Get(executionContext).Id,
                    ObjectId = Case.Get(executionContext).Id,
                    ObjectType = Case.Get(executionContext).LogicalName
                };
InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);
  EntityemailT = (Entity)instTemplateResp.EntityCollection.Entities[0];
Entity email = service.Retrieve(EmailSource.Get(executionContext).LogicalName, EmailSource.Get(executionContext).Id, new ColumnSet("description", "subject"));
  email.Attributes["description"] = emailT.Attributes.Contains("description") ? (String)emailT.Attributes["description"] : "";
email.Attributes["subject"] = emailT.Attributes.Contains("subject") ? (String)emailT.Attributes["subject"] : "";
service.Update(email);

 5.  Call Replace Placeholder workflow to update placeholder with embedded image.
 6.  Finally send this email. 

















I have not considered email Template with attachment. For Email Template with attachment you need to extend “update email from email template” custom workflow.

Result: -













I am sharing custom workflow assembly. Hope this will help other also.

Thursday, September 27, 2018

Dynamics CRM : Export Security Role


In CRM as a developer sometime we need to see permission given to Security role.
Dynamics CRM/365 we need to open security role to check permission in that security role.
But there is no provision to export Security role permission.

Case Study: -
Developer X is working in an organization as support engineer. He was not part of implementation team.
User requested role given in each security role permission in excel sheet.
It is time consuming job to open each security role and make excel sheet manually with all permission.

Solution: -
To tackle this case study, I have created a XrmToolBox Plugin which help Developer X.
User can see permission given in each security role to entity.
User can see Miscellaneous Privileges.
User can export all Privileges based on selected Security role.
Download XRMToolBox from given URL and install. https://www.xrmtoolbox.com/
Install “Export Security Role” Plugin in your XRMTollBox using plugin store.























If you are not able to find “Export Security Role in Xrmtoolbox.
Use below links
Download Plugin compatible with Xrmtoolbox Version 1.2019.7.34 from here:
Download 1.2019.11.15

Download Xrmtoolbox Version 1.2019.7.34 from here:
XrmToolbox v1.2019.7.34

And download plugin from here. Install in XRMToolBox by clicking Settings->Path->Storage folder->Plugin folder.
Copy assemblies in Plugin folder.









































Connect your organization and open plugin Export Security Role.













Click on “Retrieve Roles” and select your security Role.
Now you will see list of all security roles in role list.





Now when you select any security role it will show all Privileges for selected security role.

































If need to search specific entity enter logical name in search box.











Click on Exportbutton to export privileges in excel.
















As a result, you will see a excel file with Security role name is create with all privileges.





























Now Developer X will easily able to find and share permission based on security role.

Note: - Plugin based on .net framework 4.5.2. Microsoft office required to export result.

Follow my blog so you will not miss my new upcoming blogs.

Print Preview in Dynamics CE

After moving to new unified interface from legacy web interface. We are moving one important feature Print Preview.  With print Preview we w...