Showing posts with label Client Side. Show all posts
Showing posts with label Client Side. Show all posts

Sunday, 27 August 2017

How to send data in excel along with an email in CRM using C#

How to send data in excel along with an email in CRM using C#

In the previous post i discussed how can we convert a datatable into a csv file using c#.

Often in Dynamics crm we face the requirement that customer wants some data in excel attached along with the email on some regular interval or so. To do so, the first option looks like we paste the result into email body and send it as email to customer. But that doesn't looks good as user can't do much with email body what he can do if he receive that data in an excel sheet. What if we can send him data into excel attached with the email?? But the question arise, do we need to have office on the server running code?? The latest answer is: NO. Because you can create a csv file in c# and attach it along with a crm email and send it to concerned person.

For ex: Customer wants that he should get an email daily of having the number of cases created on the same day as per their origin.

Like:
Originated from    Count
Phone call             200
Email                    120
Chat                       80
Web                       400

So to send such kind of data into excel what we can do is, we can extract the data into a datatable using some C# code from their crm application and convert the datatable into CSV as explained in previous post.   Once you get the comma separated value as result from datatable, you can then create an email record into crm and pass the guid of email record along with csv result.

Below function will attach the csv string in the form of a csv with that email which can be opened easily in excel.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   //Function to  add csv message as attachment with the email record of passed guid
        public static void addAttachmenttoEmail(Guid emailguid, String message, IOrganizationService service)
        {
            Entity attachment = new Entity("activitymimeattachment");
            attachment["subject"] = "SampleCSV";
            string fileName = "Sample.csv";
            attachment["filename"] = fileName;
            byte[] fileStream = Encoding.ASCII.GetBytes(message);

            attachment["body"] = Convert.ToBase64String(fileStream);
            attachment["mimetype"] = "text/plain";
            attachment["attachmentnumber"] = 1;
            attachment["objectid"] = new EntityReference("email", emailguid);
            attachment["objecttypecode"] = "email";
            service.Create(attachment);
        }
     


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Don't forget the send that email with which we just attached the csv string.

Happy Learning...!!

Sunday, 13 November 2016

How to enable Feedback in Dynamics crm 2016

How to manage Feedback in Dynamics crm 2016

To improve customer satisfaction, tracking customer feedback for the products and services that your organization offers is very important. You can now add feedback or ratings to records for any entity that is enabled for feedback.
For example, if the Case entity is enabled for feedback, you can capture feedback on the support experience the customer received for the case. When several customers are rating a record, the ratings can be consolidated for each record through a custom rollup field. In a sales scenario, you can enable the Product entity for feedback to capture users' feedback on the products you sell.
Here, we;ll learn how to enable feedback for an entity and how to get the collaborated rating from multiple feedbacks of a record.
Important
This feature was introduced in CRM Online 2016 Update 1 and in CRM 2016 SP1 (on-premises).
We'll take case entity in our example and learn to enable entity and capturing average feedback rating from multiple feedbacks in case itself with the help of a roll-up field in below step by step procedure:
Step 1: Navigate to setting->Customization->Customize the system as shown in image below:

Step 2: Click on entity of your choice on which you want to enable feedback. In our case it's case entity. 
In Communication and collaboration check the feedback checkbox to enable feedback for case.
Note: Once enabled for an entity it can't be disabled again.


Step 3: Now save and publish the entity.

This is how feedback is simply enabled for an entity.

Let's now see how we can insert record in feedback of a case:

Step 1: Open the related records of case by clicking as below:

Step 2: A related feedback grid appears. Click on new feedback to add new feedback for this case as shown in below image:


Step 3: Create a new feedback record and set regarding field to the above case manually to attach the feedback to this case:

This  is how you can add a feedback record for a record.

If you have observed, in the above procedure we have to manually set  the regarding field to the case and this in not much convenient to navigate to feedback first and then add a new feedback.

To overcome this you can add a subgrid of feedback over case form itself so that user can directly add feedback from case record itself and there  isn't any need of setting regarding field separately as system itself create feedback record related to case:

Below is the step by step procedure of adding a subgrid on a form:

  1. Go to Settings > Customizations.
  2. Choose Customize the System.
  3. Under Components, expand Entities, and then expand the entity you've enabled for feedback.
  4. Click Forms.
  5. Open the form of type Main or Main - Interactive experience.
    The Main - Interactive experience form is used in the interactive service hub.
  6. Select the section you want to insert the subgrid in, and on the Insert tab, in the Control group, click Sub-Grid.
  7. In the Set Properties dialog box, fill in the name and label for the subgrid.
  8. In the Data Source section, select the information:
    • Records. Select Only Related Records.
    • Entity. Select Feedback (Regarding).
    • Default View. Select a default view for the list.
9. Click save and publish to make change effective.



You might want to get the average collaborated rating given by customers for a given case through feedback records on case record.
This can be easily done with the help of roll-up field.
Below is the step by step procedure to create a rollup field to calculate the average rating for a case:

Step 1:  Add a new field on case entity as shown in image below.
Datatype of field should be whole number and make it of type rollup and click edit to set an aggregation formula:

Step 2:  Set the properties as shown below:

Step 3: Save the field and place the field on the place of your choice in case form and publish the case form.

Step 4:  Let's say there are 3 feedback records for a case with ratings 3.4 and 5 as shown in below image:



Step 5:
Calculating average rating: 

This was how you can enable and configure feedback in your system for any entity.

Hope this post was helpful.

Keep following for more updates...!!

Monday, 7 November 2016

Server side business Rules in MS Dynamics crm 2015 and later

Make the Business Rule execute over server side in Dynamics CRM 2015 and later
In CRM 2015, there is a very important update which can be considered for replacing many workflows or plugins – Executing Business Rules from Server Side code.

When Business Rule feature was introduced in CRM 2013, it was capable of executing from client side alone and with the 2015 update, we now be able to execute business rule from server side as well- which is a great feature

Hope it would be useful...!

Sunday, 23 October 2016

Show-Hide Section on the Basis of Security Role of Current User Using Javascript in MS Dynamics CRM

Show-Hide Section on the Basis of Security Role of Current User Using Javascript in MS Dynamics CRM
Show Fields on the Basis of Security Role of Current User Using Javascript in MS Dynamics CRM

Sometimes, we face such situation where we want to show some specific section to users having a specific role only.

Scenario: Let's say on case form you want to show a "Details" section under "general" tab only to the user having "CSR Manager" role.

Now,it can be achieved using Javascript easily using below code:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Function Show-Hide a section based on a particular Role
function ShowHideSectionAccordingToRole()
{

if(CheckCurrentUserRole("CSR Manager"))
{
//Show the section
ToggleSection("general","Details",true);
}
else 
{
//Hide the section
ToggleSection("general","Details",false);
}
}

//Function to Show-Hide a section in Specific Tab
//Parameters
//TabName - Name of the Tab in Which the Section Exist
//SectionName - Name of the Section you want to Show-Hide
//Flag - true to Show and false to hide section
function ToggleSection(TabName, SectionName, Flag)
{       
    Xrm.Page.ui.tabs.get(TabName).sections.get(SectionName).setVisible(Flag);
}


//Check login User has role passed through Parameter
//Parameter
//IsRole - Name of the Role you want current user to check for
//Return Type - Boolean, True if User have that Role ,False if user doesn't have that Role
function CheckCurrentUserRole(IsRole) {
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < currentUserRoles.length; i++) 
{
         var userRoleId = currentUserRoles[i];
         var userRoleName = GetRoleName(userRoleId);
         if (userRoleName == IsRole) 
   return true;
    }
    return false;
}

//Get Role-name based on RoleId
function GetRoleName(roleId) {
var serverUrl = Xrm.Page.context.getClientUrl();
var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "RoleSet?$filter=RoleId eq guid'" + roleId + "'";
    var roleName = null;
    $.ajax(
        {
            type: "GET",
            async: false,
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: odataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {
                roleName = data.d.results[0].Name;
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); }
        }
    );
    return roleName;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



Show Fields on the Basis of Security Role of Current User Using Javascript in MS Dynamics CRM

If you want to show/Hide or Lock/Unlock specific field instead of a whole section you can just replace the "ShowHideSectionAccordingToRole" function with the below function :

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ShowHideFieldAccordingToRole()
{

if(CheckCurrentUserRole("CSR Manager"))
{
//Show the Field
Xrm.Page.ui.controls.get("FieldName").setVisible(true);
               //To UnLock the field
              // Xrm.Page.getControl("FieldName").setDisabled(false); 
}
else 
{
//Hide the Field
Xrm.Page.ui.controls.get("FieldName").setVisible(false);
               //To lockLock the field
              // Xrm.Page.getControl("FieldName").setDisabled(true); 
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Some may argue that why would we use javascript to show/hide field when we have field security profiles.
I agree we have field security profiles which have much more feature to control over a field but here Javascript  is also an easy way to handle this.

I hope it would be helpful. 
Comments are highly appreciated.

Happy CRM...!!!

Monday, 12 September 2016

Open Specific Form Using javascript in Dynamics CRM

Open Specific Form of any entity Using javascript in Dynamics CRM


In this post we'll learn how we can open a specific type of form of an entity using javascript.

Scenario: Let's say you have 2 different main form of phone call entity. You want to open specific form on the basis of specific attribute on the form. 

Here in this case we have a Call Type option set. On the basis of which use wants to open different form. For instance vales of Call Type may be Sales/Service and user wants to open new form of sales of phone call when he selects sales in option set and want to open main form of service of phone call if he selects service, he can implement the same using javascript as below:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  function ChoosePage()
{
var CallType = Xrm.Page.getAttribute("new_calltype").getValue();
if(CallType!=null)
{
   var FormId;
if (CallType==1) //Sales
{
FormId='78958D5A-FE73-45E8-8F78-950B1389BE2E';
}
else if (CallType==2) //Service
{
   FormId='A91390A5-99BB-4D10-9EEE-3A5C87F841F1';  
var parameters = {};
parameters["formid"] = FormId;
parameters["new_calltype"] = CallType;
Xrm.Utility.openEntityForm("phonecall", null, parameters);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Here "null" signify that we are opening a new form.

If you want to open existing record in different form then you can pass Guid of that record.

* We can pass as many objects in parameter as we want which will come pre-set in the new form opened.

* The formid used in the code can be found in Customization->Entity->Forms

Hope it's helpful.


Suggestions and comments are highly appreciated.

Happy CRM...!