Run JavaScript

This action allows you to inject Javascript into any part of your application.  The action configuration has a JavaScript editor which allows you to validate your Javascript before saving it.  

Notes

  • When this action is called it runs JavaScript entered into the editor.  
  • The editor will alert you if there is an error in your JavaScript code.  Hover over the alert to see a hint or message about the error.

Tips, Tricks, and Examples

Start a JS action.

The following line should be added to the beginning of the action's expression in the JavaScript Editor.

    //runAs JavaScript
  

Store the MVC (View) to a variable.

Add to a stored variable expression you have created, create a new record on a view, update values in a view, save a view or even delete records.  

The first thing you want to do is store the MVC (view) object that you want to reference in a variable.  The way you accomplish this depends on what view you want to reference.  

Store the current view that is executing the action into a variable.

  • This script is easiest to use when you are working within the view where you are executing the JavaScript.
    var mvc = expreng.currexpression.mvc;
  

Store a top level opened view into a variable.

  • This is useful if you are trying to reference another top level view.
    var mvc = app.dv.cache.mvcs.getVal(viewID);
  

Reference any view that embedded in another view.

  • This is useful if you are trying to access an embedded view or a related view. This JavaScript will return only the first instance of the view.
    var mvc = app.dv.mvc.findFirstLoadedMVC(viewID);
  

ViewID - The ID of the View needed, it can be found on the View Editor.  

Create a New Record

Once you have the MVC you would like to reference, you can add a new record to that MVC with the following Javascript.

    app.dv.actions.newRecord.adHoc(mvc, callback, source)
  
  • mvc - the mvc to add new record to.
  • callback - the result of the new record action. (not required)   
  • source - the source of the MVC making the call. (not required)

Update a Selected Value

To update a value on a selected record in a view, the following JavaScript can be used.

    mvc.selectedRecord.gridValues.getVal(PropertyID).displayProperties.Text = 'Value'
  
  • PropertyID - The ID of the property in the record type driving the view.  This numeric value can be found in the details of the Property Editor.  

Save Data

Saving data is an asynchronous process so the callback should be used if you would like to perform another action to the data after the data is saved.

    app.dv.actions.saveAction.adHoc(mvc,callback,source,SkipNewRecords,SuppressSpinner,saveForRefresh)
  
  • mvc - the MVC (view) you wish to save
  • callback - the result of the save action. Where it was successful or not.  (See example below). (not required unless saving the data is required before referencing the grid again.)
  • source - the source of the MVC making the call. (not required)
  • SkipNewRecord - Skip saving any new records in the view. true/false.  (not required)
  • SuppressSpinner - True if you would like to show the busy indicator. False if you would like to hide the busy indicator. (not required)
  • saveForRefresh - This is a flag that is used when saving during a refresh action produces dirty values.  (not required)

Delete a Selected Record

To delete the selected record from a specified view, the following JavaScript can be used.

    app.dv.actions.deleteAction.adHoc(mvc, callback, source, suppressPrompt)
  
  • mvc - the mvc to delete the selected record from.
  • callback - the result of the new record action. (not required)
  • source - the source of the MVC making the call. (not required)

Filter and Select a Single Record in the Grid

To Select a specific record in a mvc's grid, the following Javascript can be used.

If more than one value is returned, only the first record will be placed into a variable and selected.

    //runAs JavaScriptvar mvc = expreng.currexpression.mvc;
var recordMatch = mvc.records.Values.where(function(gr) {   
  return gr.gridValues.Values.where(function(gv) {       
    return gv.property.Name == '{PropertyName}';   
  }).first().displayProperties.Text == '{PropertyValue}';
}).first();
app.dv.mvc.setSelectedRecord(mvc, recordMatch);
  

Viewing Actions for a View

By accessing the console window in your web browser (F12),  you can see all of the actions and their structure.

JavaScript Example

Below is JavaScript that can be used to create a new record in a view (ID: 120), update a "Status" property (ID: 1629) to "New" and save the record to the database. 

//runAs JavaScript

//Get the current MVC
var mvc = app.dv.mvc.findFirstLoadedMVC(120);

//Add a new record to the view.
app.dv.actions.newRecord.adHoc(mvc);

//Set value of the status property to new
mvc.selectedRecord.gridValues.getVal(1629).displayProperties.Text = 'New';

//Save the record
//The callback can be left blank if nothing needs to be performed after save
app.dv.actions.saveAction.adHoc(mvc, function(e) {
    message = e.success ?
'Data saved successfully!' :
'There was an error saving the data.';
app.browser.notifier.showToast(message, 3000);
});
 
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.