Help with label and javascript

Thanks it worked, I hadn’t noticed this small difference!

Hello everyone, sorry again for the inconvenience but as already said several times I am new to suitecrm and I am learning it a little at a time. Now I have a value that comes out of a specific operation and I would like there to be a control. Now let me explain, for example, the result of the operation is “A = 38”, I would like the user to be able to modify it, but he must be forced to choose “37 or 38” as a value and cannot enter other values. Now I am attaching the code of the operation to which I refer. I saw that in JQuery there is InputFilter I tried to implement it but it doesn’t work so I’m sure I’m doing something wrong. I ask you for help so that I can finally go on as I have been working on it for 3 days and I cannot find a solution.
Thanks in advance!

THIS IS THE CODE OF OPERATION:

$(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){

  console.log("my custom js has been loaded");

  $('#ore_c').val(Math.round($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val())); 

  console.log("custom js finished. result ore_c: " + $('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()) ;

});

Hi,
I created a new fiddle for you. Basically, it is storing the lower and upper limit for the field as html attributes and adds a new listener that compares the current value with those new attributes.
The user can now alter the suggested value, but the browser is enforcing a value in between min/max:

https://jsfiddle.net/8gc9omx5/3/

1 Like

Thank you very much, with your help it worked and I also understood where I was wrong, you made me understand very well. Thanks so much!

Good morning yesterday while I was testing my code before continuing with my module construction I started to do some tests to see if everything worked correctly. At some point I notice that I have a little problem between the editview and the detailview. Now let me explain better when I insert the values ​​in the editview everything works correctly when I save and access the detailview these values ​​change, now I am attaching the screenshots so that you too can see.

EDITVIEW:

As you can see in the editview everything works fine. Now pay attention to the value underlined in red and see what happens after I save.

DETAILVIEW:

Look, in the field “N ° pieces” which would be the one underlined in red a point “.” Is added, I know which suite it uses to mark the figures but this causes a problem for me. Now look at the values ​​highlighted in green. Now I show you what happens when I go back to the edit view.

RETURN TO THE EDITVIEW:

As you can see the previously selected values ​​in green are now selected in red and as you can see the related fields are now empty. I don’t understand why this happens I think it is due to the point that it is automatically inserted by suite.
I don’t think this is due as I have set those fields as non-editable. Can anyone kindly help me?

EDIT: Is it possible that the disappearance of the values ​​from the fields is due to the fact that they are not editable?

EDIT TWO: I removed the custom code from the editview code that made the values ​​read only and now they don’t disappear but the problem is that those values ​​must not be editable. How can I solve?
Now another problem remains, the point “.” separating the digits.

Hi,
first of all, I would try to figure out if your javascript is causing this issue, so disable it first and see if those values are shown again.
If that is the case, insert debug logs to the js and watch the dev console closely while testing. I could imagine that on page-load something doesn’t work due to the decimal point (you should be very careful here - users can change these settings individually, your script needs to reflect that too).

What is the current js your injecting btw?

This is my Javascript code:

//CALCOLO ORE LAVORATIVE

$(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){

  console.log("my custom js has been loaded");

  $('#ore_c').val(Math.round($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val())); 

  console.log("custom js finished. result ore_c: " + $('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()) ;

});

//CONTROLLO INSERIMENTO ORE

$( "#ore_c" ).change(function() {

  //Ottenere i valori massimi e minimi correnti dal campo

  var max = parseInt($(this).attr('max'));

  var min = parseInt($(this).attr('min'));

  

  if ($(this).val() > max)

  {

      $(this).val(max);

      alert("Hai selezionato un valore superiore al max corrente : " + max);

  }

  else if ($(this).val() < min)

  {

      $(this).val(min);

       alert("Hai selezionato un valore inferiore al min corrente : " + min);

  }       

}); 

$(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){

//Calc. valore corrente e inserirmento nel campo

var currValue = $('#n_pezzi_c').val() / $('#tempo_ciclo_c').val();

$('#ore_c').val(Math.round(currValue));

//debug

var min = Math.floor(currValue);

var max = Math.ceil(currValue);

console.log("curr: " + currValue + " min: " + min + " max: " + max);

//Settagio nuovi attributi min/max 

$("#ore_c").attr({

"max" : max,       

"min" : min      

});

});

//CONVERSIONE IN ORE

function convertNumToTime(number) {

  // Controlla il segno del numero specificato

  var sign = (number >= 0) ? 1 : -1;

  // Imposta il valore positivo del numero di segno negativo

  number = number * sign;

  // Separa l'int dalla parte decimale

  var hour = Math.floor(number);

  var decpart = number - hour;

  var min = 1 / 60;

 // Arrotonda al minuto più vicino

  decpart = min * Math.round(decpart / min);

  var minute = Math.floor(decpart * 60) + '';

  // Aggiungi imbottitura se necessario

  if (minute.length < 2) {

  minute = '0' + minute; 

  }

  // Aggiungi Accedi al risultato finale

  sign = sign == 1 ? '' : '-';

  // Concatena ore e minuti

  time = sign + hour + ',' + minute;

  return time;

}

$(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){

  console.log("my custom js has been loaded");

  $('#ore_totali_effettive_c').val(convertNumToTime(parseFloat($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()).toFixed(2)));

  console.log("custom js finished. result ore_totali_effettive_c: " + $('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()) ;

});

//CALCOLO TURNI/GIORNI/OPERAI

//TURNI

$(document).on('change','#durata_ore_turno_c, #team_c, #ore_c',function(){

  console.log("JS started with turn calcolation");

  $('#turni_c').val(Math.round($('#ore_c').val() / ($('#durata_ore_turno_c').val() * $('#team_c').val())));

  console.log("custom JS finished. result turni_c: " + $('#ore_c').val() / $('#durata_ore_turno').val() * $('#team_c').val());

});

//GIORNI

$(document).on('change', '#ore_c, #durata_ore_turno_c, #team_c, #n_turni_giorno_c',function(){

  console.log("JS started with day calculation");

  $('#giorni_c').val(Math.round($('#ore_c').val() / ($('#durata_ore_turno_c').val() * $('#team_c').val() * $('#n_turni_giorno_c').val())));

  console.log("custom JS finished. result giorni_c: " + $('#ore_c').val() / $('#durata_ore_turno_c').val() * $('#team_c').val() * $('#n_turni_giorno_c').val());

});

//OPERAI

$(document).on('change', '#ore_c, #durata_ore_turno_c',function(){

  console.log("JS started with operai calc");

  $('#operai_c').val(Math.round($('#ore_c').val() / $('#durata_ore_turno_c').val()));

  console.log("custom JS finished. result operai_c: " + $('#ore_c').val() / $('#durata_ore_turno_c').val());

});

/* ARROTONDAMENTO A 3 CIFRE DECIMALI WITH toFixed()

$(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){

  console.log("my custom js has been loaded");

  $('#ore_totali_effettive_c').val(parseFloat($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()).toFixed(2));

  console.log("custom js finished. result ore_totali_effettive_c: " + $('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()) ;

});*/

The problem I think is the point that adds suite to separate the digits, because if I go back to the editview to do the calculations correctly I have to insert the value without the point “.”

Hi,
as suggested earlier, add logging to your scripts and watch them while being executed. If the decimal point is actually causing the problem, try to parse/cast the value, but be cautious: the thousands/decimal separator can be changed in the user profile, so your script needs to reflect that.

Example:

regular settings:
chrome_Ub6aZYgW8P

with custom user settings:
chrome_TVH0IJRAQN

you can easily see that with my custom settings, your script would fail. If you want to build a stable solution, you therefore need to retrieve those user settings too.

Thank you for the detailed answer as that value will not be changed in my company. It would be enough for me to remove the dot “.” as a flag and then leave the value “4650” like this and do not set it like “4.650”.
Is there a simple method or do I have to tap the code again?

You can also try to parse/cast the value: https://jsfiddle.net/szh97e85/

But it would be possible too to remove the decimal character in general, maybe someone else here can assist you on that.

1 Like

I solved it by going to the studio and in the specific field I selected “disable format” and now the point “.” however I don’t understand why if I set some variables to read only when I return to the editview the fields disappear

Hi Diligent sorry for the inconvenience, I would need some help.

When I create a new quote all the calculations work and also the control of the min and from the max regarding the value “Ore Totali” I am attaching screenshots below.

As you can see both the min and max controls work. But see if I save now and go back to the editview what happens.

As you can see if I return to the editview the min and max controls no longer work and the user can enter any value and it shouldn’t be. Now I am attaching my code as I am trying in different ways but I cannot find the solution. I hope for your help thanks in advance.

MY CODE:
//CALCOLO ORE LAVORATIVE

$(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){

  console.log("my custom js has been loaded");

  $('#ore_c').val(Math.round($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val())); 

  console.log("custom js finished. result ore_c: " + $('#n_pezzi_c').val() / $('#tempo_ciclo_c').val());

});

  //CONTROLLO INSERIMENTO ORE

  $('#ore_c').change (function() {

    console.log("JS STARTED!!!");

    //Ottenere i valori massimi e minimi correnti dal campo

    var max = parseInt($(this).attr('max'));

    var min = parseInt($(this).attr('min'));

    

    if ($(this).val() > max)

    {

        $(this).val(max);

        alert("Hai selezionato un valore superiore al max corrente : " + max);

    }

    else if ($(this).val() < min)

    {

        $(this).val(min);

         alert("Hai selezionato un valore inferiore al min corrente : " + min);

    }      

  }); 

  

  $(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){

  //Calc. valore corrente e inserirmento nel campo

  var currValue = $('#n_pezzi_c').val() / $('#tempo_ciclo_c').val();

  $('#ore_c').val(Math.round(currValue));

  

  //debug

  var min = Math.floor(currValue);

  var max = Math.ceil(currValue);

  console.log("curr: " + currValue + " min: " + min + " max: " + max);

  

  //Settaggio nuovi attributi min/max 

  $("#ore_c").attr({

  "max" : max,       

  "min" : min      

  });

  });

//CONVERSIONE IN ORE

function convertNumToTime(number) {

  // Controlla il segno del numero specificato

  var sign = (number >= 0) ? 1 : -1;

  // Imposta il valore positivo del numero di segno negativo

  number = number * sign;

  // Separa l'int dalla parte decimale

  var hour = Math.floor(number);

  var decpart = number - hour;

  var min = 1 / 60;

 // Arrotonda al minuto più vicino

  decpart = min * Math.round(decpart / min);

  var minute = Math.floor(decpart * 60) + '';

  // Aggiungi imbottitura se necessario

  if (minute.length < 2) {

  minute = '0' + minute; 

  }

  // Aggiungi Accedi al risultato finale

  sign = sign == 1 ? '' : '-';

  // Concatena ore e minuti

  time = sign + hour + ',' + minute;

  return time;

}

$(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){

  console.log("my custom js has been loaded");

  $('#ore_totali_effettive_c').val(convertNumToTime(parseFloat($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()).toFixed(2)));

  console.log("custom js finished. result ore_totali_effettive_c: " + $('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()) ;

});

//CALCOLO TURNI/GIORNI/OPERAI

//TURNI

$(document).on('change',function(){

  console.log("JS started with turn calcolation");

  $('#turni_c').val(Math.round($('#ore_c').val() / ($('#durata_ore_turno_c').val() * $('#team_c').val())));

  console.log("custom JS finished. result turni_c: " + $('#ore_c').val() / $('#durata_ore_turno').val() * $('#team_c').val());

});

//GIORNI

$(document).on('change', function(){

  console.log("JS started with day calculation");

  $('#giorni_c').val(Math.round($('#ore_c').val() / ($('#durata_ore_turno_c').val() * $('#team_c').val() * $('#n_turni_giorno_c').val())));

  console.log("custom JS finished. result giorni_c: " + $('#ore_c').val() / $('#durata_ore_turno_c').val() * $('#team_c').val() * $('#n_turni_giorno_c').val());

});

//OPERAI

$(document).on('change',function(){

  console.log("JS started with operai calc");

  $('#operai_c').val(Math.round($('#ore_c').val() / $('#durata_ore_turno_c').val()));

  console.log("custom JS finished. result operai_c: " + $('#ore_c').val() / $('#durata_ore_turno_c').val());

});

The validation is added on-change, so I would try to add the missing validation on-page load (in jquery: $( document ).ready(function() {...}).

Hi Diligent I also thought about using .ready () and I’m trying to insert it but so far there have been no changes.

Can you kindly explain it to me?

Hi @Crazy_El3ctr0,
I saw your posts, I simply didn’t have the time to re-create a similar example in Suite (the fiddles are way easier/faster to generate). I’m pretty sure you have all the tools you need, it seems to be an issue of using the right listeners.

So apparently you need to add those restrictions not only on-change, but on-page-load too. You could consider creating a function (setCustomFieldRestrictions() ) that you can re-use in both listeners.

Further, use console.log() commands to create a temporary log in your browser console. You can also use the browsers debugger tool to figure out what happens exactly at which point of time.

Thanks now I’ll do some research on the internet so I can create a rule hoping it works! Thank you very much Diliegente, if you can give me some other help I would be grateful but if you are busy quiet! Thanks again

It seems like Math.round() is a better solution, but it is not! In some cases it will NOT round correctly. Also, toFixed() will NOT round correctly in some cases.

To correct the rounding problem with the previous Math.round() and toFixed(), you can define a custom JavaScript rounding function that performs a “nearly equal” test to determine whether a fractional value is sufficiently close to a midpoint value to be subject to midpoint rounding. The following function return the value of the given number rounded to the nearest integer accurately.

Number.prototype.roundTo = function(decimal) {
  return +(Math.round(this + "e+" + decimal)  + "e-" + decimal);
}

var num = 9.7654;
console.log( num.roundTo(2)); //output 9.77