Important Update: Community URLs redirect issues are partially resolved. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Populate a Read Only field with JavaScript?

JayFogelson2
Contributor III

I'm wondering if there is a way/different syntax to use to populate a text field, that is being made read only with a DDE?

The below works, when the field is not being made read only. 

 

The odd thing is the when I check the value with an alert, such as:

alert($('input[id*="f' + textUserField + 'c"]').val());

It works when it's both editable and read only.

<script type="text/javascript">
var userPermField = 20936;
var textUserField= 20937;


Sys.Application.add_load(function() {
var newUserName = $('div[id*="f' + userPermField + 'c"]').text().trim().replace('Pick','');
$('input[id*="f' + textUserField + 'c"]').val(newUserName);
});
</script>
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
20 REPLIES 20

This is in 6.4 SP1.

Thanks.  See how this updated code works.

<script type="text/javascript">
     var textFieldId = 20719;

     Sys.Application.add_load(function() {
          setTextNumericField(textFieldId,'This is a test')
     });

     function setTextNumericField(f,v) {
          var textFieldAttributes = new Array();

          textFieldAttributes.push({
               enabled: true,
               emptyMessage: '',
               validationText: v,
               valueAsString: v,
               minValue: '-9999999999999',
               maxValue:'9999999999999',
               lastSetTextBoxValue: v});

          var textFieldAttributesSerialised = Sys.Serialization.JavaScriptSerializer.serialize(textFieldAttributes[0]);

          $('input[id*="'+ f +'c"]').val(v);
          $('input[id*="'+ f +'c_ClientState"]').val(textFieldAttributesSerialised);

          //Check to see if the field is read-only
          if($('input[id*="'+ f +'c"]').parent().parent().find('.readOnly')) $('input[id*="'+ f +'c"]').parent().parent().find('.readOnly').text(v);
          csp(event)
     }
</script>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

There was an extra span that contained the text value that wasn't getting populated.

 Advisory Consultant

JayFogelson2
Contributor III

That did the trick - thanks David!

Hi @DavidPetty ,
I tried using this code for 6.15 and it seems it does not work.

Similarly, we have a requirement where we create a child record from a parent record, copying different fields values over, and making these fields read only through ACL.

Fields like Text (single line/multi), Date, Cross Reference or Radio Button Values List don't get populated, while Drop-Down Values List fields are successfully populated.
All the above fields are also set to Read Only through ACL. 
Is there another way to set these fields to Read-Only through Custom Object, or maybe an updated version of the script?

Thank you

See if this updated function works for clearing a text field.

     function setTextNumericField(fld,fldValue) {
          if($CM._fields[fld].type == 1 && $CM._fields[fld].displayControl == 1) {
               setTimeout( function(){
                    $('input[id$="'+ fld +'c"]').focus({preventScroll: true}).val(fldValue).blur();
               }, 200);
          } else {
               $('input[id$="'+ fld +'c"]').val(fldValue);
               $('div[id$="'+ fld +'c_text"]').html(fldValue);
          }

          //Check to see if the field is read-only
          if($('input[id$="'+ fld +'c"]').parent().parent().find('.readOnly')) $('input[id$="'+ fld +'c"]').parent().parent().find('.readOnly').html(fldValue);

     }

 Advisory Consultant

Thank you @DavidPetty 

It worked like a charm. Will it work with Cross Reference fields as well? 

Thanks again.

Glad it's working : D

No as the function name clearly states text and numeric fields.  You'd have to use the following function and pass a null array [] for the values.

function setXrefFieldValues(fieldID,values){
     var xrefFldId = fieldID;

     if(xrefFldId != 0){
          field = $CM._fields[xrefFldId].clientId;
          ArcherTech.UI.ReferenceField.CurrentLookupField = $find(field);
          var currentField=ArcherTech.UI.ReferenceField.CurrentLookupField;

          if(currentField) {
               currentField.setLookedUpRecords(values);
               if (currentField.get_isInDdeCondition() && Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
                    var endRequestHandler = function () {
                         ArcherTech.UI.ReferenceField.UpdateCurrentField(values);
                         Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(values);
                    };
                    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
               } else {
                    ArcherTech.UI.ReferenceField.UpdateCurrentField(values);
               }
          }
     }
}

 Advisory Consultant

Thank you @DavidPetty a ton.

As I mentioned in my original question, I will need to  populate a Date field and a Dropdown values List.

I was able to populate the Radio Buttons Values list successfully.

Do you happen to have the code for remaining field types in one post for future reference, that will be super helpful if possible.

Thank you again for your continued help and support. 

Thank you again David.

I managed to get these values populated for read-only fields, but I encountered a weird behavior for some fields like the Date and Dropdown values list, where values assigned only show up on the layout after clicking on Save button, and looks empty/blank before that.

Any thoughts or suggestions?

Thank you

Can you post the code you're using to set the data and values list?

 Advisory Consultant