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

时间:2017-04-24 20:09:52

标签: javascript c# typescript dynamics-crm

点击“潜在客户”实体表单中的Qualify按钮时的要求:

  • 不要创造机会
  • 保留原始CRM qualify-lead JavaScript
  • 检测重复项并显示潜在客户的重复检测表格
  • 完成后重定向到联系,合并或创建的版本

3 个答案:

答案 0 :(得分:2)

最简单的方法是创建一个在预验证上运行的插件,用于消息" QualifyLead"。在这个插件中,您只需将CreateOpportunity输入属性设置为false。所以它看起来像:

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

或者你可以用更花哨的方式:

%PATH%

请记住,应该进行预验证才能正常工作。这样做可以让你继续使用现有的" Qualify"按钮,没有任何JavaScript修改。

答案 1 :(得分:2)

因此,Pawel Gradecki已经发布了如何在潜在客户合格时阻止CRM创建商机。棘手的部分是让UI /客户端刷新或重定向到联系人,因为如果没有创建机会,CRM什么都不做。

在我们开始之前,Pawel指出

  

不支持某些代码,因此在升级期间要小心

我没有CRM 2015之外的任何其他版本的经验,但他写道,在CRM 2016中有更好的方法可以做到这一点,所以如果可以,请进行升级。这个修复程序现在很容易实现,并且在您升级后很容易删除。

添加JavaScript资源并在Lead表格的OnSave事件中注册。下面的代码是在TypeScript中。 TypeScript-output(js-version)就在这个答案的最后。

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定义:

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
}

打字稿输出:

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);
}

答案 2 :(得分:2)

我们的Thrives博客发布了一个功能齐全且支持的解决方案:https://www.thrives.be/dynamics-crm/functional/lead-qualification-well-skip-that-opportunity

基本上我们将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);
        }
    }
}