Monday, January 2, 2012

Checking Existing Value for a Field using OData

Problem:
               We need to check the existing value of the MS CRM 2011 form. Mean we need to prevent the duplicate value of the Account form "Account Number" field and we need to check the value of the "Account Number" when user press save button, check the value in the database and then based on the return value we show message to use and prevent close of form.
Solution:
               We need to check the existing value of the MS CRM 2011 form. Mean we need to prevent the duplicate value of the Account form "Account Number" field and we need to check the value of the "Account Number" when user press save button, check the value in the database and then based on the return value we show message to use and prevent close of form.
JavaScript which is used for the above problem as mention in the Problem section is shown in List 1 here you can see that I have created simple function with name "checkAccountNumber" which is used to check the account number of the account, when you create online Organization which is of trial period of 30 days then you can see that Account number is the field which is already added mean you don't need to add any custom field. In the start of the function is the first statement is used to get the Id of the current record so that I can check if record is used for update then we need to exclude the current record from the search if I don't then it will always return record if we don't change the Account number when updating the account record. Next is the If condition to check the value of the current record if value is not null then add the and (as this will be second condition in the search as the Account number condition will be the first condition)  and then AccountId (primary id of the account) with ne (not equal ) operator. If the account id is null then "accoundIdCondition" will be empty string and we will search with the account condition only.
List 1

The next important thing in the code which I like to explain is the accountNumberRequest.open() the third parameter which is false is used to determine whether call is async o sync as we need to send  sync call because if we send async then when the completed event of the form called the form will be closed. So we will pass false in the third parameter sot that call be will sync and depending on the return value we will prevent the form from closing and saving record. In the getAccountNumberCallBack function I just check for the record and then show message to the end user and then call the preventDetault() function which is used to Cancels the save operation, but all remaining handlers for the event will still be executed.

Image 1
The output of the above function when you execute is shown in the Image 1.Here you can see that I have enter account number which already exist in the database.If it is not in the database then it will not close the form and the form will save the record.


All and any comments / bugs / suggestions are welcomed!

1 comment:

Asad Mahmood said...

Hi Asim,
I am trying to use the same script but it is not working.
Are you passing any Comma seperated list of parameter in the script.