Scenario
I am changing the account form header based on the value of customertypecode option set field.- If the option set value is 1 then change the form header to red.
- If the option set value is 2 then change the form header to green.
- Else keep the default colour.
Solution
The solution consists of 3 web resources- CSS web resource for red colour
I have named this web resource “new_/ColoredHeaderRed.css”. Here is the code.
.ms-crm-Form-HeaderContainer{ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#FF0000, endColorstr=#fff6f8faE); }
- CSS web resource for green colour
I have named this web resource “new_/ColoredHeaderGreen.css”. Here is the code.
.ms-crm-Form-HeaderContainer{ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#008000, endColorstr=#fff6f8faE); }
- JavaScript web resource to attach the relevant CSS file to the form
I have named this web resource “new_/ColoredHeaderScript.js”. The code is using a switch statement to attach an appropriate CSS file to the form, based on the value of customertypecode field. I am not using default in my switch statement as I don’t want to create a CSS file for default colour. Here is a code.
function loadCSS() { // get the value picklist field var relType = Xrm.Page.getAttribute("customertypecode").getValue(); var filename; switch (relType) { case 1: filename="/WebResources/new_/ColoredHeaderRed.css"; attachCSS(filename); break; case 2: filename="/WebResources/new_/ColoredHeaderGreen.css"; attachCSS(filename); break; } }// end function function attachCSS(filename){ var fileref = document.createElement("link"); fileref.setAttribute("rel", "stylesheet"); fileref.setAttribute("type", "text/css"); fileref.setAttribute("href", filename); document.getElementsByTagName("head")[0].appendChild(fileref); }Now call the loadCSS() function on account form load event.
Save and publish the changes and test the solution.
Results
If the option set value is 1.If the option set value is 2.
We did this too, but used colours from Dynamics's pallet:
ReplyDeletecolor = "#b9fdb0";
color = "#ffffae";
color = "#b0e9fd";
color = "#ffaeae";
color = "#ffaeae";
good stuff mate.
DeleteHi..
ReplyDeleteI tried this function in my one of the custom entity but the color is not changing.Its in same colors.Same steps I followed...
Microsoft Dynamics CRM training in Hyderabad | Microsoft Dynamics CRM online training
ReplyDeleteDoes it work for CRM online ?
DeleteHi Amreek,
ReplyDeleteI am Trying to use your code but it is not working. I am not getting ant error also.
Can you tell me what is to checked or what is wrong.
Regards
Mrp
It should. Can you paste your code and I have a look.
DeleteI am Trying to use your code but it is not working. I am not getting ant error also.
ReplyDeleteThe code:
function loadCSS()
{
var relType = Xrm.Page.getAttribute("new_apoio").getValue();
var filename;
switch (relType) {
case 2:
filename="/WebResources/ColoredHeaderYellow.css";
attachCSS(filename);
break;
case 3:
filename="/WebResources/ColoredHeaderGreen.css";
attachCSS(filename);
break;
case 4:
filename="/WebResources/ColoredHeaderRed.css";
attachCSS(filename);
break;
}
}
function attachCSS(filename){
var fileref = document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", filename);
document.getElementsByTagName("head")[0].appendChild(fileref);
}
Can help??
Without looking into details. I think your webresource names are wrong. It should have some sort of prefix.
DeleteGo to the webresource and look at the URL.
Thanks
Did you ever get this to work? I am also having difficulty displaying the colors, however there is no error.
DeleteThanks for the instructions this out me on the path to what I need.
ReplyDeleteJust got the colors to work so far. I added the entire URL to the filename 'http://servername/org/webresources/filename' to get the colors to show up.
Also the line that calls the gradients is for IE 6 and 7. Go to the below for updated versions of it.
http://msdn.microsoft.com/en-us/library/ms532887(v=vs.85).aspx
Thanks mate.
Deletehi,
ReplyDeletei used your code exactly and nothing is happening
i could work it out if i at least got an error but am getting nothing and the form is not changing
any help please!!
Curious if you ever got this code to work? I'm trying to accomplish the same thing today and no error messages and color does not change. I know it's been 1 1/2-years but hopefully you recall what your resolution was. Thanks mark
DeleteThis is due to changes in IE Browser. Refer to link below to get it working.
Deletehttp://stackoverflow.com/questions/16148551/ie-10-attach-css
Is this compatible with CRM 2015 Online/Onpremise environemnt?
ReplyDeleteThanks!
Hi,
ReplyDeleteI just tried this solution in CRM Online and doesn't seem to work here. I guess the CSS class has changed over time. But even by amending that I am not able to get this to work. I need to colorize the header in CRM online in dependence of the relationship field. Any ideas how to achieve this?
Many thanks!
Thanks Mate the code works just fine.
ReplyDeleteJust a quick question can I do the same thing to the body of the form ??
or is there a different way ??
I Tried your same code with same scenario..
ReplyDeletewhile running this code onload am not getting any error also..
Its not working ..
Here is my code
function loadCSS(executionContext)
{
// get the value picklist field
var formContext = executionContext.getFormContext();
var relType =formContext.getAttribute("customertypecode").getValue();
var filename;
switch (relType) {
case 1:
filename="/WebResources/new_ColoredHeaderRed.css";
attachCSS(filename);
break;
case 2:
filename="/WebResources/new_ColoredHeaderGreen.css";
attachCSS(filename);
break;
}
}// end function
function attachCSS(filename){
var fileref = document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", filename);
document.getElementsByTagName("head")[0].appendChild(fileref);
}