hkhan
hkhan

Reputation: 863

how to place a button on crm 2011 form.

I want to place a button on CRM 2011 form.

function create_button(fldName, btnLabel, btnWidth, evt) 
{
try{
fldName = "inmate_button_submit";
btnLable="Click Me";
// btnWidth="200px";


var btn = '<button class="ms-crm-Button" onclick="evt()" style="width:50px" id="' +      fldName + '" onmouseover="Mscrm.ButtonUtils.hoverOn(this);"  onmouseout="Mscrm.ButtonUtils.hoverOff(this);" >Click Me</button>';

var ctrl = null;

try {
    ctrl = Xrm.Page.ui.controls.get(fldName).get_chromeElement()[0];
} catch (e) {
    ctrl = Xrm.Page.ui.controls.get(fldName)._control.get_element();
}

// Add the new button
ctrl.innerHTML += btn;

// Hide the textbox
ctrl.children[0].style.display = 'none';
ctrl.children[1].style.display = 'none';

// Hide the label
Xrm.Page.ui.controls.get(fldName).setLabel(btnLable);

// Add Event to the newly created button
 ctrl.children[0].attachEvent('onclick', evt);

}
catch(e){alert(e.description);}
  }
function evt()
{
alert("You have clicked me!!");
}

The above is my following code which places the button on the form. When i click on the button, after showing the alert crm gives me the following error.

ERROR An error has occurred. Try this action again. If the problem continues, check the Microsoft Dynamics CRM Community or you for solutions or contact....

I have no idea why this is happening. Any help guys?

Upvotes: 2

Views: 2711

Answers (5)

Richard Dewhirst
Richard Dewhirst

Reputation: 229

Have you tried the 3rd party applications...

http://crmvisualribbonedit.codeplex.com/

Call the function from a web resource. NOTE: Remember to add $ to reference the web resource location Example $webresource:ButtonScript.js

Upvotes: 0

Adi Katz
Adi Katz

Reputation: 548

2 things might cause this issue:

  1. evt function is not accessible to your button

Resolution: try registering the evt function in global scope i.e.

evt = function() { alert("…"); } 

or

window.evt = function() {} 

2. Your button is using the same field name as the placeholder field. This might cause internal issues.

Resolution: Try giving your button another (bogus) id instead i.e. fieldname + "_button"

Upvotes: 0

Matus Baco
Matus Baco

Reputation: 1

i will recommend add custom webresource ( HTML page which contanis javascript functionality on in onload event ) if you need access to form, use window.parent.document.Xrm.Page ....

Upvotes: 0

Nick
Nick

Reputation: 353

Keep in mind that using getElementById is going to be unsupported and may not work (does not work in Outlook 2007) in some browsers.

I would recommend placing this is in a web resource, or utilizing the ribbon for this functionality. Would either of those meet your requirements?

Upvotes: 3

minohimself
minohimself

Reputation: 526

If you use your js as a webresource then you shouldnt have any problems.

Or you can even trigger the function OnLoad

Please see following :

http://www.mscrmconsultant.com/2012/07/insert-custom-button-on-crm-form-using.html

Upvotes: 1

Related Questions