Search This Blog

Wednesday, September 30, 2015

Get current user roles using javascript in CRM

If you have any doubt in the post please post comments. I will try to solve your problem.


In this article , I am going to explain how to get current user roles


Current user roles
var UserRoles = Xrm.Page.context.getUserRoles();

Open CRM form with parameters using javascript

If you have any doubt in the post please post comments. I will try to solve your problem with in a day.

To open the form using javascript.
Opens an entity form for a new or existing entity record using the options you set as parameters.

Xrm.Utility.openEntityForm(name,id,parameters,windowOptions)

Parameters


NameTypeRequiredDescription
name
String
Yes
The logical name of the entity.
id
String
No
The string representation of a unique identifier or the record to open in the form. If not set, a form to create a new record is opened.
parameters
Object
No
A dictionary object that passes extra parameters to the form. Invalid parameters will cause an error.
Valid extra query string parameters are:
windowOptions
Object
No
For Microsoft Dynamics CRM Online 2015 Update 1 or later use this optional parameter in the web application to control how the form opens. You can choose to open a form in a new window by passing a dictionary object with a Boolean openInNewWindowproperty set to true.
This parameter is ignored in CRM for tablets and CRM for phones.


Xrm.Utility.openEntityForm("account");
Open an existing account record using the default form
Xrm.Utility.openEntityForm("account","A85C0252-DF8B-E111-997C-00155D8A8410");
Open a new account record with a specific form and setting default values
var parameters = {};
parameters["formid"] = "b053a39a-041a-4356-acef-ddf00182762b";
parameters["name"] = "Test";
parameters["telephone1"] = "(425) 555-1234";
Xrm.Utility.openEntityForm("account", null, parameters);
Open a new account record using the default form in a new window(Microsoft Dynamics CRM Online 2015 Update 1 or later will use function)
var windowOptions = {
openInNewWindow: true
};
Xrm.Utility.openEntityForm("account",null,null,windowOptions);
Open a new account record using the default form in a new window(before CRM online 2015 update1)
Open webResource
Xrm.Utility.openWebResource(webResourceName,webResourceData,width, height)
Parameters

ParameterDescription
etn
The logical name of the entity.
ImportantImportant
Do not use the etc (entity type code) parameter that contains an integer code for the entity. This integer code varies for custom entities in different organizations.
extraqs
Optional for forms. This parameter contains encoded parameters within this parameter.
Use this parameter to pass values to a form. For more information, see Set field values using parameters passed to a form.
When an entity has more than one form defined, you can use this parameter to specify which form to open by passing the encoded parameter formid with the value equal to the ID value of the form. For example, to open a form with the ID of ‘6009c1fe-ae99-4a41-a59f-a6f1cf8b9daf’, include this value in the extraqs parameter: formid%3D6009c1fe-ae99-4a41-a59f-a6f1cf8b9daf%0D%0A.
pagetype
The type of page. There are two possible values:
  • entityrecord

    Displays an entity record form.
  • entitylist

    Displays an entity view.
id
Optional for forms. Use this when you open a specific entity record. Pass in the encoded GUID identifier for the entity. The encoded version of the GUID substitutes opening and closing brackets “{“ and “}” with “%7B” and “%7D”, respectively, for example {91330924-802A-4B0D-A900-34FD9D790829} is %7B91330924-802A-4B0D-A900-34FD9D790829%7D.
viewid
Required for views. This is the ID of the savedquery or userquery entity record that defines the view. The easiest way to get the URL for a view is to copy it. For more information, see Copy the URL for a View.
viewtype
Defines the view type. Possible values are as follows:
  • 1039

    Use for a system view. The viewid represents the Id of a savedquery record.
  • 4230

    Use for a personal view. The viewid represents the Id of a userquery record.
navbar
Controls whether the navigation bar is displayed and whether application navigation is available using the areas and subareas defined in the sitemap.
  • on

    The navigation bar is displayed. This is the default behavior if the navbar parameter is not used.
  • off

    The navigation bar is not displayed. People can navigate using other user interface elements or the back and forward buttons.
  • entity

    On an entity form, only the navigation options for related entities are available. After navigating to a related entity, a back button is displayed in the navigation bar to allow returning to the original record.
cmdbar
Controls whether the command bar is displayed.
noteNote
This capability supports requirements for the Unified Service Desk for Microsoft Dynamics CRM application.
Using this to display an entity form within an IFrame embedded in another entity form is not supported.

  • true

    The command bar is displayed. This is the default.
  • false

    The command bar is hidden.
Example:
We want to open the project quote entity in the new window and when we click save& close, the window must be close.

Open the project quote entity in a new window and have close window when click save & close
//Open the project quote in a new window
  var strURL = "main.aspx?etc=10046&pagetype=entityrecord&newWindow=true&histKey=888185290&extraqs=";
//histKey=888185290:  If histkey is not available then if you click save & close window will close instead it just show the list of projectquote in grid.
 // When you give histkey in the URL then click save & close button window itself close.
//Form the parameter when the form is open set the default value to fill in the form.
//Set projectstatus as draft when the form is open.
  var strQuoteId = "kti_projectquotestatus=963548";
//you can also pass multiple parameter with & symbol.
  var strQuoteStatus = "&Description='new value filled'";
   var strExtras = strQuoteId + strQuoteStatus;
    //Set features for how the window will appear.
   var features = "location=no,menubar=no,status=no,toolbar=no";
   window.open(strURL + encodeURIComponent(strExtras), "_blank", features, false);



NameTypeRequiredDescription
webResourceName
String
Yes
The name of the HTML web resource to open.
webResourceData
String
No
Data to be passed into the data parameter.
width
Number
No
The width of the window to open in pixels.
height
Number
No
The height of the window to open in pixels.
Open a new account record using the default form
 window.open(URL,name,feature,false);


URL: url of the application in crm (main.aspx)
  window.open(strURL + encodeURIComponent(strExtras), "_blank", features, false);

name 
String that specifies the name of the window. This name is used as the value for the TARGET attribute on a formor an anchor element.
_blank
The url is loaded into a new, unnamed window.
_parent
The url is loaded into the current frame's parent. If the frame has no parent, this value acts as the value_self.
_self
The current document is replaced with the specified url.
_top
The loaded url replaces any framesets that might be loaded. If there are no framesets defined, this value acts as the value _self.

Handle window close and save & close event in CRM

Handle Save, Save & close and unload of CRM form


If you have any doubt in the post please post comments. I will try to solve your problem.

This is a tricky situation one can come across in CRM 4.0 development. Leaving the form can happen in three ways. Those are SAVE, SAVE AND CLOSE, UNLOAD
We can write JavaScript for all three occasions.

UNLOAD

If you put below code in onLoad event, you can execute a script on unload.

// Put this in onload
window.onbeforeunload = function()
{
    alert("unload event")      
}


SAVE & SAVE AND CLOSE

Saving is straight forward in CRM form, but you can distinguish two ways of saving by event mode. Try below script in save event.


var _mode = event.Mode;
alert("Save event .. with mode : " +_mode);

Different modes could be easily distinguished as blow. 



SAVE - 1 
SAVE AND CLOSE – 2 

Now my real focus is to do something when we leave a form with any of above three methods. Best example would be Quote entity can have total fields which depend on quotedetail entities under it (quotedetai is a child of quote). If we need them to be accurate at any given moment... we might need to do the calculations (unless we do it in plug-ins) whenever any of above events occur.

Way above event occurs are quite messy. 
Please check what I found. Different combination of events trigger depends on circumstance.

SAVE (without change to form data) - Save event
SAVE (with change to form data) - save event, unload event
SAVE AND CLOSE (without change to form data) - save event, unload event
SAVE AND CLOSE (with change to form data) - save event, unload event

So, if we place the calculation event in all the three events, nothing will go wrong but in some occasions, it will run twice. Still I don’t know how to solve this simply. What we could do is maintaining a hidden check box (Boolean field) to say whether form needs recalculation. We should be smart enough to update this field even when child records are changed, if they influence the calculations.

Then we can add our own calculation methods to all three events, but calculation should always check the Boolean field before execution. In this way, we can avoid happening of calculation twice for no reason.

I am glad to hear if someone got better approach for this.

Tuesday, September 22, 2015

Date format setting based on user or Business Unit(BU) in CRM

Date format setting based on user or Business Unit(BU)  in CRM

If you have any doubt in the post please post comments. I will try to solve your problem.

We need to display different date time format based on Business Unit(BU). By default in CRM display date time format based on BU is difficult but based on user we can achieve it. Of course we have to achieve it by change date time format for the particular BU user.


Click setting icon in CRM environment and click options

Change the specific current format in  drop down and click ok

We can do it similarly for all the specific BU user.we can also update in database.