Assigning number values to string choice fields and using those value numbers for another field

Hello,
I created choice fields which include string text choices, for example:

Question1 choice field -
A. We have written acceptance.
B. We only have verbal acceptance.
C. Not accepted.

Question2 choice field -
A. We have written offer.
B. We only have verbal offer.
C. No offer.

Each of these choice fields will have a score depending on their choice. Example: A = 0, B = 3, C = 6. If the user chooses B then another field (RatingField1) will take the number 3 as its value. RatingField1 = 3, RatingField2 = 3, etc.

Is there a way to do this in Plumsail? Maybe through code with a formula depending on their choice?
Thank you,
Lun

Dear @Iso,
Good question! There are actually dozens of ways this can be done - you just need to match these answers to some value in JavaScript, and it can be done in a variety of ways.

Since you're using A, B and C as part of your answer (these letters actually appear in every choice, right?) - you can use these letters for matching answers to values, and then run a function which will update a specific rating field, like this:

fd.spRendered(function(){
  //calculate rating on change
  fd.field('Question1').$on('change', function(value) {
    calculateRating(value, 'Rating1');
  });
  fd.field('Question2').$on('change', function(value) {
    calculateRating(value, 'Rating2');
  });
});

function calculateRating(value, ratingFieldName){
  switch (value.charAt(0)) {
      case 'A':
        fd.field(ratingFieldName).value = 0;
        break;
      case 'B':
        fd.field(ratingFieldName).value = 3;
        break;
      case 'C':
        fd.field(ratingFieldName).value = 6;
        break;
      default:
        fd.field(ratingFieldName).value = null;
    }
}

Hi @Nikita_Kurguzov ,
Thank you, this coding worked! To make things a bit more difficult I have different formats for each question. I have 5 total questions with Question2, Question4, and Question5 being boolean type answers of either Yes or No and giving Yes choice answers a number value such as 0 and No a value of 6. To make things more complicated we have different scoring formats for each question. Here's the code I'm trying to use where the boolean questions are not giving any score when testing:

fd.spRendered(function(){
  //calculate rating on change
  fd.field('Question1').$on('change', function(value) {
    calculateRating(value, 'Rating1');
  });
    fd.field('Question2').$on('change', function(value) {
    calculateRating2(value, 'Rating2');
  });
      fd.field('Question3').$on('change', function(value) {
    calculateRating3(value, 'Rating3');
  });
      fd.field('Question4').$on('change', function(value) {
    calculateRating4(value, 'Rating4');
  });
      fd.field('Question5').$on('change', function(value) {
    calculateRating5(value, 'Rating5');
  });
});

function calculateRating(value, ratingFieldName){
  switch (value.charAt(0)) {
      case 'A':
        fd.field(ratingFieldName).value = 1;
        break;
      case 'B':
        fd.field(ratingFieldName).value = 3;
        break;
      case 'C':
        fd.field(ratingFieldName).value = 6;
        break;
      default:
        fd.field(ratingFieldName).value = null;
    }
}

function calculateRating2(value, ratingFieldName){
  switch (value.charAt(0)) {
      case 'Yes':
        fd.field(ratingFieldName).value = 0;
        break;
      case 'No':
        fd.field(ratingFieldName).value = 6;
        break;
      default:
        fd.field(ratingFieldName).value = null;
    }
}

function calculateRating3(value, ratingFieldName){
  switch (value.charAt(0)) {
      case 'A':
        fd.field(ratingFieldName).value = 0;
        break;
      case 'B':
        fd.field(ratingFieldName).value = 6;
        break;
      case 'C':
        fd.field(ratingFieldName).value = 6;
        break;
      default:
        fd.field(ratingFieldName).value = null;
    }
}

function calculateRating4(value, ratingFieldName){
  switch (value.charAt(0)) {
      case 'Yes':
        fd.field(ratingFieldName).value = 8;
        break;
      case 'No':
        fd.field(ratingFieldName).value = 0;
        break;
      default:
        fd.field(ratingFieldName).value = null;
    }
}

function calculateRating5(value, ratingFieldName){
  switch (value.charAt(0)) {
      case 'Yes':
        fd.field(ratingFieldName).value = 8;
        break;
      case 'No':
        fd.field(ratingFieldName).value = 0;
        break;
      default:
        fd.field(ratingFieldName).value = null;
    }
}

Any assistance would be appreciated. Thank you!

Dear @Iso,
I've used a switch in my function, because it seemed like the best fit in your case - multiple options is easier to handle with switch. You can read more about switch and its use cases here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

You don't have to use it - a simple if() can work just as well. For example:

function calculateRating2(value, ratingFieldName){
  if(value){
        fd.field(ratingFieldName).value = 0;
  }
  else{
        fd.field(ratingFieldName).value = 6;
  }
}

This should work, because Yes/No fields actually return true/false as boolean, not a string of "Yes"/"No"

P.S. Also, note that value.charAt(0) is used specifically to get ONLY the first letter of a string (such as "A", "B" or "C") - don't use it when not necessary.

1 Like

This is great @Nikita_Kurguzov! It works for me. Thank you!