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.
  1. Local array of string &AcceptCodes = CreateArray("AA", "AB", "AC", "EE");
  2.  
  3. If &AcceptCodes .Find(MY_REC.MY_CODE.Value) > 0 Then
  4. MessageBox(0,"",0,0,"Success. This code is one of the accepted Codes");
  5. rem Do logic of found condition;
  6. Else
  7. MessageBox(0,"",0,0,"Denied. This code is not one of the accepted Codes");
  8. rem Do logic of other codes not in this group;
  9. 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.
  1. Local array of string &UnPlnSuppress = CreateArray("XP3DENCPR", "XP3DENCPP", "XP3DENCCC");
  2. /* if union code & plan combo not found in our suppress list produce message and warning. */
  3. If &UnPlnSuppress.Find(&job.UNION_CD.Value | &bnPlan) = 0 Then
  4. MessageBox(0, "", 20002, 6, "No mapping found for %1. ", &employee);
  5. 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
  1. For &i = 1 To 3;
  2. &rec = CreateRecord(Record.CHALLENGE_QA);
  3. &rec.EMPLID.Value = DERIVED.EMPLID;
  4. &rec.SEQUENCE.Value = &i;
  5. &fld = "DERIVED.MACID_C" | &i;
  6. If None(@&fld) Then
  7. &fld = "DERIVED.MACID_Q" | &i;
  8. End-If;
  9. &rec.QUESTION_TEXT.Value = @(&fld);
  10. &rec.ANSWER_TEXT.Value = @("DERIVED.MACID_R" | &i);
  11. &rec.LAST_CHG_DATE.Value = %Datetime;
  12. &rec.LAST_CHG_ID.Value = %UserId;
  13. &rec.Insert();
  14. End-For;

Peoplebooks Documenation on the @ Operator

Left Pad number with Zeros

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

No comments:

Post a Comment