CRM 2011 – Tool – JavaScript Model Generator

I like to see what useful tools are being created  and there have been some fantastic ones, my favourite is the sitemapeditor

I have been writing some Javascript recently and it’s always a bit of a hassle having to look up all of the  names of the fields you want to use.

In CRM 4 there use to be an excellent tool which created some intellisense for you but I haven’t found anything which is really useful for CRM 2011.

so it is with some interest I saw a tweet from CRM MVP Donna Edwards about a Javascript model generator.  The project is available on codeplex and you can download it using the link below

http://crmjsmodelgenerator.codeplex.com/

Now it took me a while to work out how to get this working, these are the instructions

How to generate it:

NOTE – Make sure your javascript file changes are saved before generating the model as your unsaved changes will get overwritten.

  1. Edit the provided sample.jsconfig file.
  2. Update CrmServer to use the correct server and org.
  3. Set the JsDirectory to be the folder where your script files are located.
  4. Add an Entity node for the entity form that you will be working on
    • EntityName is required and needs to be the schema name
    • FileName is optional and it will try to use the EntityName + “.js” to find the javascript file in the JsDirectory. Use this if the file name is different than the EntityName
  5. Run the JSModelGenerator.exe

<JsConfig CrmServer=”http://server/org&#8221;
JsDirectory=”C:\Source\Project\Scripts”>
<Entities>
<Entity EntityName=”contact” FileName=”contact.js” />
</Entities>
</JsConfig>

I changed the config file but then I was running it and nothing was really happening.  What you have to do is copy specify where the downloaded javascript files are on your computer and then it will update those files and add in the intellisense code to the file.

I quite like this solution because I can now quickly find all the fields on a form and it’s quick and easy to run

 

 

 


CRM 2011 – Javascript case statement

I had to write some code where I had a lot of checkboxes and when a user selected a value on a dropdown then some would be shown and the rest would be hidden.

To do this I created 6 sections and placed the checkboxes in the correct section.  When the user changed the dropdown I then displayed one of the sections and hide the others.  This saved me writing the visible functionality for all the checkboxes (and there were loads – 60 plus).

Below is the Javascript which first reads the drop down and then based on the value it then displays a section and hides another.  The real code had 6 sections which is why I used a switch statement rather than lots of IF and IFElse statements.


var mainJobType = Xrm.Page.getAttribute("meta_generaljobtype").getText();

 switch (mainJobType) {
 case "Internal Works":
      Xrm.Page.ui.tabs.get("general").sections.get("InternalWorks").setVisible(true);
      Xrm.Page.ui.tabs.get("general").sections.get("ExternalWorks").setVisible(false);
 break;
 case "External Works":
      Xrm.Page.ui.tabs.get("general").sections.get("ExternalWorks").setVisible(true);
      Xrm.Page.ui.tabs.get("general").sections.get("InternalWorks").setVisible(false);
 break;
 default:
      Xrm.Page.ui.tabs.get("general").sections.get("InternalWorks").setVisible(false);
      Xrm.Page.ui.tabs.get("general").sections.get("ExternalWorks").setVisible(false);

 }

CRM 2011 – How to Hide the form assistant

I was using CRM 2011 and was getting annoyed by the form assistant constantly barging across the screen taking up valuable space, it reminded me of the paper clip in word, which made me look up this video – http://www.youtube.com/watch?v=GPbw9mY3_7A

“I AM NOT WRITING A LETTER”

anyway in fit of anger I typed in how to get rid of it and found my way on a Microsoft forum post with instructions

it took me to this blog page

but you can remove the assistant by using this javacript

document.getElementById('tdRelatedInformationPane').style.display = 'none';

how about that.

I will say that in the end I couldn’t be bothered to try and remove the form assistant, thinking that maybe other users of the CRM system might actually like the form

assistant and not really wanting to abuse my power as CRM SYSTEM ADMINISTRATOR, MWAHAHAHAHAHAA.

as someone said to Spiderman

With great power comes great responsibility

or

stop prancing about in those red pyjamas

CRM 2011 – Javascript snippet – How to remove all existing values from an OptionSet

A quick Javascript snipped on how to clear all options in an OptionSet

Xrm.Page.getControl(“optionSet”).clearOptions();

 

and there is nothing more to say on this

CRM 2011 – Free Twitter Integration for the Account entity

I saw this project on codeplex

MS CRM 2011 Twitter Integration

and thought this might be interesting.

It could be useful to some people but when I looked into the project it left me a little bit baffled.

When I saw project, this is actually a link to an html page

Twitter.html


&nbsp;

<HTML><HEAD>
<META charset=utf-8></HEAD>
<BODY contentEditable=true>
<SCRIPT src="http://widgets.twimg.com/j/2/widget.js"></SCRIPT>

<SCRIPT>
var search = parent.document.forms[0].all.name.DataValue;
new TWTR.Widget({
version: 2,
type: 'profile',
rpp: 10,
interval: 2000,
width: 630,
height: 300,
theme: { shell: { background: 'transparent', color: '#333' },
tweets: { background: 'transparent', color: '#333', links: '#c10000' }
},
features: { scrollbar: false, loop: true, live: false, hashtags: true, timestamp: true, avatars: true, toptweets: true, behavior: 'default' }
}).render().setUser(search).start();
 </SCRIPT>
</BODY></HTML>

There doesn’t seem to be hardly anything in the page and then I released most of the work was being done by the Javascript page

http://widgets.twimg.com/j/2/widget.js

 

It all seemed a bit weird and the thought of my CRM running external javascript files which could get changed to do something completely different didn’t really appeal to me.

 

If they are giving the solution away they should package this up into a managed solution.

I’m also not sure how the twitter integration works because I can’t see where you put in the

twitter address so I’m guessing it works by looking up the company name, which doesn’t seem

logical to me.

 

To summarise I think this is a great idea but I don’t like the way it has been done


					

How to highlight code in hosted wordpress blogs

This has been something that has been annoying me for quite a while, how do I show c# and javascript code in the blog in a decent way.

The other important factor to note here is I am using a hosted blog, so all the solutions with plugins are no good to me.

Well I finally figured it out thanks to this stackoverflow page here

you put this in your code but make sure you put it in the Visual view and not the HTML. I’m not sure how to type this so I have typed the square bracket rather than putting them in (because then it puts in code)

 

[sourcecode language='javascript']
[/sourcecode]

here is an example


function customerid_onchange(){
 // Get value of the selected customer account
 var lookupItem = Xrm.Page.getAttribute("parentcustomerid").getValue();

CRM 2011 – Javascript code to lookup parent customer addresses from contact form

I had written this code before but it is the property of my previous employer.  I could rewrite fairly easily because I have recently done the same thing in CRM 4 based on  code similar to this – http://www.stunnware.com/crm2/topic.aspx?id=JS26

but good news for me and other people out there JoshJWilliams has recently written the code and copied it to this forum post

so well done JoshJWilliams for perserving and solving the problem and thanks for copying the code up for the CRM community to use.

here is the code


function customerid_onchange(){
 // Get value of the selected customer account
 var lookupItem = Xrm.Page.getAttribute("parentcustomerid").getValue();

if (lookupItem != null)
 {
 //alert(lookupItem);

// Lookup Customer info
 var url = '/' + Xrm.Page.context.getOrgUniqueName() +
 '/_controls/lookup/lookupsingle.aspx?class=BrowseCustomerAddress&objecttypes=1071&browse=1&bindingcolumns=line1%2cpostalcode&parentType='
 + lookupItem[0].type + '&parentId='
 + lookupItem[0].id + '&ShowNewButton=1&ShowPropButton=1&DefaultType=1071';

// shows a modal window to select the addresses
 var selectedAddress = window.showModalDialog(url, null, 'dialogWidth:600px;dialogHeight:400px;resizable:yes');

// Validate address info in not null before continuing
 if (selectedAddress != null)
 {
 //alert(selectedAddress);
 var addressFields = selectedAddress.items;
 // we have the id of the address -now use the json to get the values in an array.
 sLookupValue = addressFields[0].id;
 //alert(sLookupValue);

var xml = "" +
 "<?xml version=\"1.0\" encoding = \"utf-8\"?>" +
 "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
 decodeURI(GenerateAuthenticationHeader()) +
 " <soap:Body>" +
 " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
 " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryByAttribute\">" +
 " <q1:EntityName>customeraddress</q1:EntityName>" +
 " <q1:ColumnSet xsi:type=\"q1:ColumnSet\" >" +
 " <q1:Attributes>" +
 " <q1:Attribute>city</q1:Attribute>" +
 " <q1:Attribute>country</q1:Attribute>" +
 " <q1:Attribute>fax</q1:Attribute>" +
 " <q1:Attribute>line1</q1:Attribute>" +
 " <q1:Attribute>line2</q1:Attribute>" +
 " <q1:Attribute>line3</q1:Attribute>" +
 " <q1:Attribute>name</q1:Attribute>" +
 " <q1:Attribute>postalcode</q1:Attribute>" +
 " <q1:Attribute>primarycontactname</q1:Attribute>" +
 " <q1:Attribute>stateorprovince</q1:Attribute>" +
 " <q1:Attribute>telephone1</q1:Attribute>" +
 " </q1:Attributes>" +
 " </q1:ColumnSet>" +
 " <q1:Attributes>" +
 " <q1:Attribute>customeraddressid</q1:Attribute>" +
 " </q1:Attributes>" +
 " <q1:Values>" +
 " <q1:Value xsi:type=\"xsd:string\">" + sLookupValue + "</q1:Value>" +
 " </q1:Values>" +
 " </query>" +
 " </RetrieveMultiple>" +
 " </soap:Body>" +
 " </soap:Envelope>" + "";

xml = xml.replace(/:/g, ":");
 xml = xml.replace(///g, "/");

//window.clipboardData.setData("text", xml);
 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;

//alert(xmlHttpRequest.responseText);
 var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 oXmlDoc.async = false;

oXmlDoc.loadXML(resultXML.xml);

var businessEntities = oXmlDoc.getElementsByTagName('BusinessEntity');
 /*if (businessEntities[i].selectSingleNode('./q1:name') != null)
 {
 alert(businessEntities[0].selectSingleNode('./q1:name').nodeTypedValue);
 }
 alert(businessEntities[0].selectSingleNode('./q1:city').nodeTypedValue);
 alert(businessEntities[0].selectSingleNode('./q1:country').nodeTypedValue);
 alert(businessEntities[0].selectSingleNode('./q1:line1').nodeTypedValue);
 alert(businessEntities[0].selectSingleNode('./q1:postalcode').nodeTypedValue);
 alert(businessEntities[0].selectSingleNode('./q1:stateorprovince').nodeTypedValue);
 }
 }
 } */

for (i=0; i< 1; i++)
 {

if (businessEntities[i].selectSingleNode('./q1:name') != null)
 {

// Address Name Set
 Xrm.Page.getControl("address1_name").setDisabled(false);
 Xrm.Page.getAttribute("address1_name").setValue(businessEntities[i].selectSingleNode('./q1:name').text);
 Xrm.Page.getAttribute("address1_name").setSubmitMode("always");
 Xrm.Page.getControl("address1_name").setDisabled(true);

if (businessEntities[i].selectSingleNode('./q1:city') != null) {
 // Address City set
 Xrm.Page.getControl("address1_city").setDisabled(false);
 Xrm.Page.getAttribute("address1_city").setValue(businessEntities[i].selectSingleNode('./q1:city').text);
 Xrm.Page.getAttribute("address1_city").setSubmitMode("always");
 Xrm.Page.getControl("address1_city").setDisabled(true);
 }

if (businessEntities[i].selectSingleNode('./q1:country') != null){
 // Country set
 Xrm.Page.getControl("address1_country").setDisabled(false);
 Xrm.Page.getAttribute("address1_country").setValue(businessEntities[i].selectSingleNode('./q1:country').text);
 Xrm.Page.getAttribute("address1_country").setSubmitMode("always");
 Xrm.Page.getControl("address1_country").setDisabled(true);
 }

if (businessEntities[i].selectSingleNode('./q1:line1') != null){
 // Address Line 1 set
 Xrm.Page.getControl("address1_line1").setDisabled(false);
 Xrm.Page.getAttribute("address1_line1").setValue(businessEntities[i].selectSingleNode('./q1:line1').text);
 Xrm.Page.getAttribute("address1_line1").setSubmitMode("always");
 Xrm.Page.getControl("address1_line1").setDisabled(true);
 }
 if (businessEntities[i].selectSingleNode('./q1:line2') != null){
 // Address Line 2 set
 Xrm.Page.getControl("address1_line2").setDisabled(false);
 Xrm.Page.getAttribute("address1_line2").setValue(businessEntities[i].selectSingleNode('./q1:line2').text);
 Xrm.Page.getAttribute("address1_line2").setSubmitMode("always");
 Xrm.Page.getControl("address1_line2").setDisabled(true);
 }

if (businessEntities[i].selectSingleNode('./q1:line3') != null){
 // Address Line 3 set
 Xrm.Page.getControl("address1_line3").setDisabled(false);
 Xrm.Page.getAttribute("address1_line3").setValue(businessEntities[i].selectSingleNode('./q1:line3').text);
 Xrm.Page.getAttribute("address1_line3").setSubmitMode("always");
 Xrm.Page.getControl("address1_line3").setDisabled(true);
 }

if (businessEntities[i].selectSingleNode('./q1:postalcode') != null){
 // Address ZipCode set
 Xrm.Page.getControl("address1_postalcode").setDisabled(false);
 Xrm.Page.getAttribute("address1_postalcode").setValue(businessEntities[i].selectSingleNode('./q1:postalcode').text);
 Xrm.Page.getAttribute("address1_postalcode").setSubmitMode("always");
 Xrm.Page.getControl("address1_postalcode").setDisabled(true);
 }

if (businessEntities[i].selectSingleNode('./q1:stateorprovince')!= null){
 //Address State set
 Xrm.Page.getControl("address1_stateorprovince").setDisabled(false);
 Xrm.Page.getAttribute("address1_stateorprovince").setValue(businessEntities[i].selectSingleNode('./q1:stateorprovince').text);
 Xrm.Page.getAttribute("address1_stateorprovince").setSubmitMode("always");
 Xrm.Page.getControl("address1_stateorprovince").setDisabled(true);
 }

}// end if entity not empty
 }// end for i++

}// end if no address selected
 } // end of if no customer seelcted
 // end of new address selected function
 }

CRM 2011 – Good list of Javascript articles

The blog Slow xRM has a really good list of useful Javascript articles for CRM 2011.

To check out the whole list go and read the blog post here.  I have copied a sample of some of the articles which I thought were useful and yes of course I put the links to my blog in here.

I would say the most useful CRM 2011 Javascript links on my blog our

CRM 2011 – Javascript Xrm.Page Basics

CRM 2011 – Comparison between CRM 4.0 and CRM2011 script

CRM 2011 – Getting the ServerUrl in Javascript and using Xrm.page

CRM 2011 – Javascript and Subgrids code example

 

here are some of the links from the Slow xRM article

Date: 01/28/2011 – Source: Biz Forward
Title: Format Date Fields in CRM
Keywords: JavaScript, Date

Date: 06/13/2011 – Source: Power Objects
Title: CRM 2011 Useful JavaScript tidbits
Keywords: JavaScript

Date: 07/24/2011 – Source: MS CRM 2011 Hospital
Title: Auto Refresh of Grids on form while loading MS CRM 2011
Keywords: JavaScript, Form, Grid

Date: 08/07/2011 – Source: MS CRM 2011 Hospital
Title: GetGlobalContext on MS CRM 2011
Keywords: JavaScript, Web Resource

Date: 02/17/2011 – Source: Ben Hosking
Title: How to set up a lookup using Javascript
Keywords: JavaScript, Lookup

Date: 02/24/2011 – Source: Ben Hosking
Title: How to set an attribute label in Javascript
Keywords: JavaScript, Field – Attribute

Date: 05/05/2011 – Source: Ben Hosking
Title: Javascript Date Difference Code
Keywords: JavaScript, Date

 

CRM 2011 – Comparing only dates in Javascript

I was trying to compare some dates in Javascript today, I wanted to know if one dates was less than another, if it was I set the a status to overview.

When I compared the dates I found my CRM date had set the hours, minutes, seconds to 0,0,0 which is correctly because I wasn’t interested in storing the hours and minutes.

When I compared that date to todays date in Javascript it was coming up less because todays date had the current time in it.

so to compare the dates I used the setHours method to blank the time from the date then you can compare just the date values.

 

var reviewDate = Xrm.Page.getAttribute(“followupby”).getValue();
var currentTime = new Date();
currentTime.setHours(0, 0, 0, 0);
if (reviewDate < currentTime) {
//kpi incomplete
Xrm.Page.getAttribute(“meta_kpistatus”).setValue(951850001);
} else {
//KPI MET
Xrm.Page.getAttribute(“meta_kpistatus”).setValue(951850000);
}

CRM 2011 – Quick tip – Javascript to stop a form saving

This is probably something you will need to do at some point.  You want to validate a page and if it doesn’t fit certain criteria then the form should not be saved until the user has met your criteria.

To stop the save event in Javascript all you need is this

event.returnValue = false;

Tanguy the CRM tool guy (and CRM MVP) put something very interesting in the comments but I thought I would promote it to this very small blog post

The new method in CRM 2011 is the following one:

Xrm.Page.context.getEventArgs().preventDefault();

preventDefault : Cancels the save operation, but all remaining handlers for the event will still be executed.