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>
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
12 REPLIES 12

DavidPetty
Archer Employee
Archer Employee

Jay you can populate a field that's set a read-only via a apply conditional layout.

 

It just it takes a little more to populate a users/groups record permission field:

 

<a href="#" onclick="setRecordPermission()">Set User</a>
<script type="text/javascript">
     var setUser = {
          fldId: '16196',
          usersName: 'Doe, John',
          usersId: '1194',
          itemtype: 'user'};  // Use 'group' if you're adding a group

     function setRecordPermission() {
          var RPFieldRootId = ArcherTech.UI.ClientContentManager.GetInstance().getFieldById(setUser.fldId), UsrArray = [];

          if(setUser.usersId != '') {
               UsrArray.push({
                    name: setUser.usersName,
                    value: setUser.usersId + ':' + (setUser.itemType == 'group' ? 3 : 1)
               });
          }
          var serialized = Sys.Serialization.JavaScriptSerializer.serialize(UsrArray);

          $('div[id*="'+ RPFieldRootId +'_"] div:first-child').text(setUser.usersName);
          $('input[id*="'+ RPFieldRootId +'_"]').val(serialized);
          if(setUser.itemType == 'user'){
               $('#SelectedUsers'+setUser.fldId).val(setUser.usersId);
          }else if(setUser.itemType == 'group') {
               $('#SelectedGroups'+setUser.fldId).val(setUser.usersId);
          }
     }
</script>

 Advisory Consultant

Thanks David!  I'm actually trying to populate a text field, which is being made read only with a DDE.  I've tried the below, and different variations of it, but nothing seems to work.

 

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

Try

<script type="text/javascript">
     var textFieldId = 18572;
    
     Sys.Application.add_load(function() {
          setTextField(textFieldId,'')
     });

     function setTextField(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);
          csp(event)
     }
</script>

 Advisory Consultant

This is working well David - Thanks!  The one lingering issue is that it's requiring me to save the record twice, before it displays anything in that field.  I've tried moving the code around on the layout, but no luck.  Any way to get this to fire right upon page load, or at least after 1 save?

Anytime Jay

 

Can you post your custom code?

 Advisory Consultant

if the user permission field changes, the record has to be saved twice before the text field is updated.  even when I just hard coded in the value in the custom object, the record has to be saved twice before pulling in the new value.

 

 

<script type="text/javascript">
          var assignedUser = 20936;
     var assignedUserText = 20937;
    
     Sys.Application.add_load(function() {
     var userTextString = $('div[id*="f' + assignedUser + 'c"]').text().trim().replace('Pick','');
     userTextString = userTextString.split(',')[1].trim() + ' ' + userTextString.split(',')[0].trim();


     setTextField(assignedUserText,userTextString)
     });

     function setTextField(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);
          csp(event)
     }
</script>

Odd, it shouldn't need to be saved at all for it to fire and update the field.

 

Not sure if this is the issue or not, but you define userTextString and set a value (line 6) and the next line you're setting a new value (line 7) evaluating the old value.

 Advisory Consultant

Lines 6 and 7 are just removing the "Pick" text from the user field, and then switching to display as first and last name with no comma.

 

Even when I just try your syntax, as hard code in a value, it requires the record to be saved once, then it displays, IF the field is Read only with a DDE.  In the example below, "This is where the persons name goes." does not render until the record is saved.  If the field is editable, it works right away upon page load.

<script type="text/javascript">
     var textFieldId = 20937;
    
     Sys.Application.add_load(function() {
          setTextField(textFieldId,'This is where the persons name goes.')
     });

     function setTextField(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);
          csp(event)
     }
</script>

Let me take a look on what's going on 

 

Jay which version of Archer are you on?

 Advisory Consultant