Workaround to change primary column value from one column to another using JavaScript Workaround to change primary column value from one column to another using JavaScript
Hiren Senjaliya

Hiren Senjaliya

August 22, 2025

All Post
Change Primary Column Value Using JavaScript
Share:

In Dataverse, the primary column serves as a key identifier that plays a crucial role in how data is displayed and managed. Whether you’re working with important data or adding records manually, the primary column must contain a unique value.
If you need to update a column value using JavaScript, especially the primary column within a model-driven app, here’s how to do it effectively.

Scenario: Imagine you are importing data into a Dataverse table by copying and pasting from external sources. During this process, there’s a risk that the primary column might end up with incorrect or missing values.

This issue becomes particularly problematic when handling more than 1,000 records manually correcting the primary column would be time-consuming and inefficient.

Since the primary column in Dataverse is essential for uniquely identifying records, you need a way to fix or assign values to it automatically.
This is where changing the primary column value JavaScript becomes extremely useful.

In the case described here, some phone numbers and ID column values have been swapped. While the correct IDs are stored in another column, “Student ID,” you cannot set that column as the primary one. This leads to inconsistencies in how the records are identified. To address this issue, we need a workaround to copy the values from the “Student ID” column to the primary column (referred to as the “ID” column in this scenario) using JavaScript when the form loads.

column to the primary column

Here are the steps to automate the process using JavaScript, ensuring that the primary column is populated with a unique and non-modifiable value for each record.

Step 1: The following JavaScript code is used to copy the Student ID column values to the ID column.


function setIDValue(executionContext) {
// Get the form context
var formContext = executionContext.getFormContext();

// Get the value from the ‘cr32f_studentid’ attribute
var Id = formContext.getAttribute(“cr32f_studentid”).getValue(); // Use getValue() to get the value

// Set the value in the ‘cr32f_id’ attribute
formContext.getAttribute(“cr32f_id”).setValue(Id);

// Save the form to persist changes
formContext.data.entity.save();
}

Step 2: To run JavaScript updates data in a table column, you need to add the internal names of both columns. To find these, go to the Dataverse table, then navigate to the columns section, and copy the logical names of both columns.

Step 2

Step 3: After saving the JavaScript, go to the Dataverse table, navigate to the form, click on ‘JavaScript’ from the left navigation pane, and then click on ‘Add Library’.

Step 3

Step 4: After clicking on ‘Add Library,’ click on ‘New Web Resource,’ select the file, provide a name for the file, and then click ‘Save’ and ‘Publish .’

Step 4

Step 5: Now, search for that file, select it, and click on ‘Add.’

Step 5

Step 6: Select the form, then click on ‘Events’ on the right side. In the events section, click on ‘On Load.’

Step 6

Step 7: After clicking on ‘Event Handler,’ select the JavaScript file, enter the function name, and check ‘Pass execution context as the first parameter.’ Then click ‘Done,’ and save and publish the application.

Step 7

Step 8: After that, when you click on existing data, the script will run and set the Student ID value to the ID column.[JP5]

Step 8

Conclusion

By following these steps, you can automate the process of updating the primary column with the correct values from another column, ensuring that each record in your Dataverse table has a unique and accurate primary identifier. This approach not only saves time but also improves data integrity, making your data management more efficient and reliable. For organizations working with complex Microsoft ecosystems, especially those guided by SharePoint Premium Consultants, this kind of automation ensures seamless integration, better governance, and smarter data handling across platforms.

Leave a Reply

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

Want to talk?

Drop us a line. We are here to answer your questions 24*7.