How to Retrieve Field Values in Power Apps Model-Driven Apps Using JavaScript

AJ Zafar

AJ Zafar

Solution Architect

Navigating through the diverse field types in Power Apps Model-Driven Apps might seem to be a tricky task. This guide simplifies this by grouping similar field types together, demonstrating how to effectively retrieve values for each type. 

Gaining Access to the Form Context

To interact with data on your Power Apps form, you first need to obtain the form context. This is done through the execution context. Here’s how:

				
					function yourSnazzyFunction(executionContext) {
    const formContext = executionContext.getFormContext();
    // formContext is now ready to use
}

				
			

Now, let’s break down the field types you can get using the formContext:

Two Options, Choice, and Choices Field Types

Two options, choice fields, and multi-choice fields are used for selections and can be handled in a similar fashion:

Two Options (Yes/No):

				
					let twoOptionValue = formContext.getAttribute("twoOptionField").getValue(); // true or false

				
			

Choice and Choices (Option Sets):

				
					let choiceValue = formContext.getAttribute("choiceField").getValue(); // Numeric value
let choiceText = formContext.getAttribute("choiceField").getText(); // Text label

				
			

Numeric Field Types

Numeric fields encompass various data formats, from integers to floating points and currency. They are all simple to get and use the same method.

Whole, Decimal, Floating Point Numbers, and Currency:

				
					let wholeNumberValue = formContext.getAttribute("wholeNumberField").getValue();
let decimalValue = formContext.getAttribute("decimalField").getValue();
let floatValue = formContext.getAttribute("floatField").getValue();
let currencyValue = formContext.getAttribute("currencyField").getValue();
				
			

Text Field Types

Text fields cover a range of textual data, from single lines to rich content:

Text Fields (Single Line, Multi-Line, Rich Text):

				
					let textFieldValue = formContext.getAttribute("textField").getValue();
let multilineTextValue = formContext.getAttribute("multilineTextField").getValue();
let richTextValue = formContext.getAttribute("richTextField").getValue(); // HTML content
				
			

Date and Date Time Fields

Date fields are important for handling calendar data:

Date and Date Time Fields:

				
					let dateValue = formContext.getAttribute("dateField").getValue(); // Returns a Date object set at midnight
let dateTimeValue = formContext.getAttribute("dateTimeField").getValue(); //Includes specific date and time
				
			

Lookup Fields

Lookup fields are a bit more complex, as they can store multiple pieces of information about a related record:

Lookup Fields – Retrieving Details:

				
					let lookupField = formContext.getAttribute("lookupField").getValue();
if (lookupField) {
    let lookupRecordGuid = lookupField[0].id; // GUID of the related record
    let lookupEntityName = lookupField[0].entityType; // Entity type (schema name)
    let lookupRecordName = lookupField[0].name; // Name of the related record
    }
				
			

In this snippet, lookupField[0].id retrieves the GUID, lookupField[0].entityType gets the entity name (schema name), and lookupField[0].name provides the name of the related record.

To Conclude 😀

Understanding how to retrieve values from various field types in Model-Driven Apps is a fundamental skill for Power Apps developers looking to extend and customise model driven apps. This guide provides a strong foundation for handling different data types.

Check out the Client API Reference for model-driven apps – Power Apps | Microsoft Learn for more information on this. My next post will cover how to get additional data from the related record a lookup field is referencing.

Remember, the more you practice, the more proficient you’ll become. Happy power-apping!

AJ Zafar

AJ Zafar

Solution Architect

Leave a Reply

Your email address will not be published. Required fields are marked *