locked
Attach pdf to existing crm email record RRS feed

  • Question

  • I created a custom workflow for adding a report in pdf as an attachment to an email.

    The code for creating the email message is however also in code.

    How can I refer to the existing crm email record of my workflow instead of hardcoding the e-mail message ?

     


    nour din

    Tuesday, April 8, 2014 12:13 PM

Answers

  • Hi Nour,

    as Jason rightly suggested above you need to include email as input parameter as shown below..

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


    once you have above input parameter in your execute method you need to use this email id retrieve email and use that email (refer pseudo code for this mentioned below)

                         //get the execution context

                    IExecutionContext context = executionContext.GetExtension<IExecutionContext>();

                    //get tracing service
                    ITracingService tracer = executionContext.GetExtension<ITracingService>();


                    IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

                    //get crm Service
                    IOrganizationService crmService = serviceFactory.CreateOrganizationService(context.UserId);


                    ////Create Email
             

          Entity createdEmail = RetrieveEntityFromId(crmService, emailId.Get(executionContext).Id.ToString(), "email");

    ----------------------------------------------------------------------------------------------------------------------------------------------------------

      /// <summary>
            /// Retrieves the appointment objects
            /// </summary>
            /// <param name="myService">The CRM Service</param>
            /// <param name="searchId">The Search Id String</param>
            /// <returns>appointment Object</returns>
            public static Entity RetrieveEntityFromId(IOrganizationService myService, string searchId, string entityType)
            {

                try
                {
                    Guid Id;

                    Id = new Guid(searchId);

                    // Create the column set object that indicates the fields to be retrieved.
                    ColumnSet attributes = new ColumnSet();
                    attributes.AllColumns = true;

                    // Retrieve the appointment
                    Entity myEntity = (Entity)myService.Retrieve(entityType, Id, attributes);


                    return myEntity;
                }
                catch (Exception ex)
                {
                    throw (ex);
                }

            }


    Hope this helps..


    MayankP
    My Blog
    Follow Me on Twitter

    • Marked as answer by nour din Thursday, April 10, 2014 12:15 PM
    Tuesday, April 8, 2014 2:28 PM
    Answerer

All replies

  • Make your custom workflow activity accept an email as an input parameter. That way you can create and edit the email it the workflow editor but send the reference to the custom code to add the attachment. 

    http://msdn.microsoft.com/en-us/library/gg327842.aspx#AddingInput


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Tuesday, April 8, 2014 12:54 PM
    Moderator
  • Do you have a sample where I have to put this in my code?

    I am new to custom workflow activity


    nour din

    Tuesday, April 8, 2014 1:21 PM
  • Hi Nour,

    as Jason rightly suggested above you need to include email as input parameter as shown below..

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


    once you have above input parameter in your execute method you need to use this email id retrieve email and use that email (refer pseudo code for this mentioned below)

                         //get the execution context

                    IExecutionContext context = executionContext.GetExtension<IExecutionContext>();

                    //get tracing service
                    ITracingService tracer = executionContext.GetExtension<ITracingService>();


                    IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

                    //get crm Service
                    IOrganizationService crmService = serviceFactory.CreateOrganizationService(context.UserId);


                    ////Create Email
             

          Entity createdEmail = RetrieveEntityFromId(crmService, emailId.Get(executionContext).Id.ToString(), "email");

    ----------------------------------------------------------------------------------------------------------------------------------------------------------

      /// <summary>
            /// Retrieves the appointment objects
            /// </summary>
            /// <param name="myService">The CRM Service</param>
            /// <param name="searchId">The Search Id String</param>
            /// <returns>appointment Object</returns>
            public static Entity RetrieveEntityFromId(IOrganizationService myService, string searchId, string entityType)
            {

                try
                {
                    Guid Id;

                    Id = new Guid(searchId);

                    // Create the column set object that indicates the fields to be retrieved.
                    ColumnSet attributes = new ColumnSet();
                    attributes.AllColumns = true;

                    // Retrieve the appointment
                    Entity myEntity = (Entity)myService.Retrieve(entityType, Id, attributes);


                    return myEntity;
                }
                catch (Exception ex)
                {
                    throw (ex);
                }

            }


    Hope this helps..


    MayankP
    My Blog
    Follow Me on Twitter

    • Marked as answer by nour din Thursday, April 10, 2014 12:15 PM
    Tuesday, April 8, 2014 2:28 PM
    Answerer
  • Hello,

    When I use this code then in the workflow I have to choose an existing (already sent email).

    However I want to use the email just created in the workflow where I want to add the attachment.

    What changes do I have to make ? 

    So first I create a workflow.

    I add a new mail message to this workflow with variables from the entity.

    I add some fixed attachments

    Then I want to add an attachment based on results from a report (in pdf) -> code I already have.

     


    nour din

    Tuesday, April 8, 2014 6:43 PM
  • The code provided by MayankP worked for me.

    Instead of selecten create record and then choose email and every time choose send email.

    This is the reason why I couldn't refer to the email created in the step above.

     


    nour din

    Thursday, April 10, 2014 12:17 PM