Showing posts with label Javascript CRM 2011. Show all posts
Showing posts with label Javascript CRM 2011. Show all posts

Saturday, April 7, 2012

Startup script Onload AND Onsave! Basic's for every Jscript!

Tired of having XX web-resurces on a form? Well.. Now you can controll everything within one JS with only 1. trigger:
- OnChange
- Onload
- OnSave

as well as:
- For all form types: OnCreate, OnNewForm, onReadonly ++

Read more about this in my previous post: Best javascript startup script ever
/* Script Entry Point */
function OnCrmPageLoad() {
 var FormTypes =
 {
Undefined: 0,
Create: 1,
Update: 2,
ReadOnly: 3,
Disabled: 4,
QuickCreate: 5,
BulkEdit: 6
 }
 runAlways();
 switch (crmForm.FormType) {
 case FormTypes.Create: OnNewFormLoad(); break;
 case FormTypes.Update: OnUpdateFormLoad(); break;
 case FormTypes.ReadOnly: OnReadOnlyFormLoad(); break;
 case FormTypes.Disabled: OnDisabledFormLoad(); break;
 case FormTypes.QuickCreate: OnQuickCreateFormLoad(); break;
 case FormTypes.BulkEdit: OnBulkEditFormLoad(); break;
 case FormTypes.Undefined: alert("Error"); break;
 }
}
/* Implement each Form Type you wish to address */
function runAlways() {
 onchange();
 Xrm.Page.data.entity.addOnSave(addToOnSave);
}
function OnNewFormLoad() {
}
function OnUpdateFormLoad() {}
function OnReadOnlyFormLoad() { }
function OnDisabledFormLoad() { }
function OnQuickCreateFormLoad() { }
function OnBulkEditFormLoad() { }
/******************** OnChange and OnSave functions ***************************/
function onchange(){
 //Skriv inn alle felter som skal ha OnChange
 Xrm.Page.data.entity.attributes.get("prefix_field").addOnChange(Summering);
}
function addToOnSave() {
 var SAVE_MODE_SAVE = 1;
 var SAVE_MODE_SAVEANDCLOSE = 2;
 // Validate only if the user clicked "Save".
 switch (event.Mode){
 case SAVE_MODE_SAVE:
  //Do something for "save"
  if(/*write you check*/) {
   // Cancel the save operation.
   event.returnValue = false;
   return false;
  }
  break;
 case SAVE_MODE_SAVEANDCLOSE:
  //Do something for "Save and close"
  if(/*write you check*/) {
   // Because this is a "Save and Close",
   // just save the form.
   return true;
  }
  break;
 }
}
/*************** End OnChange and OnSave functions ***************************/
/* --------------- Private functions ---------------------*/
function Summering(){
alert("You are inside a function triggerd from a onChange");
}
/* ------------ End Private functions------------------*/

Friday, January 13, 2012

Several Basic Javascripts part 2

Hi, more basic javascripts!! Yes, we love them and hate them! Please read Several Basic Javascripts Part 1 aswell!

Get value of lookup: Xrm.Page.getAttribute("prefix_fieldname").getValue()[0].name
- read method as: get array 0's name (or Id)

Set Value of lookup:
var idValue = "2B0CDF21-7C04-E011-8ABA-00155D011B05"; //GUID of record
var textValue = "Prisliste"; //Name of record
var typeValue = "Pricelevel"; //Schema name of record entity
Xrm.Page.getAttribute("prefix_fieldname").setValue([{id: idValue, name: textValue, entityType: typeValue}]);

Show/hide section: Xrm.Page.ui.tabs.get(1).sections.get("Section_name").setVisible(false)
Set visible values: true or false
Tab value: number (1) or "name"
Section value: "name_of_section"

Show/hide field: Xrm.Page.getControl("prefix_fieldname").setVisible(false)
Set values: true or false

Show/hide tab: Xrm.Page.ui.tabs.get(5).setVisible(false);
Set Values: true or false

Save: Xrm.Page.data.entity.save();
Optional params: "saveandclose" or "saveandnew"

Set Require lvl: Xrm.Page.getAttribute("prefix_fieldname").setRequiredLevel('required');

Get length of string:
var oField = Xrm.Page.getAttribute("prefix_fieldname").getValue();
alert(oField.length);

Substract content of string:
var oField = Xrm.Page.getAttribute("prefix_fieldname").getValue();
alert(oField.substr(0,7);

Ex: "Consultancy" --> after substract: "Consult"

Set Focus:
Xrm.Page.getControl("prefix_fieldname").setFocus(true);

Hope you found this usefull! :)

Thursday, November 24, 2011

Several basic Javascript commands CRM 2011

Case switch:
var valueToSwitch = Xrm.Page.getAttribute("yourField").getValue();

switch(valueToSwitch)
{
case 0:
//what to do if value = 0
break;
case 1:
//what to do if value = 1
break;
case 2:
//what to do if value = 2
break;
}
 Catch error:
try {
//Code
}
catch (ex) {
var txt="You got the following error\n\n" + ex;
alert(txt);
}
Force submit (fields that are marked read only - make scripts able to write to them)
Xrm.Page.getAttribute("youField").setSubmitMode("always"); 
 Forminfo check:
function forminfo()
{
var items = Xrm.Page.ui.formSelector.items.get();
if (items.length > 1)
{
alert("Formtype: " + Xrm.Page.ui.getFormType());
alert("Form ID: " + Xrm.Page.ui.formSelector.getCurrentItem().getId());
alert("Formname: " + Xrm.Page.ui.formSelector.getCurrentItem().getLabel());

else
{
alert("There is only one form item currently available.");
}
}
 Maximize window
window.top.moveTo(0,0);
window.top.resizeTo(screen.width, screen.height);
 Get name of selected picklist value
Xrm.Page.data.entity.attributes.get("crayon_fagomrde").getText();
 Start Dialog from Form (note tings to change)
var dialogId = "a13ad982-d812-40a0-ab67-f314cdabbd2b"; // This must be your Dialog ID
var returnValue = showModalDialog("/" + Xrm.Page.context.getOrgUniqueName() + "/cs/dialog/rundialog.aspx?DialogId=%7b" + dialogId + "%7d&EntityName=account&ObjectId=" + Xrm.Page.data.entity.getId());

//Optional:To save and close entity form as soon as the dialog is closed.
//Xrm.Page.data.entity.save("saveandclose"); 
 Set URL to Iframe
var url = http://www.crayon.com
Xrm.Page.getControl("IFRAME_iFrameName").setSrc(url); //("about:blank") if black
 This was some of the basic javascripts..

Hope it's usefull for you!

Sunday, November 13, 2011

How to Hide Top Bar, Ribbon, Quick Launch in SharePoint 2010?

Ribbon, Top Bar and Quick Launch comes up with Application Pages by default. If you want to open the page in Dialog Framework then those elements will take more space and confuse the enduser. You can remove these elements by creating new page layout which is time consuming.

There is other ways to hide these controls. One simple way is by passing a Parameter with the URL. [&IsDlg=1]
Under I'll show an example of SharePoint 2010 viewed in iFrame CRM 2010..
function setturl()
{
//Field with URL - Field have to be created first 
if(Xrm.Page.getAttribute("prefix_url").getValue() != null){
/*
//Remove "Documents botton" in default CRM 2010
var crmNavBar = "navDocument";
var tdAreas = "_NA_Info";
document.getElementById(crmNavBar).parentElement.style.display = "none";
document.getElementById(tdAreas).parentElement.parentElement.parentElement.parentElement.colSpan = 2;


//Show a custom "Document tab" - Have to be created first
Xrm.Page.ui.tabs.get("docs").setVisible(true);

*/ 
var accountURL = "Insert you default URL for SharePoint server ink the libarary name - End it with /";
// example SharePoint 365 Online: "https://testcompany.sharepoint.com/DMCRM/account/Forms/AllItems.aspx?RootFolder=%2FDMCRM%2Faccount%2F

//The relative URL. Is the folder in the libary entert in accountURL 
var relativ = encodeURI(Xrm.Page.getAttribute("prefix_url").getValue());
//example: test%20company%2F

//Remove Ribbon, Quick luch top bar  
var removeSPmenu = "&IsDlg=1"

//Re-create URL with SharePoint site content only.   
var newURL = (accountURL + relativ + removeSPmenu);
//alert("The new URL: " + newURL);
//example test alert: The new URL:
https://testcompany.sharepoint.com/DMCRM/account/Forms/AllItems.aspx?RootFolder=%2FDMCRM%2Faccount%2Ftest%20company%2F&IsDlg=1

//iFrame on form called "iframe_docs" get's new URL
Xrm.Page.getControl("IFRAME_docs").setSrc(newURL);
}
}
SharePoint will hide these elements when It gets IsDlg Parameter on URL.

This only work in SharePoint 2010, but then showing SharePoint 2010 via iFrame in CRM 4.0/CRM 2011 this trick is very handy!
But every time we can't pass these parameters. This is when you'll have to edit the CSS code in the Application Page under PlaceHolderMain scetion:
  
<style type="text/css">
#s4-ribbonrow, .ms-cui-topBar2, .s4-notdlg, .s4-pr s4-ribbonrowhidetitle, .s4-notdlg noindex, #ms-cui-ribbonTopBars, #s4-titlerow, #s4-pr s4-notdlg s4-titlerowhidetitle, #s4-leftpanel-content {display:none !important;}
.s4-ca{margin-left:0px !important; margin-right:0px !important;}
</style>

This will hide the Ribbon, Top Bar and Quick Launch and only show the content.

Note : If you use this code in SharePoint Site Pages with Content Editor Webpart then you can't check in the page since the toolbar goes hidden.

If you still want to use this code on Site Pages then add Content Editor Webpart and paste the above code. When you want to make other changes then open the page in browser with parameters [ ?Contents=1 ] which opens the page in WebPart maintenance mode. Remove the content editor webpart and re-open the page. Make the changes and add content editor webpart at last along the CSS Code. To check in the page use Site Pages Library.

Hope you found this tip usefull! :)

Thursday, November 10, 2011

"Start-Up - Script" for MS CRM 2011

This script can contain every aspect of scripting with only one trigger point. It also checks the FormType state for "Create", "update" and so on.. This javascript is used by many people already, but I've updated it with "onchange" aswell:

The only thing you'll have to do is: Add this as a webresource to the entities Form that needs javascript. and add the function "OnCrmPageLoad" to the "Form OnLoad" tirgger point!
/* Script Entry Point */
function OnCrmPageLoad() {
var FormTypes =
{
Undefined: 0,
Create: 1,
Update: 2,
ReadOnly: 3,
Disabled: 4,
QuickCreate: 5,
BulkEdit: 6
}
runAlways();
switch (crmForm.FormType) {
case FormTypes.Create: OnNewFormLoad(); break;
case FormTypes.Update: OnUpdateFormLoad(); break;
case FormTypes.ReadOnly: OnReadOnlyFormLoad(); break;
case FormTypes.Disabled: OnDisabledFormLoad(); break;
case FormTypes.QuickCreate: OnQuickCreateFormLoad(); break;
case FormTypes.BulkEdit: OnBulkEditFormLoad(); break;
case FormTypes.Undefined: alert("Error"); break;
}
}
/* Implement each Form Type you wish to address */
function runAlways() {
onchange(); //Loads all fields that should have onchange functions on them
}
function OnNewFormLoad() {}
function OnUpdateFormLoad() {}
function OnReadOnlyFormLoad() { }
function OnDisabledFormLoad() { }
function OnQuickCreateFormLoad() { }
function OnBulkEditFormLoad() { }
/* --------------- Below is all the Private Functions ---------------------*/
function onchange(){
Xrm.Page.data.entity.attributes.get("prefix_fieldname").addOnChange(examplefunction);
}
function examplefunction(){
alert("Hello World");
}
Hope you like this script! It's one of the "life saving" scripts for CRM 2011
(it's also avaiable for MS CRM 4.0)
- Happy Scripting!