1
votes

Basically, what I want is that the user should be able to select the dropdown and not be able to change it after making and saving the selection. So it will be a one-time entry field.

Below is a screenshot of the field I want to apply this property.

photo

So this field has a Yes or No selection. And to make the business logic from failing I have to make it a one-time entry field only.

I looked up the form editor for possible things but couldn't find anything that would let me achieve this.

UPDATE #1

Below is my onload function:

function Form_onload() {
    var formType = Xrm.Page.ui.getFormType();
    var p = Xrm.Page.getAttribute("opportunityid");
--------------NEW CODE--------------------------------

    if(formType ==2){ //form type 2 means the form is a saved form. form type 1 is new form.

    alert(formType);
    var myattribute = Xrm.Page.getAttribute("var_internal");
    var myname = myattribute.getName();
    if (Xrm.Page.getControl(myname) != null) {
        //alert(myname);
        Xrm.Page.getControl(myname).setDisabled(true);
    }
    }
--------------NEW CODE---------------------------
    if (formType == 1 && p != null && p.getValue() != null) {
        alert('Child Opportunities can only be created by clicking the Create Child Opportunity button in the Opportunity ribbon.');
        window.top.close();
    }


}
4

4 Answers

1
votes

No code solution: I think you could use an Option set with a Yes/No option and a default of Unassigned Value. Then add that field to Field Level Security with "Allow Update" set to No.

Field Level Security with no update

When updating the FLS field permissions, be sure that the profile is associated with the organization "team" so that all users can see the field:

enter image description here

1
votes

Arun already gave you an hint how to proceed, I just tried this req on one of my instance.

Create one extra field (dummy field) in my case I call it new_hasfieldbeenchanged1 This field will hold data when field is changed. Lock this field (always) and keep this field on form (but visibile =false)

Now you need 2 trigger Onload and OnSave. Below function will do your work. Let me know if this helps.

function onLoad(executionContext) {

    debugger;
    var formContext;
    if (executionContext && executionContext.getFormContext()) {
        formContext = executionContext.getFormContext();
        //executionContext.getEventSource()
        if (formContext.getAttribute("new_hasfieldbeenchanged1") && formContext.getAttribute("new_hasfieldbeenchanged1").getValue()!=null) {
            if (formContext.getControl("new_twooptionfield")) {
                formContext.getControl("new_twooptionfield").setDisabled(true);
            }
        }
    }
}
function onSave(executionContext) {
    debugger;
    var formContext;
    if (executionContext && executionContext.getFormContext()) {
        formContext = executionContext.getFormContext();
        //executionContext.getEventSource()
        if(formContext.getAttribute("new_hasfieldbeenchanged1") && formContext.getAttribute("new_twooptionfield") && formContext.getAttribute("new_twooptionfield").getIsDirty()){
            formContext.getAttribute("new_hasfieldbeenchanged1").setValue((new Date()).toString());
            if (formContext.getControl("new_twooptionfield")) {
                formContext.getControl("new_twooptionfield").setDisabled(true);
            }
        }

    }
}
1
votes

Due to environment-specific settings in my DEV, I was not able to reproduce what was suggested by @Eccountable. Although, his solution worked in other environments.

@AnkUser has a good answer as well but I was looking to shorten the code and make things as simple as possible.

My solution

I was able to handle this using Javascript on client-side. using the XRM toolbox.

In the XRM toolbox, I located the javascript for the opportunity and observed field changes in formType when the Opportunity was New and when the Opportunity was Existing. This variable (formType) was =1 when the Opportunity was New and =2 when it was Existing/Saved.

Using this piece of information I was able to leverage my Javascript as follows in Form_onload()

function Form_onload() {
    if (formType == 2) {
        var myattribute = Xrm.Page.getAttribute("internal");
        var myname = myattribute.getName();
        if (Xrm.Page.getControl(myname) != null) {
            Xrm.Page.getControl(myname).setDisabled(true);
        }
    }
}
0
votes

There’s no OOB configuration for such custom requirements, but we can apply some script logic.

Assuming this is a picklist with default null and not a two-option bool field, we can use onLoad form script to check if it has value & lock it. No need to have onChange function.

If it’s a bool field, then it’s hard to achieve. You have to track the initial value & changes made to implement the logic you want. Or through some unsupported code.