Get and Set Field Values using FormContext and JavaScript with Dynamics 365 and PowerApps

1 Comment

In this post, we will look at how to get and set field values using FormContext and JavaScript.

Let’s say we want to get and set the Website field of the Account form:

Let’s do this on Save of the form. Edit the form and go to Form Properties:

Create a new JScript web resource:

Add the following code:

function GetSet(executionContext) {

var formContext = executionContext.getFormContext();

// Get value
var website = formContext.getAttribute("websiteurl").getValue();
alert(website);

// Set value
formContext.getAttribute("websiteurl").setValue("http://newvalue.com");

// Alert new value
website = formContext.getAttribute("websiteurl").getValue();
alert(website);

}

Save and publish:

Add the new function to the OnSave event:

Ensure to pass the execution context:

Now go to the form, make a change and Save.

We see the website field is read using getValue:

And then changed using setValue:

With the new value displayed on the form:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

One Response to Get and Set Field Values using FormContext and JavaScript with Dynamics 365 and PowerApps

  1. what is the recommended way of reading in javascript a form field?
    var nameField = executionContext.getFormContext().data.entity.attributes.getByName(“name”);
    var nameField1 = executionContext.getFormContext().getAttribute(“name”);

    i see nameField == nameField1 is true

Leave a Reply

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