Using a Variable for an Object

The JavaScript script for modifying a limit with the drop-down selection is:

var Selection=drp_Territory.Item(drp_Territory.SelectedIndex);

ActiveDocument.Sections["Limits Results"].Limits[1].SelectedValues.RemoveAll();
ActiveDocument.Sections["Limits Results"].Limits[1].SelectedValues.Add(Selection);
ActiveDocument.Sections["Limits Results"].Limits[1].Operator=bqLimitOperatorEqual;
ActiveDocument.Sections["Limits Results"].Recalculate()

Several words in the last four statements are repeated in each line—the path to Limits[1] and the path to Limits Results section. A variable can hold these objects (the repeated words), which makes the JavaScript logic easier to read.

Note:

This following exercise edits the JavaScript associated with drp_Territory from the previous exercise (in the Limits Dashboard section of Sample1mod.bqy) to use a variable for the object path.

Example

Refer to figure in the and study the last two sets of statements—using a variable for the object (path). Select one of the sets to enter in Design mode.

Start by commenting out the last four statements in your current script by typing /* before the first ActiveDocument.Sections statement and */ after the fourth one. This will make the four lines comment lines that JavaScript does not execute.

Enter one set of statements with a variable newChoice holding the object path. Do not enclose these statements in comments.

Test the new statements in Run mode. Selecting an item in the drop-down box should still change the pie chart results.

Caution!

Do not end an object path value with a period. The syntax error “missing name after. operator” refers to an incomplete object model path.

The first set of commented statements in the figure below creates a variable equal to navigating from ActiveDocument to Limits[1].

var newChoice;
newChoice=ActiveDocument.Sections["Limits Results"].Limits[1];
newChoice.SelectedValues.RemoveAll();
newChoice.SelectedValues.Add(Selection);
newChoice.Operator=bqLimitOperatorEqual;
ActiveDocument.Sections["Limits Results"].Recalculate()

The second set creates a variable equal to navigating from ActiveDocument to the Limits Results section (one step up in the hierarchy).

var newChoice;
newChoice=ActiveDocument.Sections["Limits Results"];
newChoice.Limits[1].SelectedValues.RemoveAll();
newChoice.Limits[1].SelectedValues.Add(Selection);
newChoice.Limits[1].Operator=bqLimitOperatorEqual