web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Living In Technology / JavaScript to Allow Updatin...

JavaScript to Allow Updating Specific Fields Post Record complete.

jestuder Profile Picture jestuder 158

 Recently for a client I was given the requirement that they needed to update a custom field on activity records whether it was a completed record or not.  The problem when a record is completed, it is in read only status and users can't update the record easily, with minimal clicks.  To solve this business requirement, I wrote two JavaScript libraries.

1) UnlockFieldsOnLoad - This is the function you should list any field that you want to unlock if the record is in complete (read only status).  This function should run on load of the form and will unlock the fields so they can be updated.

2) OnChangeUnlockedFields - This function is what will do the save when the field(s) are updated.  What it does it check to see if the record is in a read only status.  If if it is, it will make the record active again.  After it makes the record activate again, it will write the change to the database, change the status back to the original status and save that change.

 function UnlockFieldsOnLoad(executionContext)  
{
var formContext = executionContext.getFormContext();
formContext.getControl("fieldname").setDisabled(false);
}
//Saves field value if form is read only.
function OnChangeUnlockedFields(executionContext)
{
var formContext = executionContext.getFormContext();
var stateCodeValue = formContext.getAttribute("statecode").getValue()
//We only want this JS to run if the record is in read only mode. 1=Completed, 2=Canceled
if(stateCodeValue === 1 || stateCodeValue === 2)
{
//Reactivate the record
formContext.getAttribute("statecode").setValue(0);
formContext.data.save(6).then(function ()
{
//Complete Record again
formContext.getAttribute("statecode").setValue(stateCodeValue);
formContext.data.save();
});
}
}


This was originally posted here.

Comments

*This post is locked for comments