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.

2 comments:

  1. I pasted your code into OnLoad event for Account, but got an error message when running preview. Is there something in the code that has to be unique to our deployment?

    ReplyDelete
  2. Im a CRM Consultant, when use this code, I have got the following error:

    error code:0x80040203


    and also I have got an error message:
    "'null' is null or not an object".

    please assist me on this,
    Thanks,
    Israel.

    ReplyDelete