Contact Management from Dynamics 365 Portal – Part 2

Situation

  • The Portal Contact form was being used by Admin for managing contacts to Create, Edit & Deactivate from Portal.
  • When new Portal contacts were created from the portal, Duplicate records were getting created despite the Duplicate Detection rule on Dynamics. It, however, showed a system message which was not understandable.
  • If the new user to be created was an Inactive user, he needed to be activated rather than be created.

Challenges

  • Liquid code could not be used since it could not be triggered on change event of the email address field.
  • Defining a custom solution which worked in parallel with existing features.

Requirement

  • To detect and restrict duplicate contacts getting created from the portal and provide an appropriate message there.
  • If contact is “Inactive” in CRM, rather than create a new contact, the same should be activated.

Solution

Since the duplicate detection Rule on dynamics could not be extended to the Portal, a custom process to be created using JavaScript and Liquid code  –

  • JavaScript to trigger the event of the Create Page
  • Liquid to query data on another Page.
  1. The Redirect web template:
    • Go to Portals ➤ Web Templates and Create Web Template “My Custom Web Template”, paste the below code in source property

      {% fetchxml contacts %}

      <fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false” count=”1″ returntotalrecordcount=”true” page=”1″ >

           <entity name=”contact”>

           <attribute name=”contactid” />

           <attribute name=”statuscode” />

          <filter type=”and”>

             <condition attribute=”emailaddress1″ value=”{{request.params[’emailId’]}}” operator=”eq”/>     

          </filter>

        </entity>

      </fetch>

      {% endfetchxml %}{  

        {% if contacts.results.total_record_count > 0 %}

        “value”: [{  

            “contactid”: “{{contacts.results.entities[0].contactid}}”,

            “statuscode”: “{{contacts.results.entities[0].statuscode.label}}”}      

        ]

         {% else %}

         “value”: []

         {% endif %}

        }

  1. Its Page template

    Go to Portals ➤ Page Templates and create page template named “My Custom page template”, Website->Select your website, Type ➤ Web Template, Web Template ➤ “My Custom Web Template”, Entity Name ➤ Web Page(adx_webpage). Click Save.
  2. Its Web page so that it can be accessed.

Go to Portals->Web Pages and create web page give any name “My Custom Web Page”, Website ➤Your website, Parent Page->Home, Partial URL ➤”any name of your choice”, Page Template➤ “My Custom page template”, Publishing State->Published and click Save.

  1. On the Create Contact page – add the below JavaScript to trigger on the change event of the Email ID Text field.

Go to Portal ➤ Web Pages->Open your page and click on a web template and paste the below code in source property of web template and click Save.

Note: If you are using default web template then create a new one

  <script type=”text/javascript”>   

      $(document).ready(function () {  

   $(“#emailaddress1”).change(function() {

       $(‘.notifications’).remove()        

      var httpReq = CreateHTTPRequest(“GET”, “/mycustomwebpage/?emailId=” + $(“#emailaddress1”).val(), false);

        httpReq.send(null);

        if (httpReq.status == 200) {

      var contactId = JSON.parse(httpReq.responseText).value;

            if(contactId.length > 0)

            {

                if(contactId[0].statuscode == “Inactive”)

                {

                   $(‘.breadcrumb’).append(‘<div class=”notifications” style=”display: block;”> <div class=”notification alert alert-danger error alert-dismissible” role=”alert”><button type=”button” class=”close” data-dismiss=”alert” aria-label=”Close”><span aria-hidden=”true”>×</span></button><span class=”fa fa-exclamation-triangle” aria-hidden=”true”></span> Note: This contact already exists in CRM, in InActive mode. Click <a href=”/updatecontact?id=’+ contactId[0].contactid +'” target=”_blank”>here</a> to open the record and Activate.</div></div>’); 

                   $(‘#InsertButton’).attr(‘disabled’,true); 

                }

                else{

                   $(‘.breadcrumb’).append(‘<div class=”notifications” style=”display: block;”> <div class=”notification alert alert-danger error alert-dismissible” role=”alert”><button type=”button” class=”close” data-dismiss=”alert” aria-label=”Close”><span aria-hidden=”true”>×</span></button><span class=”fa fa-exclamation-triangle” aria-hidden=”true”></span> Note: This contact is already exists in CRM</div></div>’); 

                   $(‘#InsertButton’).attr(‘disabled’,true);

                }              

            }

            else{ $(‘#InsertButton’).attr(‘disabled’,false); }       

        }

        });

      });

      CreateHTTPRequest = function (action, url, async) {

    try {

        var httpReq = new XMLHttpRequest();

        httpReq.open(action, url, async);

        httpReq.setRequestHeader(“Accept”, “application/json”);

        httpReq.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);

    }

    catch (ex) {

        throw ex;

    }

    return httpReq;

}

</script>

Note: In the above script change the click here to open and the activate link to your edit contact page. Steps to create Edit contact page below.

  • Create Edit contact page to activate contact on portal
  • Create a new web Contact form in CRM. Fields
    • Full Name
    • Account
    • Work Email address
    • Contact details
  • The update contact form on Portal
    • Go to Portals ➤ Entity Forms ➤ New and Create new entity form for update Contact. Select Entity name ➤ Contact, Form Name as created above in Dynamics, Select mode as Edit, Record Source type ➤ Query String, Record ID Query String Parameter  ➤ id. Website ➤ Select your website.
    • In Additional setting ➤ Action Button Configuration ➤ Actions ➤ Click on +Activate
    • Go to Portals ➤Web Pages ➤ New and Create a web page for update contact entity form.

You can add some emails and templates to be triggered on any changes to be tracked!

Also, check out Contact Management from Dynamics 365 Portal – Part 1