Wednesday, January 15, 2014

Function Library

I can be a bit of a minimalist when writing code I also firmly believe in the DRY(don't repeat yourself) principle.  This is what makes me a firm believer in using and reusing functions.  In peoplecode there are many function libraries to do work for you but you can also create you own and here's how.

  1. Create a new dynamic/derived record MY_TST_FUNCS
  2. Add a field related to the function(s) you wish to included AMOUNT
  3. Right click on the field and edit the Peoplecode
  4. Make sure you write your code in the Field Formula of the field.
    e.g. 
    /******************************************************
     Function   : doubleAmount
     Purpose    : Double the amount given
     Parameters : &AMOUNT
     Returns    : Number
    ******************************************************/
    Function doubleAmount(&AMOUNT) Returns number;
       Local number &newAmount;
       &newAmount = &AMOUNT + &AMOUNT;
       Return &newAmount;
    End-Function;
  5. Using the function you just need to Declare it at the top of you block of code
    e.g.  
    /* Functions */
    Declare Function doubleAmount PeopleCode MY_TST_FUNCS.AMOUNT FieldFormula;
    Local number &amt, &doubled;
    &amt = 2;
    &doubled = doubleAmount(&amt);
You can create any number of functions under any number of fields. For more examples look at the record FUNCLIB_HR.

NOTE: Values are passed by reference so if you change a value in the function it will have changed the original after you have returned from that function.

No comments:

Post a Comment