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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Plugin, Fetch XML retrieve GUID

(0) ShareShare
ReportReport
Posted on by

Hello,

I am writing a plugin to send an email containing multiple record links in the body to be sent to the owner of the records.  My question is:

Can I use a fetch xml query string to return the records (campaign repsonses)'s GUIDs?  I need to gather all the records that were created in the last 2 days and send the link in a email, so I will need to create the URL, but I need the GUIDs to do that. 


Does anyone know how I could accomplish this?

Thanks,


Devin

*This post is locked for comments

I have the same question (0)
  • Royal King Profile Picture
    27,686 on at

    following code should  generate hyperlink for all campaigns created last 2 days in plugin

    private static string UrlFormat = @"{0}/main.aspx?etn={1}&pagetype=entityrecord&id=%7B{2}%7D";

           private static string HyperLinkFormat = @"<a href='{0}'>{1}</a>";

           public void Execute(IServiceProvider serviceProvider)

           {

               var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               var service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(context.InitiatingUserId);

               var fetchXml = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\"> " +

    "  <entity name=\"campaignresponse\"> " +

    "    <attribute name=\"subject\" /> " +

    "    <attribute name=\"activityid\" /> " +

    "    <attribute name=\"regardingobjectid\" /> " +

    "    <attribute name=\"responsecode\" /> " +

    "    <attribute name=\"description\" /> " +

    "    <order attribute=\"subject\" descending=\"false\" /> " +

    "    <filter type=\"and\"> " +

    "      <condition attribute=\"createdon\" operator=\"last-x-days\" value=\"2\" /> " +

    "    </filter> " +

    "  </entity> " +

    "</fetch> ";

               var response = service.RetrieveMultiple(new FetchExpression(fetchXml));

               var urlLinks = new StringBuilder();

               foreach (var entity in response.Entities)

               {

                   string url = string.Format(CultureInfo.InvariantCulture, UrlFormat,

                                @"https://org.crm.dynamics.com&quot;,   // base url like  https://org.crm.dynamics.com

                                entity.LogicalName,  //lead

                               entity.Id);  //leadid

                   string htmlHyperLink = string.Format(CultureInfo.InvariantCulture, HyperLinkFormat,

                                  url,

                                  "Campaign Link");

                   urlLinks.Append(htmlHyperLink).AppendLine();

               }

           }

  • Community Member Profile Picture
    on at

    Hi Chitra,

    I think this will work.  When the plugin fires it should gather all Campaign Responses that were created in the last 2 days, and then send an email to each unique owner of each Campaign Response.  IE If 12 responses were entered and there were 5 the belonging to me and 7 belonging to you, we each would receive one email containing all a list of records that each of us own.  I would receive one with the 5 urls, and you would receive an email with the 7 urls to the responses that that belong to you.

    I think you wrote code that would work for Campaigns?

    Let me know what you think,

    Thanks,

    Devin

  • Royal King Profile Picture
    27,686 on at

    I'm not sure how you are going trigger this plugin but here is the updated code to generate campaign response URL and adding them to dictinary. All you have to do create and send email for all the items from the dictionary

    private static string UrlFormat = @"{0}/main.aspx?etn={1}&pagetype=entityrecord&id=%7B{2}%7D";

           private static string HyperLinkFormat = @"<a href='{0}'>{1}</a>";

          public void Execute(IServiceProvider serviceProvider)

          {

              var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

              var service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(context.InitiatingUserId);

              var fetchXml = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\"> " +

                               "  <entity name=\"campaignresponse\"> " +

                               "    <attribute name=\"subject\" /> " +

                               "    <attribute name=\"activityid\" /> " +

                               "    <attribute name=\"ownerid\" /> " +

                               "    <attribute name=\"regardingobjectid\" /> " +

                               "    <attribute name=\"responsecode\" /> " +

                               "    <attribute name=\"description\" /> " +

                               "    <order attribute=\"subject\" descending=\"false\" /> " +

                               "    <filter type=\"and\"> " +

                               "      <condition attribute=\"createdon\" operator=\"last-x-days\" value=\"2\" /> " +

                               "    </filter> " +

                               "  </entity> " +

                               "</fetch> ";

              var response = service.RetrieveMultiple(new FetchExpression(fetchXml));

              var OwerWithCampaignResUrl = new Dictionary<Guid,StringBuilder>();          

              foreach (var entity in response.Entities)

              {              

                  string url = string.Format(CultureInfo.InvariantCulture, UrlFormat,

                               @"https://org.crm.dynamics.com&quot;",   // base url like  https://org.crm.dynamics.com

                               entity.LogicalName,  //lead

                              entity.Id);  //leadid

                  string htmlHyperLink = string.Format(CultureInfo.InvariantCulture, HyperLinkFormat,

                                 url,

                                 "Campaign Link");

                  urlLinks.Append(htmlHyperLink).AppendLine();

                  if (OwerWithCampaignResUrl.ContainsKey(entity.Id))

                   {

                       OwerWithCampaignResUrl[entity.Id].Append(htmlHyperLink).AppendLine();

                   }

                   else

                   {

                       OwerWithCampaignResUrl.Add(entity.Id, new StringBuilder(htmlHyperLink));

                       OwerWithCampaignResUrl[entity.Id].AppendLine();

                   }

              }

              foreach (var item in OwerWithCampaignResUrl)

           {

                  //Create email and send it to each owner

           }

          }

  • Suggested answer
    NatrajY Profile Picture
    3,040 on at

    Based on you requirement, I don't believe that this email needs to be sent straight away as this seems some sort of periodic report to business users. You could probably do this using a SSRS report. SSRS would give you greater control over the format as well as the data, so that if you have any changes down the line to the format or query, you don't need to rebuild and redeploy the assembly.

    You can setup a report subcription to deliver the report to some recipients, if the delivery recipients is a static list, that is not based on some condition. If you need some sort of intelligence behind the report delivery, you can couple this with a scheduled workflow of some sort, run the report and attach the output as a HTML or PDF. You would still get the hyperlink to record capability with either of these options.

    The easiest would be Report->Subscription->MHTML->Email Body, depending on your requirements.

  • Community Member Profile Picture
    on at

    Hi Chitra,

    In the code that you provided, how does it add the owner to the dictionary?  I wasn't able to identify in the code..

    Thanks

    Devin

  • Royal King Profile Picture
    27,686 on at

    Check the highlighted code where it gets the owner of the record.

    OwerWithCampaignResUrl[etity.GetAttributeValue<EntityReference>("ownerid").Id].Append(htmlHyperLink).AppendLine();

  • suresh maurya Profile Picture
    630 on at

    For testing of FetchXML, you can test here online http://msxrmtools.com

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

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
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Liquid error: parsing "/globalsearch/?q=star+health+insurance++24+months+waiting+period+health+issue++name+" - Nested quantifier +.