I am trying to set value of the connection field in a dropdown which is mapped with Account Name in the following function in javascipt. But it does not work. Am I doing something wrong?
function setAccountDropDown(accountRecordValue)
{
const dropdown = $('.t-form-connection select.select2-hidden-accessible');
const valueToSet = accountRecordValue;
// Check if the select2 instance exists
if (dropdown.hasClass('select2-hidden-accessible')) {
// Set the value and trigger the change event
dropdown.val(valueToSet).trigger('change');
console.log('Value set successfully:', valueToSet);
} else {
console.error('The dropdown is not initialized as a select2 component.');
}
No, I have not been able to make it work that way.
The workaround I use is a two-step form. I add the connection field that I want to set in the first step of the form and hide that field on the form, then make a rule to set the connection field, and then change the submit rules to go to step two of the form.
Typically, I won’t add any other fields to the first step and just rename the button to something like “Create new Invoice” or “Start Quote” so that it doesn’t seem like a two-step form.
replace the component Id with the id of the component containing the connection field and replace the your_field text with the field id of the connection field.
I tried using the “Set Connection Field Via JS” plugin, but it doesn’t seem to be working. It behaves similarly to @Effy’s method—in a way, it disables the field. Once the script loads, I can’t search for any records in it anymore.
Do you happen to know if there’s a workaround for this, or if I might be missing something in the implementation?
/* START OF PLUGIN CODE FOR Set Connection Field via JS */
TB.render('component_4', function(data) {
const joinField = jQuery('#field_block_field_72 select.select2', data.ele).select2();
const values = `1488`.includes('[') ? JSON.parse(`1488`.replace(/'/g, '"')) : [`1488`];
values.forEach(value => {
jQuery(joinField).trigger({
type: 'select2:select',
params: { data: { id: value } }
});
});
jQuery(joinField).val(values).trigger('change.select2');
jQuery('#field_block_field_72 .select2-container', data.ele).css('width','100%');
if('False' === 'True') jQuery('.af-form-submit', data.ele).click();
});
/* END OF PLUGIN CODE FOR Set Connection Field via JS */