Wednesday, April 1, 2015

Logic Tricks or Shortcuts

Eliminate large IF conditions using Array Find

Instead of creating an if condition with several AND statements when you have a group of codes or values us an array and the FIND method.
Local array of string &AcceptCodes = CreateArray("AA", "AB", "AC", "EE");

If &AcceptCodes .Find(MY_REC.MY_CODE.Value) > 0 Then
   MessageBox(0,"",0,0,"Success. This code is one of the accepted Codes");
   rem Do logic of found condition;
Else
   MessageBox(0,"",0,0,"Denied. This code is not one of the accepted Codes");
   rem Do logic of other codes not in this group;
End-If;
This can also be done with a combination of values. Here I need to avoid producing a log message for a couple of Union code, benefit plan combinations.
Local array of string &UnPlnSuppress = CreateArray("XP3DENCPR", "XP3DENCPP", "XP3DENCCC");
/* if union code & plan combo not found in our suppress list produce message and warning. */
If &UnPlnSuppress.Find(&job.UNION_CD.Value | &bnPlan) = 0 Then
     MessageBox(0, "", 20002, 6, "No mapping found for %1. ", &employee);
End-If;

Loop through Numbered fields

PeopleCode @ operator

I had a derived record with 3 of the same question and response fields that the only difference to the field names is a number suffix. In my example the users are asked to select 3 questions and give 3 answers for password recovery. Each of the questions are either in the Q1 (pre-defined selected question) or C1 (customer defined question) and the R1 would be the response.
Derived Work Record.
  • MACID_Q1
  • MACID_Q2
  • MACID_Q3
  • MACID_C1
  • MACID_C2
  • MACID_C3
  • MACID_R1
  • MACID_R1
  • MACID_R3
I wanted to use a for loop to execute the same checks and save each one to as a single row in my table. To accomplish this I used the @ operator (The @ operator converts a string storing a definition reference into the definition).
Destination Record.
  • EMPLID
  • QUESTION_TEXT
  • ANSWER_TEXT
  • SEQUENCE
  • LAST_CHG_DATE
  • LAST_CHG_ID
For &i = 1 To 3;
   &rec = CreateRecord(Record.CHALLENGE_QA);
   &rec.EMPLID.Value = DERIVED.EMPLID;
   &rec.SEQUENCE.Value = &i;
   &fld = "DERIVED.MACID_C" | &i;
   If None(@&fld) Then
      &fld = "DERIVED.MACID_Q" | &i;
   End-If;
   &rec.QUESTION_TEXT.Value = @(&fld);
   &rec.ANSWER_TEXT.Value = @("DERIVED.MACID_R" | &i);
   &rec.LAST_CHG_DATE.Value = %Datetime;
   &rec.LAST_CHG_ID.Value = %UserId;
   &rec.Insert();
End-For;

Peoplebooks Documenation on the @ Operator

Left Pad number with Zeros

Using the Right Function and String function.
Right("00000" | 22, 4);
This will return the string "0022"

No comments:

Post a Comment