简体   繁体   English

当潜在客户合格时,如何防止Dynamics CRM 2015创造机会?

[英]How to prevent Dynamics CRM 2015 from creating an opportunity when a lead is qualified?

Requirements when clicking the Qualify button in the Lead entity form: 单击潜在客户实体表单中的Qualify按钮时的要求:

  • Do not create an Opportunity 不要创造机会
  • Retain original CRM qualify-lead JavaScript 保留原始的CRM合格线索JavaScript
  • Detect duplicates and show duplicate detection form for leads 检测重复并显示潜在客户的重复检测表格
  • Redirect to contact, either merged or created version, when done 完成后,重定向到联系人(合并或创建的版本)

The easiest approach is to create a plugin running on Pre-Validation for message "QualifyLead". 最简单的方法是为消息“ QualifyLead”创建一个在Pre-Validation上运行的插件。 In this plugin you simply have to set CreateOpportunity input property to false. 在此插件中,您只需要将CreateOpportunity输入属性设置为false。 So it would look like: 因此,它看起来像:

public void Execute(IServiceProvider serviceProvider)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    context.InputParameters["CreateOpportunity"] = false;
}

Or you can go with more fancy way: 或者,您也可以采用更花哨的方式:

public void Execute(IServiceProvider serviceProvider)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    var qualifyRequest = new QualifyLeadRequest();
    qualifyRequest.Parameters  = context.InputParameters;
    qualifyRequest.CreateOpportunity = false;
}

Remember that it should be Pre-Validation to work correctly. 请记住,它应该是预先验证才能正常工作。 Doing it like that allows you to remain with existing "Qualify" button, without any JavaScript modifications. 这样,您就可以保留现有的“合格”按钮,而无需进行任何JavaScript修改。

So Pawel Gradecki already posted how to prevent CRM from creating an Opportunity when a Lead is qualified. 因此,Pawel Gradecki已经发布了如何在潜在客户合格后阻止CRM创造机会。 The tricky part is to make the UI/client refresh or redirect to the contact, as CRM does nothing if no Opportunity is created. 棘手的部分是使UI /客户端刷新或重定向到联系人,因为如果没有创建机会,则CRM不会执行任何操作。

Before we begin, Pawel pointed out that 在开始之前,Pawel指出

some code is not supported, so be careful during upgrades 不支持某些代码,因此升级时请小心

I don't have experience with any other versions than CRM 2015, but he writes that there are better ways to do this in CRM 2016, so upgrade if you can. 除了CRM 2015之外,我没有其他版本的经验,但是他写道,在CRM 2016中有更好的方法可以做到这一点,因此请升级。 This is a fix that's easy to implement now and easy to remove after you've upgraded. 此修复程序现在很容易实现,升级后很容易删除。

Add a JavaScript-resource and register it in the Lead form's OnSave event. 添加一个JavaScript资源,并在Lead表单的OnSave事件中注册它。 The code below is in TypeScript. 下面的代码在TypeScript中。 TypeScript-output (js-version) is at the end of this answer. TypeScript输出(js版本)在此答案的结尾。

function OnSave(executionContext: ExecutionContext | undefined) {
    let eventArgs = executionContext && executionContext.getEventArgs()
    if (!eventArgs || eventArgs.isDefaultPrevented() || eventArgs.getSaveMode() !== Xrm.SaveMode.qualify)
        return

    // Override the callback that's executed when the duplicate detection form is closed after selecting which contact to merge with.
    // This callback is not executed if the form is cancelled.
    let originalCallback = Mscrm.LeadCommandActions.performActionAfterHandleLeadDuplication
    Mscrm.LeadCommandActions.performActionAfterHandleLeadDuplication = (returnValue) => {
        originalCallback(returnValue)
        RedirectToContact()
    }

    // Because Opportunities isn't created, and CRM only redirects if an opportunity is created upon lead qualification,
    // we have to write custom code to redirect to the contact instead
    RedirectToContact()
}

// CRM doesn't tell us when the contact is created, since its qualifyLead callback does nothing unless it finds an opportunity to redirect to.
// This function tries to redirect whenever the contact is created
function RedirectToContact(retryCount = 0) {
    if (retryCount === 10)
        return Xrm.Utility.alertDialog("Could not redirect you to the contact. Perhaps something went wrong while CRM tried to create it. Please try again or contact the nerds in the IT department.")

    setTimeout(() => {
        if ($("iframe[src*=dup_warning]", parent.document).length)
            return // Return if the duplicate detection form is visible. This function is called again when it's closed

        let leadId = Xrm.Page.data.entity.getId()
        $.getJSON(Xrm.Page.context.getClientUrl() + `/XRMServices/2011/OrganizationData.svc/LeadSet(guid'${leadId}')?$select=ParentContactId`)
            .then(r => {
                if (!r.d.ParentContactId.Id)
                    return RedirectToContact(retryCount + 1)

                Xrm.Utility.openEntityForm("contact", r.d.ParentContactId.Id)
            })
            .fail((_, __, err) => Xrm.Utility.alertDialog(`Something went wrong. Please try again or contact the IT-department.\n\nGuru meditation:\n${err}`))
    }, 1000)
}

TypeScript definitions: TypeScript定义:

declare var Mscrm: Mscrm

interface Mscrm {
    LeadCommandActions: LeadCommandActions
}

interface LeadCommandActions {
    performActionAfterHandleLeadDuplication: { (returnValue: any): void }
}

declare var Xrm: Xrm

interface Xrm {
    Page: Page
    SaveMode: typeof SaveModeEnum
    Utility: Utility
}

interface Utility {
    alertDialog(message: string): void
    openEntityForm(name: string, id?: string): Object
}

interface ExecutionContext {
    getEventArgs(): SaveEventArgs
}

interface SaveEventArgs {
    getSaveMode(): SaveModeEnum
    isDefaultPrevented(): boolean
}

interface Page {
    context: Context
    data: Data
}

interface Context {
    getClientUrl(): string
}

interface Data {
    entity: Entity
}

interface Entity {
    getId(): string
}

declare enum SaveModeEnum {
    qualify
}

TypeScript-output: TypeScript输出:

function OnSave(executionContext) {
    var eventArgs = executionContext && executionContext.getEventArgs();
    if (!eventArgs || eventArgs.isDefaultPrevented() || eventArgs.getSaveMode() !== Xrm.SaveMode.qualify)
        return;
    var originalCallback = Mscrm.LeadCommandActions.performActionAfterHandleLeadDuplication;
    Mscrm.LeadCommandActions.performActionAfterHandleLeadDuplication = function (returnValue) {
        originalCallback(returnValue);
        RedirectToContact();
    };
    RedirectToContact();
}
function RedirectToContact(retryCount) {
    if (retryCount === void 0) { retryCount = 0; }
    if (retryCount === 10)
        return Xrm.Utility.alertDialog("Could not redirect you to the contact. Perhaps something went wrong while CRM tried to create it. Please try again or contact the nerds in the IT department.");
    setTimeout(function () {
        if ($("iframe[src*=dup_warning]", parent.document).length)
            return;
        var leadId = Xrm.Page.data.entity.getId();
        $.getJSON(Xrm.Page.context.getClientUrl() + ("/XRMServices/2011/OrganizationData.svc/LeadSet(guid'" + leadId + "')?$select=ParentContactId"))
            .then(function (r) {
            if (!r.d.ParentContactId.Id)
                return RedirectToContact(retryCount + 1);
            Xrm.Utility.openEntityForm("contact", r.d.ParentContactId.Id);
        })
            .fail(function (_, __, err) { return Xrm.Utility.alertDialog("Something went wrong. Please try again or contact the IT-department.\n\nGuru meditation:\n" + err); });
    }, 1000);
}

There is a fully functional and supported solution posted over at our Thrives blog: https://www.thrives.be/dynamics-crm/functional/lead-qualification-well-skip-that-opportunity . 在我们的Thrives博客上发布了一个功能齐全且受支持的解决方案: https ://www.thrives.be/dynamics-crm/functional/lead-qualification-well-skip-that-opportunity。

Basically we combine the plugin modification as mentioned by Pawel with a Client Side redirect (using only supported JavaScript) afterwards: 基本上,我们随后将Pawel提到的插件修改与客户端重定向(仅使用受支持的JavaScript)结合在一起:

function RefreshOnQualify(eventContext) {
    if (eventContext != null && eventContext.getEventArgs() != null) {
        if (eventContext.getEventArgs().getSaveMode() == 16) {
            setTimeout(function () {
                Xrm.Page.data.refresh(false).then(function () {
                    var contactId = Xrm.Page.getAttribute("parentcontactid").getValue();
                    if (contactId != null && contactId.length > 0) {
                        Xrm.Utility.openEntityForm(contactId[0].entityType, contactId[0].id)
                    }
                }, function (error) { console.log(error) });;
            }, 1500);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Dynamics CRM 365 中插入潜在客户数据? - How to insert lead data in Dynamics CRM 365? 如何在Dynamics CRM 2015中向表单添加按钮 - How to add a button to a form in Dynamics CRM 2015 CRM Dynamics 2015 IFrame通信 - CRM Dynamics 2015 IFrame Communication 从弹出式CRM Dynamics 2015中的父表单获取数据 - Get data from parent form in a popup CRM Dynamics 2015 如何在 Dynamics CRM 2015 Update 1 online 中将仪表板的宽度设置为 100% - How to set width of the dashboard to be 100% in Dynamics CRM 2015 Update 1 online Microsoft Dynamics CRM 2015计算字段如何工作? - How the Microsoft Dynamics CRM 2015 Calculated Field works? 为什么此代码不会在Microsoft Dynamics CRM 2011中使用Odata创建机会? - Why will this code not create an Opportunity using Odata in Microsoft Dynamics CRM 2011? Dynamics CRM - 从子网格创建记录时的 N:N 关系数据/字段/属性映射 - Dynamics CRM - N:N relationship data/field/attribute mapping when creating record from subgrid 如何在主题字段中隐藏主题,就像在 MS Dynamics CRM 2015 中隐藏选项集字段中的项目一样? - How can I hide a subject in subject field, just like hiding an item from optionset field in MS Dynamics CRM 2015? CRM 2015 Javascript,禁止保存和关闭记录表格 - CRM 2015 Javascript, prevent record form from SAVE AND CLOSE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM