Skip to content

Tutorial 9

PhuocLe edited this page Aug 27, 2018 · 12 revisions

Task

  • When a lead qualified, send an email (with an email template) notification to all team members of the current user belong to teams

Prerequisites

Explain

This tutorial is the combine of these tutorials: 5, 6, 7, 8 that make the final solution the customer want

  1. User create a new lead then send an email to all team members
  2. If user has security role "Qualify Lead", they can see the button "QUALIFY"
  3. User click button "QUALIFY", it create Account and Contact then redirect to Account created
  4. Process "QUALIFY" should copy field Field1, Field2, Field3 from lead to account
  5. When lead qualified then send an email to all team members

Coding

Ribbon Workbench

  1. Hide 4 button in the Homepage of Lead entity
    • Mscrm.HomepageGrid.lead.ConvertLeadAs
    • Mscrm.HomepageGrid.lead.ConvertLeadAs.Web
    • Mscrm.HomepageGrid.lead.ConvertLeadQuick
    • Mscrm.HomepageGrid.lead.ConvertLeadQuick.Web
  2. Hide 4 button in the Form of Lead entity
    • Mscrm.Form.lead.ConvertLeadAs
    • Mscrm.Form.lead.ConvertLeadAs.Web
    • Mscrm.Form.lead.ConvertLeadQuick
    • Mscrm.Form.lead.ConvertLeadQuick.Web
  3. Copy button
    • Mscrm.Form.lead.ConvertLeadAs and paste as name paz.Mscrm.Form.lead.ConvertLeadQuick
    • Mscrm.Form.lead.ConvertLeadQuick.Web and paste as name paz.Mscrm.Form.lead.ConvertLeadQuick.Web
  4. Edit Customise Command of 2 button paz.Mscrm.Form.lead.ConvertLeadQuick, paz.Mscrm.Form.lead.ConvertLeadQuick.Web
  5. Add enable rule
    • Id: paz.lead.EnableRule.HasRole
    • CustomRule
      • Default: True
      • FunctionName: isNaN
      • Library: $webresource:paz_/lib/WebApiClient.js
    • CustomRule
      • FunctionName: formLead.HasRole
      • Library: $webresource:paz_/entities/Lead.js
      • String Parameter: Qualify Lead
  6. Add enable rule paz.lead.EnableRule.HasRole to 2 command Mscrm.Form.lead.ConvertQuick and Mscrm.Form.lead.ConvertQuick.Web
  7. Edit Actions in 2 command Mscrm.Form.lead.ConvertQuick and Mscrm.Form.lead.ConvertQuick.Web
    • Library: $webresource:paz_/entities/Lead.js
    • Function Name: formLead.QualifyLead_Click
    • _CRM Parameter__: PrimaryControl
  8. Publish
  9. The RibbonDiffXml bellow
<?xml version="1.0" encoding="utf-16"?>
<RibbonDiffXml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CustomActions>
    <CustomAction Id="paz.Mscrm.Form.lead.ConvertLeadQuick.CustomAction" Location="Mscrm.Form.lead.MainTab.Actions.Controls._children" Sequence="0">
      <CommandUIDefinition>
        <Button Alt="$LocLabels:Ribbon.Form.opportunity.MainTab.Actions.Convert" Command="Mscrm.Form.lead.ConvertQuick" Id="paz.Mscrm.Form.lead.ConvertLeadQuick" Image32by32="$webresource:MarketingSales/_imgs/ribbon/ConvertLead_32.png" Image16by16="$webresource:MarketingSales/_imgs/ribbon/ConvertLead_16.png" LabelText="$LocLabels:Ribbon.Form.opportunity.MainTab.Actions.Convert" Sequence="0" TemplateAlias="o1" ToolTipTitle="$LocLabels:Ribbon.Form.Lead.MainTab.Actions.Convert.ToolTipTitle" ToolTipDescription="$LocLabels:Ribbon.Tooltip.ConvertLead" ModernImage="Qualify" />
      </CommandUIDefinition>
    </CustomAction>
    <CustomAction Id="paz.Mscrm.Form.lead.ConvertLeadQuick.Web.CustomAction" Location="Mscrm.Form.lead.MainTab.Actions.Controls._children" Sequence="0">
      <CommandUIDefinition>
        <Button Alt="$LocLabels:Ribbon.Form.opportunity.MainTab.Actions.Convert" Command="Mscrm.Form.lead.ConvertQuick.Web" Id="paz.Mscrm.Form.lead.ConvertLeadQuick.Web" Image32by32="$webresource:MarketingSales/_imgs/ribbon/ConvertLead_32.png" Image16by16="$webresource:MarketingSales/_imgs/ribbon/ConvertLead_16.png" LabelText="$LocLabels:Ribbon.Form.opportunity.MainTab.Actions.Convert" Sequence="0" TemplateAlias="o1" ToolTipTitle="$LocLabels:Ribbon.Form.Lead.MainTab.Actions.Convert.ToolTipTitle" ToolTipDescription="$LocLabels:Ribbon.Tooltip.ConvertLead" ModernImage="Qualify" />
      </CommandUIDefinition>
    </CustomAction>
    <HideCustomAction HideActionId="paz.Mscrm.HomepageGrid.lead.ConvertLeadAs.Hide" Location="Mscrm.HomepageGrid.lead.ConvertLeadAs" />
    <HideCustomAction HideActionId="paz.Mscrm.HomepageGrid.lead.ConvertLeadAs.Web.Hide" Location="Mscrm.HomepageGrid.lead.ConvertLeadAs.Web" />
    <HideCustomAction HideActionId="paz.Mscrm.HomepageGrid.lead.ConvertLeadQuick.Hide" Location="Mscrm.HomepageGrid.lead.ConvertLeadQuick" />
    <HideCustomAction HideActionId="paz.Mscrm.HomepageGrid.lead.ConvertLeadQuick.Web.Hide" Location="Mscrm.HomepageGrid.lead.ConvertLeadQuick.Web" />
    <HideCustomAction HideActionId="paz.Mscrm.Form.lead.ConvertLeadQuick.Hide" Location="Mscrm.Form.lead.ConvertLeadQuick" />
    <HideCustomAction HideActionId="paz.Mscrm.Form.lead.ConvertLeadQuick.Web.Hide" Location="Mscrm.Form.lead.ConvertLeadQuick.Web" />
    <HideCustomAction HideActionId="paz.Mscrm.Form.lead.ConvertLeadAs.Hide" Location="Mscrm.Form.lead.ConvertLeadAs" />
    <HideCustomAction HideActionId="paz.Mscrm.Form.lead.ConvertLeadAs.Web.Hide" Location="Mscrm.Form.lead.ConvertLeadAs.Web" />
  </CustomActions>
  <Templates>
    <RibbonTemplates Id="Mscrm.Templates" />
  </Templates>
  <CommandDefinitions>
    <CommandDefinition Id="Mscrm.Form.lead.ConvertQuick">
      <EnableRules>
        <EnableRule Id="Mscrm.LeadIsOpen" />
        <EnableRule Id="Mscrm.Form.lead.IsUCI" />
        <EnableRule Id="Mscrm.IsEntityAvailableForUserInMocaOffline" />
        <EnableRule Id="Mscrm.IsAccountAvailableInMocaOffline" />
        <EnableRule Id="Mscrm.IsContactAvailableInMocaOffline" />
        <EnableRule Id="Mscrm.IsOpportunityAvailableInMocaOffline" />
        <EnableRule Id="paz.lead.EnableRule.HasRole" />
      </EnableRules>
      <DisplayRules>
        <DisplayRule Id="Mscrm.DynamicMenu.NewRecord.opportunity" />
        <DisplayRule Id="Mscrm.HideOnOutlookClient" />
      </DisplayRules>
      <Actions>
        <JavaScriptFunction FunctionName="formLead.QualifyLead_Click" Library="$webresource:paz_/entities/Lead.js">
          <CrmParameter Value="PrimaryControl" />
        </JavaScriptFunction>
      </Actions>
    </CommandDefinition>
    <CommandDefinition Id="Mscrm.Form.lead.ConvertQuick.Web">
      <EnableRules>
        <EnableRule Id="Mscrm.LeadIsOpen" />
        <EnableRule Id="Mscrm.Form.lead.IsNotUCI" />
        <EnableRule Id="Mscrm.IsEntityAvailableForUserInMocaOffline" />
        <EnableRule Id="Mscrm.IsAccountAvailableInMocaOffline" />
        <EnableRule Id="Mscrm.IsContactAvailableInMocaOffline" />
        <EnableRule Id="Mscrm.IsOpportunityAvailableInMocaOffline" />
        <EnableRule Id="paz.lead.EnableRule.HasRole" />
      </EnableRules>
      <DisplayRules>
        <DisplayRule Id="Mscrm.SingleQualifyStatusCode" />
        <DisplayRule Id="Mscrm.DynamicMenu.NewRecord.opportunity" />
      </DisplayRules>
      <Actions>
        <JavaScriptFunction FunctionName="formLead.QualifyLead_Click" Library="$webresource:paz_/entities/Lead.js">
          <CrmParameter Value="PrimaryControl" />
        </JavaScriptFunction>
      </Actions>
    </CommandDefinition>
  </CommandDefinitions>
  <RuleDefinitions>
    <TabDisplayRules />
    <DisplayRules>
      <DisplayRule Id="Mscrm.DynamicMenu.NewRecord.opportunity">
        <EntityPrivilegeRule PrivilegeType="Create" PrivilegeDepth="Basic" EntityName="opportunity" />
        <EntityPrivilegeRule PrivilegeType="Append" PrivilegeDepth="Basic" EntityName="opportunity" />
        <OrRule>
          <Or>
            <EntityPrivilegeRule PrivilegeType="AppendTo" PrivilegeDepth="Basic" EntityName="account" />
          </Or>
          <Or>
            <EntityPrivilegeRule PrivilegeType="AppendTo" PrivilegeDepth="Basic" EntityName="contact" />
          </Or>
        </OrRule>
      </DisplayRule>
      <DisplayRule Id="Mscrm.HideOnOutlookClient">
        <CrmClientTypeRule Type="Outlook" InvertResult="true" />
      </DisplayRule>
      <DisplayRule Id="Mscrm.SingleQualifyStatusCode">
        <OptionSetRule OptionSet="lead_statuscode" StateCode="1" ObjectTypeCode="4" />
      </DisplayRule>
    </DisplayRules>
    <EnableRules>
      <EnableRule Id="Mscrm.LeadIsOpen">
        <FormStateRule State="Existing" />
      </EnableRule>
      <EnableRule Id="Mscrm.Form.lead.IsUCI">
        <CustomRule FunctionName="ClientUtility.ClientUtil.isUCI" Library="$webresource:MarketingSales/Lead/Lead_main_system_library.js" />
      </EnableRule>
      <EnableRule Id="Mscrm.IsEntityAvailableForUserInMocaOffline">
        <CustomRule FunctionName="XrmCore.Rules.Online.IsEntityAvailableForUserInMocaOffline" Library="$webresource:Main_system_library.js">
          <CrmParameter Value="SelectedEntityTypeName" />
        </CustomRule>
      </EnableRule>
      <EnableRule Id="Mscrm.IsAccountAvailableInMocaOffline">
        <CustomRule FunctionName="XrmCore.Rules.Online.IsEntityAvailableForUserInMocaOffline" Library="$webresource:Main_system_library.js">
          <StringParameter Value="account" />
        </CustomRule>
      </EnableRule>
      <EnableRule Id="Mscrm.IsContactAvailableInMocaOffline">
        <CustomRule FunctionName="XrmCore.Rules.Online.IsEntityAvailableForUserInMocaOffline" Library="$webresource:Main_system_library.js">
          <StringParameter Value="contact" />
        </CustomRule>
      </EnableRule>
      <EnableRule Id="Mscrm.IsOpportunityAvailableInMocaOffline">
        <CustomRule FunctionName="XrmCore.Rules.Online.IsEntityAvailableForUserInMocaOffline" Library="$webresource:Main_system_library.js">
          <StringParameter Value="opportunity" />
        </CustomRule>
      </EnableRule>
      <EnableRule Id="Mscrm.Form.lead.IsNotUCI">
        <CustomRule FunctionName="ClientUtility.ClientUtil.isUCI" Library="$webresource:MarketingSales/Lead/Lead_main_system_library.js" InvertResult="true" />
      </EnableRule>
      <EnableRule Id="paz.lead.EnableRule.HasRole">
        <CustomRule FunctionName="isNaN" Library="$webresource:paz_/lib/WebApiClient.js" Default="true" />
        <CustomRule FunctionName="formLead.HasRole" Library="$webresource:paz_/entities/Lead.js">
          <StringParameter Value="Qualify Lead" />
        </CustomRule>
      </EnableRule>
    </EnableRules>
  </RuleDefinitions>
  <LocLabels />
</RibbonDiffXml>

Lead.js

  1. Create function QualifyLead_Click
...
	function qualifyLead_Click(executionContext) {//executionContext is PrimaryControl parameter use in the Riibon Workbench
	    var form = new LuckeyMonkey.FormLead(executionContext);
	    var input = {
	        LeadId: form.EntityId
	    };
	    var jsonInput = JSON.stringify(input);
	    var req = new LuckeyMonkey.WebApi.CustomRequest();
	    req.async = false;
	    req.method = "POST";
	    req.bound = false;
	    req.name = "paz_Ajax";
	    req.payload = {
	        "function": "QualifyLead",
	        "jsonInput": jsonInput
	    };
	    var res = WebApiClient.Execute(req);
	    var output = JSON.parse(res.jsonOutput);
	    if (output.Status) {
	        form.Utility.OpenForm({
	            entityName: "account",
	            entityId: output.AccountId
	        }, null, null, null);
	    }
	}
	return {
		OnLoad: onLoad,
		OnSave: onSave,
		HasRole: hasRole,
		QualifyLead_Click: qualifyLead_Click,
		__SubjectAddOnChange__: SubjectAddOnChange,
		__PhoneAddOnChange__: PhoneAddOnChange
	};
...

NOTED Please DON'T copy/paste code. Try typing to get intellisense that PL.DynamicsCrm.DevKit support

  1. Right-click on Visual Studio of file Lead.js then select PL.DynamicsCrm.DevKit - Publish WebResource and waiting PL.DynamicsCrm.DevKit deploy Lead.js to Dynamics 365
  2. Run deploy.bat of project Paz.LuckeyMonkey.Workflow

Dynamics 365 Real-time workflow

  1. Create an email template type: Lead with Title: Lead - Qualified Lead
  2. Create a real-time workflow name Lead - Qualified Lead to use 2 custom workflow Paz.LuckeyMonkey.Workflow.RetrieveUsers and Paz.LuckeyMonkey.Workflow.SendUsersMail

Test

  • Configure current user has security role Qualify Lead
  • Configure current user belong to teams: Sales and IT
  • Current user create a new lead -> check all team members received an email Lead - New Lead
  • Current user open lead just created and see the button QUALIFY
  • Current user click on button QUALIFY, system do a qualify process, create Account and Contact then redirect user to Account just created
  • Check all team members received an email Lead - Qualified Lead
  • Check 3 fields in Account that have copied from Lead

Summary

This tutorial, you know howto

  • Use simple Ribbon Workbench
  • Reference library in Ribbon Workbench by using function isNaN
  • Form Javascript call Custom Action to Dynamics 365
Clone this wiki locally