Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Saturday, March 19, 2011

CRM 2011 Retrieve Request - Retrieve Entity Record from Related Entity

With the new release of MS CRM 2011, there are new and better ways to peform some client side scripting.

Below is an example on how to retrieve data from the account entity on the contact form.



function RetrieveAccount(oAccount)
{
var oReq = getXMLHttpRequest();
if (oReq != null)
{
if (oAccount[0] != null)
{
oReq.open("GET", Xrm.Page.context.getServerUrl() +"/XRMServices/2011/OrganizationData.svc/AccountSet?$filter=AccountId%20eq%20(guid'" + oAccount[0].id + "')&$select=Name,AccountId,Telephone1", true);

oReq.onreadystatechange = function () { DisplayAccountData(oReq) };
oReq.send();
}
}
else
{
alert('not supported');
}
}

function getXMLHttpRequest () {
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest;
}
else {
try
{ return new ActiveXObject("MSXML2.XMLHTTP.3.0"); }
catch (ex) {
return null;
}
}
}

function DisplayAccountData (oReq)
{
if (oReq.readyState == 4 /* complete */)
{
if (oReq.status == 200)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(oReq.responseText);
alert('Name: ' + xmlDoc.selectSingleNode("//d:Name").text);
alert('Guid: ' + xmlDoc.selectSingleNode("//d:AccountId").text);
alert('Phone: ' + xmlDoc.selectSingleNode("//d:Telephone1").text);
}
}
}



The first function receives account object then creates an XmlHttpRequest. Next is the retrieve method:
oReq.open("GET", Xrm.Page.context.getServerUrl() +"/XRMServices/2011/OrganizationData.svc/AccountSet?$filter=AccountId%20eq%20(guid'" + oAccount[0].id + "')&$select=Name,AccountId,Telephone1", true);
This code uses the "AccountSet" and the query string keys filter and select. We retrieve using the AccountId and return the attributes Name, AccountId, and Telephone1. A SQL query would look like this SELECT Name, AccountId, Telephone FROM Account WHERE AccountId = '[account id value here]'. You can also retrieve on other attributes like Name. Example: $filter=Name%20eq%20'XYZ Company'

The rest of the code waits for the form to load and then uses the returned xml to get the attributes and display each value in alerts.

Now to get it working.



  1. We need to create a javascript file that we will use to upload when creating our library. Copy the code above and save to a file called RetrieveAccount_jscript.js (you can name it differently if you prefer).


  2. We will need to create a library in the Web Resouce. Go to Settings >> Customizations >> Customize the System, then click on the Web Resources left nav link. Click New, which will open a window to enter the following:

    • Name: new_retrieverequest
    • Display Name: Retrieve Request
    • Description: Retrieve account information using the account object parameter.
    • Type: select Script (JScript)
    • Language: English
    • Upload File: RetrieveAccount_jscript.js
  3. Open the Conact Customizable Entity. Open the Main form. Click on Form Properties.

  4. Add the new_retrieverequest library.
  5. Below, the Library section, add the Event Handler with Control=Form and Event=OnLoad.
  6. Add the Event Handler by selecting the libary new_retrieverequest and enter the function name RetrieveAccount. Make sure Enabled is checked.
  7. Next we need to fill out the parameter section. Uncheck the pass context parameter. Enter the following as the parameter to pass to the function: crmForm.parentcustomerid.DataValue.

Click OK on the windows, Save the entity, and Publish. Now open a contact record that has a related parent (which contains a main number). You will receive 3 alert windows displaying the name, guid, and a phone number.


Tuesday, May 11, 2010

CRM 4.0: Hiding Save Buttons

Recently had a client that wanted users to only "Save as Completed" activities. That had some users that thought Save & Close would actually "close" the activity. To alleviate the potential error, we decided to remove all Save buttons (Save, Save & Close, and Save and New) from the menu bar and file menu. Below is the code to do this.


var CRMFORMTYPE_CREATE = 1;
var CRMFORMTYPE_UPDATE = 2;
var CRMFORMTYPE_READONLY = 3;
var CRMFORMTYPE_DISABLED = 4;
var CRMFORMTYPE_QUICKCREATE = 5;
var CRMFORMTYPE_BULKEDIT = 6;

if(crmForm.FormType == CRMFORMTYPE_UPDATE)
{
var li = document.getElementsByTagName('LI');
var i = 0;
while(i < li.length)
{
if (li[i].getAttribute('id') == '_MIcrmFormSave' ||
li[i].getAttribute('id') == '_MIcrmFormSubmitCrmForm59truetruefalse' ||
li[i].getAttribute('id') == '_MBcrmFormSave' ||
li[i].getAttribute('id') == '_MBcrmFormSubmitCrmForm59truetruefalse')
{
li[i].outerHTML='';
}

if (li[i].getAttribute('id') == '_MIcrmFormSaveAndClose')
{
document.getElementById('_MIcrmFormSaveAndClose').style.display = "none";
}
if (li[i].getAttribute('id') == '_MBcrmFormSaveAndClose')
{
document.getElementById('_MBcrmFormSaveAndClose').style.display = "none";
}

//alert(li[i].getAttribute('title') + ' | ' + li[i].getAttribute('id'));
i = i + 1;
}
}

Thursday, April 30, 2009

Hide/Remove Items from the Entity Menu Toolbar in CRM 4.0

I recently had a request to remove or hide an item from the Entity menu toolbar in CRM 4.0.

Let's take the opportunity for example. If you had a business process reason that did not want your users to be able to close opportunities, and roles do not provide the ability to hide this, then I would write some client side javascript that would remove the link from the actions menu when the form loads.

First, you need to find the id of the item you wan to remove/hide. Using the code below on the onLoad event when updating, you can add alerts for each item.




var li = document.getElementsByTagName('LI');
var i = 0;

while(i < li.length)
{
alert(li[i].getAttribute('id'));
i = i + 1;
}




The one we are looking for is the "Close Opportunity" which is after "Recalculate" and before "Assign". Note: I would not do this on production as it will become very annoying for users.



First it will loop through all the File Menus, then the Toolbar, ending with the Action links. The first one in the image below is the Actions dropdown (action). It then goes through the activities and some other links until we see the Recalculate id (_MIcrmFormSubmitCrmForm1truetureflase) which is followed by Close Opportunity (_MIcomplete), followed by a break (), followed by the Assign link (_MIassignObject3).



So we now know the id of the link we want to remove/hide, _MIcomplete. Now we will hide this link when the form loads on update with the following code.




var CRMFORMTYPE_CREATE = 1;
var CRMFORMTYPE_UPDATE = 2;
var CRMFORMTYPE_READONLY = 3;
var CRMFORMTYPE_DISABLED = 4;
var CRMFORMTYPE_QUICKCREATE = 5;
var CRMFORMTYPE_BULKEDIT = 6;

if(crmForm.FormType == CRMFORMTYPE_UPDATE)
{
var li = document.getElementsByTagName('LI');
var i = 0;

while(i < li.length)
{
if (li[i].getAttribute('id') == '_MIcomplete')
{
li[i].outerHTML='<SPAN></SPAN>';
}
i++;
}
}




Now the link has been removed.



I've had cases where the above code did not hide buttons, so instead I had to use this:
document.getElementById('_MBCloseInvoice3').style.display = "none";