Make field ReadOnly when it contains a value

I have a requirement where once the field has been populated, it should be read only. I have placed the following script in the Create Script area for the field on the screen. However, it is not working. The field is coming up as read only. However, it is read only whether there is a value in the field or not. I don't know if it makes a difference but the field is a date only field. Is there an error in what I have done below? Any assistance would be greatly appreciated! Thanks!

if (this.value != null || this.value != "" || this.value != 'undefined');
{
ReadOnly = true;
}

  • 0

    Hi Michele,

    If you're trying to use a Create Script, try the below.

    var myFieldValue = CRM.GetContextInfo("table","field");
    if(!myFieldValue){
    ReadOnly = false;
    }
    else(myFieldValue){
    ReadOnly = true;
    }

    That way may be a bit more reliable. And I want to say "this.value" works only in the OnChange script as it's handled on the client side whereas the Create Script and Validation Scripts are both server side. Give it a shot and see if it works.

  • 0

    Thanks Basil! I forgot to mention that this is a field on a custom entity. If I remember correctly, GetContextInfo does not work for a custom entity. I will give it a try and let you know.

    Thanks again!

  • 0

    Basil. I double checked on GetContextInfo for a custom entity and it is not available for a custom entity. However, using the same thought process, I have used Find Record which I think you can use on a custom entity. Therefore, I now have the following code in the Change Script area of my field. However, it does not work correctly. The field comes up read only whether or not there is a value in the field. Can you see what I might be done wrong with this? Any assistance would be greatly appreciated.

    var DDID = Values("due_diligenceid");

    var DDRecord = CRM.FindRecord("DueDiligence", "due_diligenceid="+DDID);

    var DDReceived = DDRecord.due_datereceived;

    if (due_datereceived != null || due_datereceived != "" || due_datereceived != 'undefined' )

    {

    ReadOnly = true;

    }

    else

    {

    ReadOnly = false;

    }

    Thanks!

  • 0

    Michele

    The general way this test works is like this:

    This is code entered in the Create Script.

    Valid = false;

    if (!Values("proj_name"))

    {

    ErrorStr = "Field is null"

    }

    else

    {

    ErrorStr = Values("proj_name");

    ReadOnly = true;

    }