Friday, July 17, 2009

Auto Generated Account Number using JavaScript

Here given JavaScript to access CRM data using web service, means we can call CRM webservice using JavaScript.

Main logic for generating Account Number

a. Find the last Account number assigned to account using web service.
b. Increment last account number by one.
c. Set new generated account number to account.

=============================================


To find last account number, we are using JavaScript, and we can write this Script on "OnLoad" or "onSave" event of account.

JavaScript


var authenticationHeader = GenerateAuthenticationHeader();
// Prepare the SOAP message.
var xml = ""+
"" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+
authenticationHeader+
""+
""+
"" xsi:type='q1:QueryExpression'>"+
"account"+
""+
""+
"new_AccountNumber"+
"
"+
"
"+
"false"+
"" +
" " +
" new_AccountNumber" +
" Descending" +
"
" +
"
" +
"
"+
"
"+
"
"+
"
";

// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");

xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");

xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

xHReq.setRequestHeader("Content-Length", xml.length);

xHReq.send(xml);
// Capture the result.

var resultXml = xHReq.responseXML;
var results = resultXml.getElementsByTagName('BusinessEntity');
var i=0;
var lastAccountNumber= 0;
lastAccountNumber= results[i].selectSingleNode('./q1:new_AccountNumber).nodeTypedValue;

lastAccountNumber=eval(lastAccountNumber)+1
crmForm.all.new_AccountNumber.DataValue = lastAccountNumber;

}


Similary we can find any data of CRM entities using JavaScript and web service.

Hide associated view buttons

Some time user is not required some buttons to be shown in associated view grid menu.
To hide associated view buttons we are required to Add JavaScript on the form OnLoad event of entity, in which this assicated view is shown.

Suppose we want to hide "Add existing ----" button from associated view.
This assoicated view will be shown in Account entity, and associated view is of some custom entity. then we are requried to add following JavaScript on Account form load event.

====================================

HideAssociatedViewButtons('new_account_new_sponsoredkid', ['Add existing Sponsored Kid to this record']);

function HideAssociatedViewButtons(loadAreaId, buttonTitles){
    var navElement = document.getElementById('nav_' + loadAreaId);
    if (navElement != null) {
        navElement.onclick = function LoadAreaOverride() {
            // Call the original CRM method to launch the navigation link and create area iFrame
            loadArea(loadAreaId);
            HideViewButtons(document.getElementById(loadAreaId + 'Frame'), buttonTitles);
        }
    }
}

function HideViewButtons(Iframe, buttonTitles) {
    if (Iframe != null) {
        Iframe.onreadystatechange = function HideTitledButtons() {
            if (Iframe.readyState == 'complete') {
                var iFrame = frames[window.event.srcElement.id]; var liElements = iFrame.document.getElementsByTagName('li');
                for (var j = 0; j < buttonTitles.length; j++) {
                    for (var i = 0; i < liElements.length; i++) {
                        if (liElements[i].getAttribute('title') == buttonTitles[j]) {
                            liElements[i].style.display = 'none'; break;
                        }
                    }
                }
            }
        }
    }
}

==================================== HideAssociatedViewButtons function requires loadAreaId, buttonTitles as a parameters. We can find this loadAreaID, by viewing source code of Account form. and buttonTitles means ToolTip of that button. Above all contents are referred from http://blog.davehawes.com/post/2008/04/24/MSCRM-4-Remove-Add-Existing-xxxxx-button.aspx

Hide any tab of CRM form

To hide any tab of MS CRM form, we are reuqired to write JavaScript on "OnLoad" event of that form.

Just add following line in OnLoad event

crmForm.all.tabTab.style.display = "none";

Replace with the index number of the tab.
The first tab has index 0, the second 1 and so on.

If the notes tab is the fourth tab in a form, the code will be

crmForm.all.tab3Tab.style.display = "none";