Tuesday, July 28, 2009

Find login user of CRM using JavaScript

var xml = "" +
"" +
"< soap:envelope chema-instance="" chema="" envelope="" http:="" schemas.xmlsoap.org="" soap="" www.w3.org="" xmlns:soap="\ " xmlns:xsd="\ " xmlns:xsi="\ " >" +

GenerateAuthenticationHeader() +

" < soap:body >" +

" < retrievemultiple crm="" ebservices="" http:="" schemas.microsoft.com="" xmlns="\" >" +

" < query crm="" http:="" q1:queryexpression="" schemas.microsoft.com="" uery="" xmlns:q1="\" xsi:type="\" >" +

" < q1:entityname>systemuser< / q1:entityname >" +

" < q1:columnset q1:columnset="" xsi:type="\" >" +

" < q1:attributes >" +

" < q1:attribute>businessunitid < / q1:attribute >" +

" < q1:attribute>firstname< / q1:attribute >" +

" < q1:attribute>fullname" +

" < q1:attribute>lastname" +

" < q1:attribute>organizationid " +

" < q1:attribute>systemuserid" +

" < /q1:attributes >" +

" < /q1:columnset >" +

" < q1:distinct>false" +

" < q1:criteria >" +

" < q1:filteroperator>And" +

" < q1:conditions >" +

" < q1:condition >" +

" < q1:attributename>systemuserid" +

" < q1:operator>EqualUserId" +

" < / q1:condition >" +

" < / q1:conditions >" +

" < / q1:criteria >" +

" < / query >" +

" < / retrievemultiple >" +

" < / soap:body >" +

"< / soap:envelope >" +

"";



var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");



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

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

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

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

xmlHttpRequest.send(xml);



var resultXml = xmlHttpRequest.responseXML;

var entityNode = resultXml.selectSingleNode("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");



var firstNameNode = entityNode.selectSingleNode("q1:firstname").nodeTypedValue;

var lastNameNode = entityNode.selectSingleNode("q1:lastname").nodeTypedValue;

var fullNameNode = entityNode.selectSingleNode("q1:fullname").nodeTypedValue;

var systemUserIdNode = entityNode.selectSingleNode("q1:systemuserid").nodeTypedValue;

var businessUnitIdNode = entityNode.selectSingleNode("q1:businessunitid").nodeTypedValue;

var organizationIdNode = entityNode.selectSingleNode("q1:organizationid").nodeTypedValue;


alert(systemUserIdNode );

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