Search code examples
c#dynamics-crm-2011dynamics-crmdynamics-crm-2013dynamics-crm-online

Getting Opportunity (Opty) id and Opportunity name using QueryExpression


I am working with Dynamics CRM 365. I retrieve Opty name and Opty id (unique) using queryexpression.

QueryExpression queryOnline = new QueryExpression("opportunity");
        queryOnline.Criteria = new FilterExpression();
        queryOnline.Criteria.AddCondition("createdon", ConditionOperator.LastXDays, hours);
        queryOnline.Criteria.AddCondition("statuscode", ConditionOperator.Equal, 1); // For Open Opportunity
        queryOnline.ColumnSet = new ColumnSet(true);

        EntityCollection entCol = _service.RetrieveMultiple(queryOnline);

        foreach (Entity presName in entCol.Entities)
{
//Get Opty ID and Name
}

Now i want to create SendEmailRequest to send Opportunity Records which i retrieve using above method. In short, i want to create SendEmailRequest with the following format:

In email description (Multiple Lines Of Text / String):

You have open opportunities:

ID || NAME

00001 || OPEN OPTY NAME

00002 || OPEN OPTY NAME

I have successfully create send email request, with or without template. But i have a problem when i want to paste opty records from query expression into email description. Where the email description is "multiple lines of text" field with String format.


Solution

  • The Email description field accepts HTML format, you should create an HTML email body and pass that to the description. for the opportunity values, change your ColumnSet to this:

    queryOnline.ColumnSet = new ColumnSet("name", "opportunityid");
    

    Then, your foreach could look something like this:

    string htmlDescription = "<ul>";
    foreach (Entity presName in entCol.Entities)
    {
      //Get Opty ID and Name
      htmlDescription += string.Format("<li>{0} || {1}</li>",  presName.Id, presName.GetAttributeValue<string>("name"));
    }
    
    htmlDescription += "</ul>";
    

    just a basic example of course.