Setting value in string field based on radio button selection

0
Hi all, New to Mendix and rusty on Javascript,  essientially wanting to set a field value depending on option chosen from four radio buttons. Script now bombing so not sure where to go. Thanks!   Think problem with MxObject usage. Project is to log enquiries from clients, here’s code to date: /** * @param {"MyFirstModule.type_values.Compliment"|"MyFirstModule.type_values.Complaint"|"MyFirstModule.type_values.Suggestion"|"MyFirstModule.type_values.Historic"} currentState * @param {MxObject} setRecTo * @returns {Promise.<boolean>} */ MxObject.fetch("MyFirstModule.type_values.Compliment"|"MyFirstModule.type_values.Complaint"|"MyFirstModule.type_values.Suggestion"|"MyFirstModule.type_values.Historic", "MyFirstModule.Person/MyFirstModule.RefCode", function(currentState, setRecTo) { alert("current value " + setRecTo.get("RefCode")); }) MxObject.getOptions("currentState"); if (currentState(0) = true) { setRecTo.set("MyFirstModule.Person/MyFirstModule.RefCode", "CMP"); } if (currentState(1) = true) {   setRecTo.set("MyFirstModule.Person/MyFirstModule.RefCode", "COM"); alert("new value " + setRecTo.get("RefCode")); } if (currentState(2) = true) { setRecTo.set("MyFirstModule.Person/MyFirstModule.RefCode", "SUG");   } else { setRecTo.set("MyFirstModule.Person/MyFirstModule.RefCode", "LTE");   }  
asked
2 answers
1

Hi Paul, 

I think there is no need for javascript. Just add a OnChange action on your radiobuttons and set the RefCode there. 

Edit: if you really want to use javascript to set the value, here is a possible solution. Note that I am using the Enum-Keys in the case statement.
This is a fragile construct, as changes in the enumeration will not be checked and there is afaik no builtin model reflection for javascript actions. I would generally not recommend doing this. 

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import { Big } from "big.js";

// BEGIN EXTRA CODE
// END EXTRA CODE

/**
 * @param {MxObject} mxobj
 * @returns {Promise.<void>}
 */
export async function JavaScript_action(mxobj) {
	// BEGIN USER CODE
	var state = mxobj.get("CurrentState");

		switch (state) {
			case "Compliment":
				mxobj.set("RefCode", "CMP");
				break;
			case "Complaint":
				mxobj.set("RefCode", "COM");
				break;
			case "Suggestion":
				mxobj.set("RefCode", "SUG");
				break;
			case "Historic":
			default:
				mxobj.set("RefCode", "LTE")
				break;
			
		}

	// END USER CODE
}

 

answered
1

Additionally, use the function getCaption() to use the caption value of the enum.

answered