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

cancel
Showing results for 
Search instead for 
Did you mean: 

Help with CO to clear a text field

ScottNess1
Contributor III

I'm trying to build out a custom object in 6.9 P2 that will clear the value in a text field based on the value selected from a values list field.

I've read through article https://community.rsa.com/t5/archer-custom-objects/clear-out-text-field-based-on-value-list-options/td-p/453479 a number of times, but seem to be stuck.

The error in console is 'SCRIPT5009: 'fld' is undefined  It seems to be related to this line of code - var vlClientId = $CM.getFieldById(fld.vlField).clientId;

 

The full code is - 

<script type="text/javascript">
const vlField = 120601; // Values List Field Classification
const textFields = 123788; // Text field Exam Name
const clearOnValues = ['138716:0']; // Value from Classification = Simple
function setTextField(fld,val)
{
var textFieldAttributes = new Array();
textFieldAttributes.push({
enabled: true,
emptyMessage: '',
validationText: val,
valueAsString: val,
lastSetTextBoxValue: val});
console.log("Values list Field ID: "+ vlField);
console.log("Test Field ID: "+ textFields);

var textFieldAttributesSerialised = Sys.Serialization.JavaScriptSerializer.serialize(textFieldAttributes[0]);
$('input[id$="'+ fld +'c"]').val(val);
$('input[id$="'+ fld +'c_ClientState"]').val(textFieldAttributesSerialised);
console.log("in set text function");
console.log("fld = ", fld);
console.log("val = ", val);
}
Sys.Application.add_load(function()
{
var vlClientId = $CM.getFieldById(fld.vlField).clientId;
$('div[id^="' + vlClientId + '"]').change(function()
{
var selectedValue = $CM.getFieldValue(fld.vlField);
if (selectedValue !== null && fld.clearOnValues.indexOf(selectedValue[0]) !== -1)
console.log("Clearing Text Field");
{setTextField(textFld, '');
}
});
});
</script>

The values list field is dropdown.

Appreciate any suggestions/guidance on what I may be missing.

TIA

 

2 ACCEPTED SOLUTIONS

Accepted Solutions

I have an updated function that handles read-only fields, Re: Javascript&colon; Setting value of field in read - RSA Link - 431426

Note that the function name is different.

 Advisory Consultant

View solution in original post

The chage event will alway fire when the field actual changes.

Try,

 if (selectedValue !== null && clearOnValues[0][.indexOf(selectedValue[0]) !== 0)

 Advisory Consultant

View solution in original post

9 REPLIES 9

DavidPetty
Archer Employee
Archer Employee

Scott,

On, var selectedValue = $CM.getFieldValue(fld.vlField);.  There is no variable defined for fld and the line should read, var selectedValue = $CM.getFieldValue(vlField);

No sure why this line is wrapped in {}{setTextField(textFld, '');} also there is no variable defined for textFld.  The line should read, setTextField(textFields , '');

 Advisory Consultant

David,

Thank you, it cleared the error.  Just needed some clear eyes to review.

The console is logging the expected values and I am hitting the 'Clearing text field' message when the values list field is changed.  However, the text field is not clearing. 

If this matters, the text field is set to read only with a DDE.

 

Update code:

<script type="text/javascript">
const vlField = 120601; // Values List Field Classification
const textFields = 123788; // Text field Exam Name
const clearOnValues = ['138716:0']; // Value from Classification = Simple
function setTextField(fld,val)
{
var textFieldAttributes = new Array();
textFieldAttributes.push({
enabled: true,
emptyMessage: '',
validationText: val,
valueAsString: val,
lastSetTextBoxValue: val});
console.log("Values list Field ID: "+ vlField);
console.log("Text Field ID: "+ textFields);
console.log("VL value: "+ clearOnValues);

var textFieldAttributesSerialised = Sys.Serialization.JavaScriptSerializer.serialize(textFieldAttributes[0]);
$('input[id$="'+ fld +'c"]').val(val);
$('input[id$="'+ fld +'c_ClientState"]').val(textFieldAttributesSerialised);
console.log("in set text function");
console.log("fld = ", fld);
console.log("val = ", val);
}
Sys.Application.add_load(function()
{
var vlClientId = $CM.getFieldById(vlField).clientId;
$('div[id^="' + vlClientId + '"]').change(function()
{
var selectedValue = $CM.getFieldValue(vlField);
if (selectedValue !== null && clearOnValues.indexOf(selectedValue[0]) !== -1)
console.log("Clearing Text Field");
{setTextField(textFields, '');
}
});
});
</script>

Anytime 

I see you still have {} wrapped around {setTextField(textFields, '');}.  Remove the {} from that line and see if that fixes the issue.

Having the field as read-only via an action is fine.  The field is still accessible by javascript.

 Advisory Consultant

Sorry, missed removing those braces.  That is complete, but still not clearing the field when I change the value in the values list field.

If I change the value to something other than 'Simple' - which is ID value 138176, the console log shows the values. Then back to 'Simple' the console shows 'Clearing Text Field', but the field is still populated.

Dave,

I had not checked this before.  The value did update after clicking on Save.  Guess I was expecting a more instant change.

I have an updated function that handles read-only fields, Re: Javascript&colon; Setting value of field in read - RSA Link - 431426

Note that the function name is different.

 Advisory Consultant

Thanks, that update did the trick

@DavidPetty 

I have a slightly different look now on this request.  To have the text field blanked when the value selected from the values list field DOES NOT match a specific value.  

I thought this might something like - if (selectedValue !== null && selectedValue !== clearOnValues)

But the change function is firing for any value selected, including the value in the const.

I also tried - if (selectedValue !== null && clearOnValues.indexOf(selectedValue[0]) !== 0)

Noticing that the value for clearOnValues.indexOf(selectedValue[0]) will change between 0 and -1 based on the value selected for the values list field.  If the value matches the constant, then 0 is returned, otherwise it is -1.

The chage event will alway fire when the field actual changes.

Try,

 if (selectedValue !== null && clearOnValues[0][.indexOf(selectedValue[0]) !== 0)

 Advisory Consultant