Friday, May 11, 2012

MSCRM 2011: Automatically filling the Name Field of a Custom Entity through Javascript.

When you create a custom entity in Microsoft Dynamics CRM, the "Name" field of the entity is a required field. If this field serves no useful purpose for your system, it could just be an extra field that needs to be populated in order to save a new record. This field can be set to be not required BUT it cannot be taken off the form therefore it would be a benefit to auto populate it through Java Script.

You could easily auto populate that field with a static value; however, since this field shows up (and is required) in many of the system views and well as the Lookup field on related forms, this is probably not the best option. A better option might be to have some javascript automatically populate it with some information from the record that is relevant. In this example we will populate the name field with the name of an Account chosen from a Lookup field and the current Date/Time that the new record was created/modified. This will make the Name field useful while satisfying CRM's requirement for wanting the field populated with some value.

Below find the instructions for populating the name field in a custom entity “Daily Bookings” with an Account chosen from a Lookup field and the current date then concatenated to that value:

* Task 1 – Create a new JavaScript Library
* Go to Settings, Customizations, Customize the System.
* Click New from the toolbar and select Web Resource from the list.
* Enter the following Information in the New Web Resource Dialog:
* Name= “SetName”
* Display Name= “Set Name”
* Type= “Script (Jscript)”
* Click the Text Editor button
* Paste the following script in to the source dialog.

function SetName(){
var lookupField = Xrm.Page.getAttribute("new_accountid");

// Retrieve the Current Date

var Now = new Date();

// Convert the Date to a String

 var StringNow = Now.toString();

// Set the Name         

Xrm.Page.getAttribute("new_name").setValue(lookupField.getValue()[0].name + "-" + StringNow);
}        

* Click OK
* Click Save and Close

* Task 2 - Attach JScript function to onLoad event handler
* Next we need to setup the event handlers on the Daily Bookings form.
* Click on the Entities.
* Select Daily Bookings in the entity list and then click OK.
* When the Daily Bookings appears in the list of available entities in your solution, expand the Daily Bookings node and then expand the Forms node.
* In the forms grid, double-click Information form type Main
* Now that you are in the form editor we need to attach the event handler to the Form onLoad event.
* Click Form Properties in the ribbon to bring up the dialog.
* Click the Form Libraries Add button.
* In the lookup dialog select the “Set Name” web resource and Click OK
* In the Event Handler section Click Add.
* In the Handler Properties dialog do the following: Function = ” SetName”, Check Enabled, Check Pass execution context as first parameter
* Click OK, to close the Handler Properties dialog
* Click OK, to close the Form Properties dialog

* Task 3 - Publish changes and see the results
* While still in the Customizations, Click Publish All Customizations in the top toolbar of the Solution Explorer.
* Close the Solutions explorer

Now when a Daily Bookings entity is created or modified, the name field will be auto-populated.

12 comments:

Anonymous said...

You can also just make it read only and hide it and problem is solved.

Anonymous said...

The actual suspensions is built to match for your encounter perfectly. This has a quantity of stage shoulder straps that are flexible in order to completely match in your mind. These people achieve this without having pinching or even pushing your face. A person might wish to follow the actual fake chanel influenced appear for two times, or simply throughout the period. Think about buying and selling away clothing as well as add-ons having a buddy if you do not have all of the items you want you'd in advance. Perform a make an online search to locate web sites that provide most recent reproduction bags-style most recent reproduction totes to obtain discount rates and look for methods to put on your own fake louis vuitton bags. Airsoft face masks are made to make sure that you tend to be correctly guarded whenever actively playing the actual paintball online game. These people are created to safeguard your own le boy chanel bags towards any kind of item that might strike a person throughout perform. becoming noticed because of your competitors. For example eco-friendly suspensions is actually perfect whilst actively playing within the eco-friendly areas. Your own challenger won't be in a position to place a person very easily. The actual Airsoft face masks tend to be long lasting. These people are manufactured from the actual most powerful supplies such as the fiberglass utilized in vehicles as well as boats. They're as well powerful. Their own power may just end up being when compared with the handbags stay. Following importing picture about the chanel outlet, there are lots of option methods which are will not in order to update the appearance from the cheap longines watches.

for IT the said...

Great Article
Java Online Training | Java EE course

Java Training in Chennai | J2EE Training in Chennai | java j2ee training institutes in chennai ~ Java Course in Chennai | Java Training Institutes in Chennai

Java 360 | IT Technical Articles |Java Training Institutes

for IT the said...

Great Article

HTML5 Online Training | JavaScript Training in Chennai | JavaScript Course | Javascript Online Course | Angularjs Training in Chennai | Backbone.JS Training in Chennai | Bootstrap Training in Chennai | Node.js Training in Chennai

Addison adolf said...

your Article in too Good......

e commerce web designing

M Sunny said...

SMS Marketing Software is User-friendly and cost-effective text messaging software empowers users to send bulk SMS in this site Professional SMS Marketing

Hola Launcher said...

The share your really gives us excitement. Thanks for your sharing. If you feel tired at work or study try to participate in our games to bring the most exciting feeling. Thank you!
red ball 4 | hola launcher | cooking fever | paradise bay | agario

Messenger Indir said...

This is a great article. It gave me a lot of useful information. thank you very much. Can you play more games at :
messenger download |messenger indir | messenger | messenger indir | download messenger | messenger indir | messenger indir

Kayal Vizhi said...

Good post. Happy to visit your blog. Thanks for sharing.


Web application development services in chennai

Anthony Morano said...

It's not the easiest code that I have seen for the decision of such issues. This is probably due to the feature of the service provider in your area. Good service for sending SMS https://www.intistele.com/blog/wwc-madrid/. No need to get attached to the country or region, if you have internet access.

devikamahajan said...

IoT Consulting
IoT Solutions

Craft Mine said...

Great information, I will tweet to my friends to get them to check it out. keep it up. Thanks for sharing!.If have a long time than visit to:
minecraft | facebook baixar | whatsapp baixar | baixar facebook | photo grid