BPC-Components

Home > Products > BPC PowerForms - Silverlight > Knowledge Base > Scripting and Formulas > Script Examples

Script Examples

Get the Modified, Created, Author and Editor column values from script.

The above system columns do not create bound controls during form initialization.

You can get the values though through script and set these values inside your custom controls through script :

You should add the following script in the "Load Completed" script of the form : Designer > Options > Scripting > Load Completed

Code

created = dataitem.GetValue("Created")
template.GetControl("c_Created") .SetValue(created)

modified = dataitem.GetValue("Modified")
template.GetControl("c_Modified") .SetValue(modified)

# THE RETRIEVED VALUE IS IN THE ID;#TITLE FORMAT
author = dataitem.GetValue("Author")
# GET THE ID PART
authorId = dataitem.GetValue_Value("Author")
# GET THE TITLE PART
authorName = dataitem.GetValue_Name("Author")
template.GetControl("c_Author") .SetValue(authorName)

editor = dataitem.GetValue("Editor")
template.GetControl("c_Editor") .SetValue(editor)

 

Apply Tab and Section Permmissions

You can enable, disable, show or hide tabs through script.
So in the Load Completed script or in the Value Change event of a control, you can write something like the following :

Code

If template.Functions.UserInGroup("Admins") :
  template.ShowTab(1)
  template.EnableTab(2)
  template.HideSection(3)
else :
  template.HideTab(1)
  template.DisableTab(2)
  template.ShowSection(3)

Note : the template.Functions property includes the functions used from formulas too :

Code

string ColumnDescription(string colInternalName)
string ColumnTitle(string colInternalName)
string ColumnType(string colInternalName)
bool ColumnIsRequired(string colInternalName)
string FieldValue(string controlName)
string FieldDisplayValue(string fieldname)
string FieldValue_Value(string controlName)
string FieldValue_Name(string controlName)
string LoginName()
string UserID()
string UserFullName()
bool UserInGroup(string groupName)
bool UserInGroupById(string groupId)
string ListName()
string ListId()
string ListUrl() 

 

Set a value to a cascading lookup/combo.

There are cases that you need  to set the value to 2 controls (from script) with the second being affected by the first one.

For example you have 2 dropdown controls (ComboBoxes, LookupPickers, etc) one for Countries and one for Cities.

The Cities combo is affected by the Countries combo, meaning that when the user selects a Country, the Cities combo is forced to reload its data.

If you try to set the values to both controls using the following script, it should fail for the Cities combo since the time we are setting the value, the combo values are not loaded yet (loading is asynchronous).

Code

# THIS CODE WILL NOT WORK

template.GetControl("c_Countries").SetValue("1;#Country1")

template.GetControl("c_Cities").SetValue("34;#City34")

For that reason a new method has been introduced which set the value to a lookup control and the control will set the value as soon as loading is complete.

The method is called : SetValueOnLookupLoaded

The folloing script will work as expected.

Code

template.GetControl("c_Countries").SetValue("1;#Country1")

template.GetControl("c_Cities").SetValueOnLookupLoaded ("34;#City34")

 

ListDataGrid available methods

The ListDataGrid control includes some useful methods which can be used through script:

Code

# PROPERTY THAT RETURNS ALL THE DATA RECORDS OF THE GRID
items = c.InputControl.DataItems
count = items.Count
id = items[0].GetValue("ID")

# RELOAD GRID
c.InputControl.SetValue("")
# OR
c.InputControl.RefreshData()

# GET A VALUE FROM THE SELECTED RECORD
c.InputControl.GetGridItemValue("ID")

# SELECT A ROW BY INDEX
c.InputControl. SelectGridRow(3)

# SELECT A RECORD BY A VALUE EXAMPLE
id = template.GetControl("c_ID").GetValue()
grid = template.GetControl("c_DetailData")
count = grid.InputControl.DataItems.Count
grid.InputControl.SelectGridItem(None)
for i in range(0, count) :
  item = grid.InputControl.DataItems[i]
  if item.GetValue("ID") == id :
    grid.InputControl.SelectGridItem(item)

Set the value of a control during form loading

In order to set the value of a control during form initialization (usually for new records) you can use 3 different methods.

  1. Set a default value for that control
  2. Pass the value in the querystring
  3. Use script

To set the value you must use the LoadCompleted script and add something like the following :

Code

# The "dataitem" variable references the record being displayed

# For new records, it will only contain the default values in ColumnName/Value pairs

# Only for New records set the priority to HIGH

if dataitem.GetValue("ID") == "" :

  dataitem.SetValue("Priority", "High")