Showing posts with label Script. Show all posts
Showing posts with label Script. Show all posts

Thursday, March 21, 2013

How to save read-only fields in CRM 2011

What was I trying to do?

My CRM form had few calculated fields which were populated using Jscript. And since these were calculated, I didn't wanted users to intervene. So I made them read-only!

After customizing the form and script, I open the form as a user and tried to save the data on the form by clicking the Save button and I closed the form.

Next time I reopened the form and the read-only fields were empty (the opens used as calculated fields based on javascript).

Solution

On the OnSave event of the form, insert this snippet:
Xrm.Page.getAttribute("fieldName").setSubmitMode("always");

The fieldName is the read-only field that was updated using Jscript. If there are multiple 'Jscript updated read-only' fields on your form, you have to duplicate the above snippet for those fields as well.

Hope it helps!


Wednesday, June 10, 2009

Filtering Lookups in CRM 4

Lets get straight to the solution. Suppose you want to filter the Primary Contact lookup on the Account entity to show only contacts that belong to the data in the Parent Account field.


Below is a simple JavaScript code to achieve that.

  • On the forms OnLoad event write the following code.
/* Form.onLoad() */
FilterLookup = function(source, target)
{
if (IsNull(source) || IsNull(target)) { return; }
var name = IsNull(source.DataValue) ? '' : source.DataValue[0].name;
target.additionalparams = 'search=' + name;
}

  • On the OnChange event of the Parent Account lookup mention the below code.
/* onChange() */
FilterLookup(crmForm.all.parentaccountid, crmForm.all.primarycontactid);

Note : Before you can see the code working, check the Parent Customer checkbox in the Contact Lookup View. Steps ->
  • Settings > Customization > Customize Entites > Contact > Forms & Views > Contacts Lookup View
  • Click on Add Find Columns on the right
  • Tick Parent Customer
  • Click OK and Publish

After performing the above mentioned, I select "A Bike Store" as the Parent Account and click on the Primary Contact Lookup.

The Contact lookup opens(as shown in the figure below) and you can see "A Bike Store" in the search box & CRM is only returned those Contact records whose Parent Customer is "A Bike Store."

Thursday, May 28, 2009

Stop CRM Form Save

Here I give you an example of what code you might want to consider to achieve the CRM form from saving as you could either use
  • event.returnValue = false; or
  • return false;
To know what's the difference between the two, click on the links below.

Code : alert('You clicked C Drive.'); event.returnValue=false;
Code : event.returnValue=false; alert('You clicked Drive C.');

Observe the above two code and their behavior. When you click on C Drive, you receive an alert box and then the function returns false whereas when you click Drive C the function returns false first but then still goes ahead to and displays the alert.

When you just return false, you will notice a different behavior. Try the below links:

Code : alert('You clicked C Drive.'); return false;
Code : return false; alert('You clicked Drive C.');

This time when you click on C Drive, you receive an alert box and then function returns false whereas when you click Drive C the function returns false and the alert isn't displayed.

I believe with this information you are better equipped on what to use and when.

Tuesday, May 12, 2009

Hide / Unhide CRM Form Tabs

Did you know that you could hide and display CRM form tabs. Of course most of you did. Let me give you an example. To hide the second tab on the account form, this is what you should be doing.

crmForm.all.account.tab1Tab.style.display = "none";

My intend is not to convey how to hide a tab [I just did ;)] but also why "tab1Tab"?
I suggest you to have a look at the HTML source on the Account form IE8 > Page > View Source, You'll notice that the tabs have an ID on them (which go tab0Tab, tab1Tab, tab2Tab,........). We just refer them and then use JavaScript's CSS properties to achieve the result.

Caution: Something to keep in mind is if Microsoft decides to change the tab IDs in future releases, you're done! So make sure you have documented these customizations to easily accept future changes.

Thursday, March 12, 2009

Using window.opener to refer to Parent CRM window's field

There are many ways you could go to the parent CRM window, but this is what I prefer. At first, I followed the SDK example, but then I invented my own. You could follow the SDK if you will?

Even though it is a little JavaScript but the challenge here is to know if the parent window is the one YOU are looking for?

So here is a little chunk of code that confirms you the Parent Window.

if(window.opener) //checks if current window has a parent window
{
   var oParentForm = window.opener.parent.document.getElementById('crmForm');
   if(oParentForm) //This confirms if the parent window is a CRM form
   {
      // Now try to access a field in the parent form
      // like name attribute in Account entity
      if(oParentForm.all.name) //This ensures you have reached the form you're looking for
      {
         // Proceed with your code
      }
   }
}
There could be other methods too, like checking the Object Type Code of the parent form instead of the 'name' field.

More ideas are welcome, don't hesitate, but share.

Saturday, October 11, 2008

Javascript - Know how v1.0?

To know the protocol your current window is using:


What just happened is I had this little snippet below the "Click Here - Protocol" Button.
alert(window.location.protocol);

To know who is hosting my blog:


The executing code under "Click Here - Hostname" button is.
alert(window.location.hostname);

Application :- When we open an AJAX call from within CRM, we can generate the URL like this:
{
var Protocol = window.location.protocol;
var HostName = window.location.hostname;
var sURL = Protocol+'//'+HostName+'/webservice.asmx';
}

at runtime and pass it this way:
xmlHttp.Open('POST',sURL,true);

A useful piece of information I use it often now.

Friday, September 12, 2008

Passing CRM Form values to an IFrame

I assume that you have already added an IFrame to the entity amd unchecked "Restrict cross-frame security" under IFrame properties > Security section

PASSING SINGLE PARAMETER TO AN IFRAME :

  • new_customfield1 - Schema name of my custom field.
  • IFRAME_Custom - Name of the IFrame.

Snippet (You may place it on OnLoad or OnChange of the CRM Form as per your requirements)

var CustomFieldValue = crmForm.all.new_customfield1.DataValue;
crmForm.all.IFRAME_Custom.src = 'http://localhost/YourURL.aspx?value='+CustomerFieldValue.

PASSING MULTIPLE PARAMETERS TO AN IFRAME :

  • new_customfield1 - Shema name of first custom field.
  • new_customfield2 - Schema name of the second custom field.
  • IFRAME_Custom - Name of the IFrame.

Snippet

var CustomField1 = crmForm.all.new_customfield1.DataValue;
var CustomField2 = crmForm.all.new_customfield2.DataValue;
crmForm.all.IFRAME_Custom.src='http://localhost/YourURL.aspx?value1='+CustomField1+'&value2=' + CustomField2;

Tuesday, August 5, 2008

Apply colors to CRM form labels

To give the CRM form labels a special appearance you may want to have different colors to them.

Write this code snippet on OnLoad Event of the form:
var str = "";
str += "First Name";
str += "<SPAN style='color:blue'> Only for info. </SPAN>";
crm_form_label.innerHTML = str;

Happy coloring form labels!!

Wednesday, May 21, 2008

Displaying CRM 4.0 form field and tab

Here is the blog post if you want to hide the fields

To display a CRM form field, the below scripts must be used:
crmForm.all.new_field_c.style.display = "inline";
displays the label and

crmForm.all.new_field_d.style.display = "inline";
displays the TextBox.

Similarly to display a tab use
crmForm.all.tab1Tab.style.display = "inline";

Note: These are unsupported customizations.

Hiding CRM form fields and tabs

If you're looking for Display a hidden field or tab - Direct Here.
These apply to Microsoft Dynamics CRM 4.0. Note - These are unsupported customizations and may not work on CRM Upgrades.

Avoiding Scenarios where you can apply this functionality, I am just getting down to it. Depending on your requirement you may want to do one of these.

Let me take one at a time:

Hiding CRM form field:
Suppose that field's schema name is "new_field".

This is used to hide the textbox
crmForm.all.new_field_d.style.display = "none";

Use this to hide the Label
crmForm.all.new_field_c.style.display = "none";
Note '_d' and '_c', they are important.

Hiding CRM tabs:
The General tab is common to all and you may never want to hide this tab. The custom tab(s), the one right next to the general tab takes the value "tab1Tab", next to this on the right takes the value "tab2Tab" and so on.

Apply this nugget to hide the 1st tab next to the general tab.
crmForm.all.tab1Tab.style.display = "none";

This is about hiding them, just in case you want to seek them.

Note: These are unsupported customizations.

Wednesday, April 30, 2008

Stop the save event

Sometimes you need to stop a form from saving.

Case example - You are validating some data on the form and if the data is incorrect the save event must stop and retun back to the form rather than alerting the fault on the form and continuing saving the form. The OnSave event should return a "false" value to avoid the form from saving. This can be achieved by writting the following nugget.

event.returnValue = false;

To continue saving the CRM form you may want to use this

crmForm.Save();
or
crmForm.SaveAndClose();

Ronald Lemmen has given a few more nuggets. Also going through the SDK would give you an advantage. Plenty of them available.

Happy Scripting..|/

Wednesday, March 5, 2008

Disable a Crm Form field

Many blogs have this content on how to disable a crmForm field. I am not sampling code for how to disable a field but two different views of a disabled field and its code.

Does the below shown crm field appear disabled? Yes. Well, this is how you can achieve it at run time.


Code 1:
crmForm.all.prefix_entityname.Disabled = true;


Does the below shown crm field appear disabled? If your say is no. You are of course right. It does not look disabled, indeed it is.

And this is how you achieve it.

Code 2:
crmForm.all.prefix_entityname.disabled = true;

So, disabling a crmform field can take two different views as shown above.
Carefully admiring the code you will notice the difference.
Code 1: Capital D, Disabled
Code 2: Small Letter d, disabled.

Happy disabling crm form field.