Array example

Nice little example of using arrays in PeopleCode. Probably a more elegant, 2 dimension solution but this works just fine.

This example was required to handle overriding GL distribution by Elements, which are on level 3. and element can appear on level 3 under many level 2 rows. Code loops through all possible rows on level 3 and sums total for each unique Element found. Must add to 100.

/* Check that sum of each given element, across all pin rows, adds to 100%. */

/* 2 arrays to hold all elements and sum of their percentages. */
&arrElements = CreateArray();
&arrPercent = CreateArray();

/* Populate the arrays. */
Local Rowset &rsGP_GL_DIST_DTL = GetRowset(Scroll.GP_GL_DIST_DTL);
For &i = 1 To &rsGP_GL_DIST_DTL.ActiveRowCount
Local Rowset &rsGP_GL_DIST_PIN = &rsGP_GL_DIST_DTL(&i).GetRowset(Scroll.GP_GL_DIST_PIN);

/* Check there are actually any element rows (if one does they all do, vice-versa). Populate arrays if found.*/
If &rsGP_GL_DIST_PIN(1).GP_GL_DIST_PIN.PIN_NUM.Value = 0 Then
&hasElements = False;
Break;
Else
&hasElements = True;
For &j = 1 To &rsGP_GL_DIST_PIN.ActiveRowCount
&element = &rsGP_GL_DIST_PIN(&j).GP_PIN_DESCR_VW.DESCR.Value;
&percent = &rsGP_GL_DIST_PIN(&j).GP_GL_DIST_PIN.PCT_TO_ALLOCATE.Value;
&found = &arrElements.Find(&element);
If &found = 0 Then
&arrElements.Push(&element);
&arrPercent.Push(&percent);
Else
&arrPercent [&found] = &arrPercent [&found] + &percent;
End-If;
End-For;
End-If;

End-For;

/* Check for any elements that are <> 100% and advise with warning msg & highlighting the field. */
If &hasElements Then
For &k = 1 To &arrPercent.Len

&err = False;
If &arrPercent [&k] <> 100 Then
&err = True;
End-If;

For &i = 1 To &rsGP_GL_DIST_DTL.ActiveRowCount
&rsGP_GL_DIST_PIN = &rsGP_GL_DIST_DTL(&i).GetRowset(Scroll.GP_GL_DIST_PIN);
For &j = 1 To &rsGP_GL_DIST_PIN.ActiveRowCount
If &rsGP_GL_DIST_PIN(&j).GP_PIN_DESCR_VW.DESCR.Value = &arrElements [&k] Then
If &err Then
&rsGP_GL_DIST_PIN(&j).GP_GL_DIST_PIN.PCT_TO_ALLOCATE.SetCursorPos(%Page);
&rsGP_GL_DIST_PIN(&j).GP_GL_DIST_PIN.PCT_TO_ALLOCATE.Style =
"PSERROR";
Error
"Sum percentages of Element '" | &arrElements [&k] | "' do not total 100%.";
Else
&rsGP_GL_DIST_PIN(&j).GP_GL_DIST_PIN.PCT_TO_ALLOCATE.Style =
"PSEDITBOX";
End-If;
End-If;
End-For;
End-For;

End-For;
End-If;

0 comments: