Skip to content

Instantly share code, notes, and snippets.

@anil826
Last active November 15, 2019 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil826/2734d9336257dc7178e7d4361d94c305 to your computer and use it in GitHub Desktop.
Save anil826/2734d9336257dc7178e7d4361d94c305 to your computer and use it in GitHub Desktop.
Sample code for Qty calcuation 2 different table
formyoula.form_fields['50a7-888a-450e'].on("table:results:render:success",function(r){
//Hide the product id coloum
$("#salesforce_table_2f7b-db31-bc5b").DataTable().column(4).visible(false);
$("#salesforce_table_50a7-888a-450e").DataTable().column(7).visible(false)
//First table
var parent_table = $("#salesforce_table_2f7b-db31-bc5b").DataTable();
//second table
var child_table = $("#salesforce_table_50a7-888a-450e").DataTable();
//Get all the table data
var parent_table_data = parent_table.rows().data().toArray()
var child_table_data = child_table.rows().data();
//Keyup event for getting current record and calculation
$('#salesforce_table_50a7-888a-450e').on('keyup', 'input', function(e){
//Get current row
var row = $(this).closest('tr');
//Current Row data
var current_row = child_table.row( row ).data();
//Set the current value.
current_row.Planned_Qty__c = $(this).val();
if ( current_row != undefined ) {
//Get of all Planned Qty
var sum_of_all_planned_Qty = _.where(child_table_data, {Product2Id: current_row.Product2Id}).reduce((s, f) => {
//Return sum of all Planned Qty
return parseInt(s) + parseInt(f.Planned_Qty__c); // return the sum of the accumulator and the current time, as the the new accumulator
}, 0);
console.log(current_row)
//Get the targeted Quanity
var Targeted_quantity__c = _.where(parent_table_data,{Product_Name__c: current_row.Product2Id})[0].Targeted_quantity__c;
Targeted_quantity__c = parseInt(Targeted_quantity__c);
//Check for
if ( sum_of_all_planned_Qty > Targeted_quantity__c ) {
//revert to 0
$(this).val(0);
//Set the current value.
current_row.Planned_Qty__c = $(this).val();
//get sum of all
sum_of_all_planned_Qty = _.where(child_table_data, {Product2Id: current_row.Product2Id}).reduce((s, f) => {
//Return sum of all Planned Qty
return parseInt(s) + parseInt(f.Planned_Qty__c); // return the sum of the Planned_Qty__c
}, 0);
//show alert
alert(current_row.Name + ' - Not a valid Quanity - Remaining Quanity is : '+ (Targeted_quantity__c - sum_of_all_planned_Qty))
}
}
} );
});
var time;
$('#salesforce_table_50a7-888a-450e').on('keydown', 'input', function(e){
console.log('Key Down')
if (time) {
clearTimeout(time);
}
//First table
var parent_table = $("#salesforce_table_2f7b-db31-bc5b").DataTable();
//second table
var child_table = $("#salesforce_table_50a7-888a-450e").DataTable();
//Get all the table data
var parent_table_data = parent_table.rows().data().toArray()
var child_table_data = child_table.rows().data();
//Get current row
var row = $(this).closest('tr');
//Current Row data
var current_row = child_table.row( row ).data();
//Set the current value.
current_row.Planned_Qty__c = $(this).val();
//Get of all Planned Qty
var sum_of_all_planned_Qty = _.where(child_table_data, {Product2Id: current_row.Product2Id}).reduce((s, f) => {
//Return sum of all Planned Qty
return parseInt(s) + parseInt(f.Planned_Qty__c); // return the sum of the accumulator and the current time, as the the new accumulator
}, 0);
//Get the targeted Quanity
var Targeted_quantity__c = _.where(parent_table_data,{Product_Name__c: current_row.Product2Id})[0].Targeted_quantity__c;
Targeted_quantity__c = parseInt(Targeted_quantity__c);
time = setTimeout(function(){
//Check if Qty available
if( sum_of_all_planned_Qty <= Targeted_quantity__c ){
//Set the current value.
//current_row.Planned_Qty__c = $(this).val();
//get sum of all
sum_of_all_planned_Qty = _.where(child_table_data, {Product2Id: current_row.Product2Id}).reduce((s, f) => {
//Return sum of all Planned Qty
return parseInt(s) + parseInt(f.Planned_Qty__c);// return the sum of the Planned_Qty__c
}, 0);
//show alert
alert('Remaining Quanity is : '+ (Targeted_quantity__c - sum_of_all_planned_Qty));
}
clearTimeout(time);
},1000)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment