Show popup/alert if account has related entity records

感情迁移 提交于 2019-12-25 01:48:48

问题


I have created a custom entity called Alert. I have associated this with the out of the box Account entity.

What I want to do now is to customise the Account form so that when a user opens it, it checks if the current account has any active alerts. If it does, it should show a message informing them of this (a javascript alert?) and then navigate to the alerts view for the account.

I've done some basic javascript in CRM but I'm not sure how to query related entities.

Note Active alert is defined by the Display From and Display Until dates in the Alert being active dates (Display From <= Today AND Display Until >= Today).

Update

Thanks for pointing me in the direction of oData. I now have the following function which is looking up the account set but expanding the relationship with the alerts. I'm trying to figure out how to check if there are any alerts, currently my code always triggers the javascript alert.

function CheckForAlerts(accountId)
{
    var odataSelect = "http://mscrmdev/Test/xrmservices/2011/OrganizationData.svc/AccountSet?$expand=new_account_new_alert_Account&$filter=AccountNumber eq '" + accountId + "'";

    $.ajax({
           type: "GET",
           contentType: "application/json; charset=utf-8",
           datatype: "json",
           url: odataSelect,
           beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
           success: function (data, textStatus, XmlHttpRequest) 
               { 
                   // Use only one of these two methods

                   // Use for a selection that may return multiple entities
                   ProcessReturnedEntities(data.d.results); 

               },
           error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
       });
}

function ProcessReturnedEntities(ManyEntities)
{
    var oneEntity = ManyEntities[0];

    if (oneEntity != null)
    {
        alert('There are active alerts associated with this account.');
    }
}

回答1:


The best way to do this will be via an oData query from the javascript. The CRM 2011 SDK comes with some helper functions for oData calls like this. You will want to use the 'retrieveMultiple' method which will allow you to retrieve all 'alerts' with a lookup to the 'account' in question.

First add the 'RESTJQueryEditor.js' file from the SDK to your form and then you can add your own custom script to perform the retrieve. I then suggest creating the message which you wish to show the user in the callback success function, something like the following:-

retrieveMultiple('nameOfYourAlertEntitySet', '?$filter=myAccountLookupName eq ' + accountId, function(alerts){
    if(alerts.length > 0)
    {
        var message = '';

        for(var index = 0; index<alerts.length; index++)
        {
            message += 'alert: ' + alerts[index].name;
        }   

        alert('Found associated alerts: ' + message);
    }
    else
    {
        alert('No associated alerts found for this account');
    }
}, 
function(){
    // Log an exception
});

You will want to make the alert message a little nicer of course and adjust your attribute names accordingly, but this is the flavour of what you want to do I believe. Additionally, you can add any further criteria on the alert entity in the filter by use of the 'and' keyword.

Let me know if you have any issues.



来源:https://stackoverflow.com/questions/10027404/show-popup-alert-if-account-has-related-entity-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!