Answered by:
set default text in fields

Question
-
Hi,
Dos anyone know how to set a field in a form to have a default value pls? I would like to set the Mark Up field to have a default value which can be edited if the user wants. Can this be done? If so how?
Thanks :)
Friday, July 8, 2011 8:34 AM
Answers
-
Hi there,
Just had a look at your screengrabs and think the problem is in the Handler Properties screen - you want to change the Function from 'prefill' to 'prefillField' as this is the name of your function in the Web Resource's Jscript.
Then change the Parameters to just being: 'new_markup',15
After this, touchwood, the logic of the function should fire correctly.
(as a sidenote, is a common piece of CRM 2011 strangeness that if the text of the Parameters box is a little off then the ribbon curiously disappears)
Kind Regards, Paul | http://crmconsultancy.wordpress.comMonday, July 11, 2011 9:28 AM -
Aye thats a good point, your current settings there would prompt the script to run whenever the Mark Up field is changed which may not be what you are aiming for in the default value.
Might be best to change this to Form onLoad, such that the Control drop-down is Form and the Event drop-down is OnLoad.
This should prompt the script to fire when the Form is first loaded - hopefully then you should see the alert box pop-up when the Quote Product screen opens.
Kind Regards, Paul | http://crmconsultancy.wordpress.com- Marked as answer by tcal0005 Tuesday, July 12, 2011 7:27 AM
Monday, July 11, 2011 11:11 AM -
Should not be a problem - in the Form Editor, double-click to bring up the properties of the Mark Up field and ensure that the 'Field is read-only' box is not ticked. This should make the field editable.
The user of the CRM System would then find the Mark Up field defaulted to 15 but could then change this to whatever value they wanted.
With this done, you could repeat the process for the Insurance field.
Kind Regards, Paul | http://crmconsultancy.wordpress.com- Marked as answer by tcal0005 Tuesday, July 12, 2011 7:27 AM
Monday, July 11, 2011 2:29 PM
All replies
-
you can set value in form onload using setValue method
you can refer http://www.magnetism.co.nz/blog/paul/11-06-29/Set_Default_Text_on_Any_Field_in_Dynamics_CRM_2011.aspx
Mahain : My Dynamics CRM Blog- Proposed as answer by HIMBAPModerator Friday, July 8, 2011 8:48 AM
Friday, July 8, 2011 8:48 AMModerator -
I have tried this (the steps mentioned in the link) but I think I'm not doing it right. I have created the web resource with the JScript but then when I go to the Quote Product Form ( as that is where I want my field), I'm not sure what I should do in the Form Properties and how I should set up my field to hold the default value :/Friday, July 8, 2011 9:01 AM
-
Hi there,
You need to attach your new Web Resource to the Quote Product Form in CRM in the Form Properties area, and then invoke one of the functions in your Web Resource to run when the onLoad event of the Quote Product Form is triggered. Is quite difficult to explain without screen shots, but did write a blog entry on how to do this back when first looking at CRM 2011 which might help you out:
http://crmconsultancy.wordpress.com/2010/10/19/form-scripting-in-crm-2011/
Kind Regards, Paul | http://crmconsultancy.wordpress.comFriday, July 8, 2011 10:17 AM -
In that post he has mentioned all the step with screenshot you just need to replicate that with quote entity.
Mahain : My Dynamics CRM Blog- Proposed as answer by Jan AS Friday, July 8, 2011 11:44 AM
Friday, July 8, 2011 10:35 AMModerator -
What should I write as a list of parameters when it says: "Comma separated list of parameters that will be passed to the function"
Thanks :)
Friday, July 8, 2011 10:50 AM -
If your function in the Web Resource takes in a parameter, say in a format like the following:
function myFunction(someVariable)
{
alert(someVariable);
}
Then you would write this parameter into the comma separate list as:
'function input'
For multiple parameters:
function myFunction(variable1, variable2)
{
alert(variable1 + ' and ' + variable2);
}
then:
'function input for variable1', 'function input for variable2'
Hope that helps
Kind Regards, Paul | http://crmconsultancy.wordpress.comFriday, July 8, 2011 11:12 AM -
I have done what you have written but so far nothing has changed :/Friday, July 8, 2011 12:27 PM
-
Best bet would be to check the following:
- Your Web Resource is added to the list of Form Libraries in the Form Properties
- That your function is added to the list of Event Handlers and that the function name matches that contained within the script of the Web Resource,
- That the function is enabled in the Handler Properies
- Whether the function is running at all when the Form is loaded by placing a quick Alert prompt
- Wrap a try { } catch(ex) { alert(ex.description) } block to see if the function is reporting any kind of scripting error when run
If all of that looks okay and still nothing is happening when the Form loads - might be worth sending over a few screen shots to see if we can diagnose the problem.
Kind Regards, Paul | http://crmconsultancy.wordpress.comFriday, July 8, 2011 12:54 PM -
I'm still having trouble with it :/ How can I send the screen shots since it seems I cannot paste them here. Is there a way to attach a file?Monday, July 11, 2011 7:01 AM
-
try to upload your screenshots somewhere on net and just share url here so that we can check.
Mahain : My Dynamics CRM BlogMonday, July 11, 2011 7:17 AMModerator -
I uploaded the document with the screen shots here: Hope it is visible
http://www.speedyshare.com/files/29375986/prefill_problem.docx
Monday, July 11, 2011 7:23 AM -
Hi there,
Just had a look at your screengrabs and think the problem is in the Handler Properties screen - you want to change the Function from 'prefill' to 'prefillField' as this is the name of your function in the Web Resource's Jscript.
Then change the Parameters to just being: 'new_markup',15
After this, touchwood, the logic of the function should fire correctly.
(as a sidenote, is a common piece of CRM 2011 strangeness that if the text of the Parameters box is a little off then the ribbon curiously disappears)
Kind Regards, Paul | http://crmconsultancy.wordpress.comMonday, July 11, 2011 9:28 AM -
I have done the changes you said and the ribbon appears normally now but the fields are not prefilled :/ Do I have to wait a while for the changes to be implemented or should it work immediately?Monday, July 11, 2011 9:35 AM
-
The changes will take affect as soon as click the Publish button on the Form Edit window, so if the ribbon has started appearing normally this should indicate that the changes have taken affect.
So probably close to getting this working, best bet for a next step would to place a simple alert("prefillField") at the top of your Web Resource's Jscript to indicate that the function is running.
Also if you put a Try/Catch in the prefillField function - this will help see if any errors are occuring in the Jscript itself. So the script will look like the following:
// JavaScript Document
function prefillField(fieldName, value) {
try {
alert("prefillField");
var isCreateForm = Xrm.Page.ui.getFormType() == 1;
var field = Xrm.Page.getAttribute(fieldName);
if (isCreateForm) {
field.setValue(value);
field.setSubmitMode("always");
}
}
catch(ex)
{
alert("prefillField reported an error '" + ex.description + "'");
}
}
Publish and then see what happens when the form loads.
Kind Regards, Paul | http://crmconsultancy.wordpress.comMonday, July 11, 2011 9:54 AM -
I have changed the JScript code to the one you posted above. No errors are showing however the fields are not being prefilled :/
Am I selecting the correct options in the Event Handler? In the Form Properties, Event Handler, I am selecting Control to be Mark Up and the Event is OnChange and the event handler is added to this. Is this correct or should I leave the Control to be Form?
- Proposed as answer by P McQ Monday, July 11, 2011 2:30 PM
Monday, July 11, 2011 10:42 AM -
The best option to put a simple alert like below to check if you are setting it for correct field or not, trying to write something on that field and tab out you should get this alert.
function prefillField(fieldName, value) {
alert('I am in Test Mode');
var field = Xrm.Page.getAttribute(fieldName);
if (isCreateForm) {
field.setValue(value);
field.setSubmitMode("always");
}
}
Mahain : My Dynamics CRM BlogMonday, July 11, 2011 10:56 AMModerator -
It worked now however I want it to be editable ie: that the field has a DEFAULT value of 15 when the form loads which can be edited if necessary. Is this possible?Monday, July 11, 2011 11:10 AM
-
Aye thats a good point, your current settings there would prompt the script to run whenever the Mark Up field is changed which may not be what you are aiming for in the default value.
Might be best to change this to Form onLoad, such that the Control drop-down is Form and the Event drop-down is OnLoad.
This should prompt the script to fire when the Form is first loaded - hopefully then you should see the alert box pop-up when the Quote Product screen opens.
Kind Regards, Paul | http://crmconsultancy.wordpress.com- Marked as answer by tcal0005 Tuesday, July 12, 2011 7:27 AM
Monday, July 11, 2011 11:11 AM -
Should not be a problem - in the Form Editor, double-click to bring up the properties of the Mark Up field and ensure that the 'Field is read-only' box is not ticked. This should make the field editable.
The user of the CRM System would then find the Mark Up field defaulted to 15 but could then change this to whatever value they wanted.
With this done, you could repeat the process for the Insurance field.
Kind Regards, Paul | http://crmconsultancy.wordpress.com- Marked as answer by tcal0005 Tuesday, July 12, 2011 7:27 AM
Monday, July 11, 2011 2:29 PM -
Ok I will try it and see what happens. Thanks :)Monday, July 11, 2011 2:51 PM
-
It finally worked!! :D Thanks guys :)
By any chance you wouldn't know how to edit a field once created? Example I created a field which is a Whole Number and I would like to change it to Decimal Number. When I select Edit, this option is greyed out and I cannot edit it :/ Is there a way to edit this?
Tuesday, July 12, 2011 7:27 AM