Summary
Example to create a new repeat entry when a user updates the Quantity of an existing OLI. Every time quantity is decreased custom script will create a new entry based on the quantity difference.
Proposed Solution
Create a form with an opportunity lookup and repeat group to pre-fill existing OpportunityProdcts along with Product details and quantities for respective OLIs. A custom Javascript to listen to repeat entry quantity field for change in value, if a user has changed quantity then the difference is calculated, and use the difference to create a new repeat entry.
Please create a form as shown below.
Please add a javascript element from the Media tool tab to form below repeat group as shown below.
Click on the pencil icon of the JS field and copy the code below onto the edit section of the JS field:
// Target fields to listen to
var target_field_ids = [ '28ae-4276-7bb9', '1d2d-678d-dd51', 'e4e6-33da-87b3', '551a-9a77-1cef' ];
// Repeat Group Id
var repeat_group_id = '0861-40f3-70e9';
//Editable quantity field Id
var quantity_field_id = 'e4e6-33da-87b3'
//Quantity field value which stores original value
var original_quantity_field_id = '551a-9a77-1cef';
// Quantity field
var quantity_field_id = '';
// Listening to all field changes in an existing repeat group item
formyoula.form_fields[ repeat_group_ids ].on( 'all', function( event, data ) {
// Check if field updates are available
if ( !data || _.isEmpty( data.updated_sub_component ) ) return;
// Get actual field that was changed
var component = data.updated_sub_component;
// Check if repeat field component changes should be tracked
if ( [ quantity_field_id ].includes( component.get( 'component_id' ) ) ) {
var target_field = data.updated_repeat_item.findWhere( {
component_id: original_quantity_field_id
} );
// Check if difference is present
if ( target_field.get( 'value' ) > component.get( 'value' ) ) {
// Call new entry creation function.
add_new_repeat_entry( {
difference_amount: target_field.get( 'value' ) - component.get( 'value' ),
repeat_fields: data.updated_repeat_item
} );
}
}
} );
// Function to create repeat entry data and click new entry action button via JS
function add_new_repeat_entry( options ) {
// Loop through all repeat fields which are necessary to create a new OLI
for ( target_field_id of target_field_ids ) {
// Get the existing repeat entry field data which needs to be copied to new entry exceppt for quantity field
var target_field = options.repeat_fields.findWhere( {
component_id: target_field_id == original_quantity_field_id ? quantity_field_id : target_field_id
} );
// Check if current looped field is quantity and initial quantity field from diffference value
if ( target_field_id == quantity_field_id || target_field_id == original_quantity_field_id ) {
window.formyoula.form_fields[ target_field_id ].set( 'value', options.difference_amount ).trigger( 'change' );
} else {
window.formyoula.form_fields[ target_field_id ].set( 'value', target_field.get( 'value' ) ).trigger( 'change' );
}
}
$( '#component-' + repeat_group_ids + ' .add' ).click();
}
Example form is attached:
For any questions, please contact us - [email protected] or the Formyoula in-app chat 🙂