How to Sum Variable on List Form

jversusjjversusj Member Posts: 489
Hello. I did search the forum for this subject, but did not find anything relating specifically to my question.

We have the Calendar Views granule and have built some custom calendar views to help with managing Jobs - specifically showing total job qty ordered per day per month. clicking the day on this form (which i believe we got from Cost Control) opens another form that shows the Jobs that make up that qty. This form is called against a MARK(TRUE) recordset because i have had to go backwards from the job specific router to the job for the view to show certain types of work that needs to be done.

This all works fine.

Now, I have a user request that at the bottom of this list form, there should be a field showing the Job Remaining Qty (what still needs to be produced on this list). When i attempt to call code to loop through the records shown in the list and sum the job remaining qty, it does not seem to be working with the same MARKed recordset. When i put code in the OnAfterGetRecord or OnAfterGetCurrRecord triggers to sum this value, I find that it keeps adding to the variable as I scroll through the list.

Any tips for how to accomplish this? Maybe I just need a push toward the right trigger to call this from.

bottom line, i have a form opening that contains a tablebox. This tablebox includes prefiltered (Marked) records from the job table. how can I sum the job remaining qty for all records in the list?

Thanks!
kind of fell into this...

Answers

  • krikikriki Member, Moderator Posts: 9,110
    Create a function:
    Function CalculateTotals(VAR Irec : "your table") : OdecTotal as Decimal
     local variable : Lrec : record : "your table"
    
    OdecTotal := 0;
    Lrec.COPYFILTERS(Irec);
    IF Lrec.FINDSET THEN
      REPEAT
        OdecTotal += Lrec."job remaining qty";
      UNTIL Lrec.NEXT = 0;
    

    This function you can put in the SourceExpression of your total-field like this "CalculateTotals(rec)".
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • jversusjjversusj Member Posts: 489
    thank you very much for replying.

    this is essentially what i was doing before, but more elegant in that you are calling the function from the control (and i was trying to call it from triggers). unfortunately, it still does not seem to work. it appears that the qty is not just those in the data set shown on the form.

    here is what i am doing:
    there is the Calendar View form, and i have added a view for Bagging Job Qty by End Date. There is C/AL that looks at Job Specific Router table for a Step Resource No. = Bagging. It adds the Job qty Ordered and shows this on the Calendar view so that production can see how many total pieces need to be bagged on that day.

    When you double click on a given day, it enters C/AL that sets the same filters on the Job table, and loops through records, MARKing those Jobs that meet some other criteria (not all my filterable data exists at the job specific router table level, i have to look at the Job itself as well). i then call a form by RUNMODAL, passing the marked record set.

    This pops my form which shows the jobs that need production for that day. i want to have a totals field at the bottom of the form that displays total remaining outstanding qtys.

    When i use the function like you have demonstrated, it does not seem to copy the marks - since i am copying filters and not copying marks... i will play around with this some more and see what i can do. any other advice is greatly appreciated.

    EDIT:
    okay, i tried to use COPY instead of COPYFILTERS since the former is supposed to bring the marks along with it.
    My function therefore looks like this:
    CalculateTotals(VAR IRec : Record Job) dQtyRemain : Decimal
    MESSAGE('going to copy rec');
    LRec.COPY(IRec);
    IF LRec.FINDSET THEN BEGIN
       MESSAGE('found rec');
       REPEAT
          dQtyRemain += LRec."Job Qty Remaining";
       UNTIL LRec.NEXT = 0;   
    END ELSE
      MESSAGE('did not find');
    

    the messages are there for testing. i am getting the 'did not find' message! :( as a result i get dQtyRemain = 0. Any ideas?
    kind of fell into this...
  • jversusjjversusj Member Posts: 489
    i found that if i do not initiatlize the LRec, and instead work with IRec (from Kriki's example), it works!

    any idea why it does not work if i use the LRec variable?
    EDIT:

    This is what i did to get it to work:
    //On OpenForm, i added this single line of code:
    recJob.COPY(Rec); 
    
    CalculateTotals(VAR IRec : Record Job) dQtyRemain : Decimal
    {--Calculate Total Qty Remaining--}
    IRec.MARKEDONLY(TRUE);
    IF IRec.FINDSET THEN BEGIN
       REPEAT
          dQtyRemain += IRec."Job Qty Remaining";
       UNTIL IRec.NEXT = 0;
    END ELSE
      MESSAGE('did not find');
    

    then my control calls the function using recJob. It works, but i worry that is not ideal.
    kind of fell into this...
  • krikikriki Member, Moderator Posts: 9,110
    In my example, I supposed you already had the MARKEDONLY(TRUE) on your record.

    SO my example should be:
    Function CalculateTotals(VAR Irec : "your table") : OdecTotal as Decimal
    local variable : Lrec : record : "your table"
    
    OdecTotal := 0;
    Lrec.COPYFILTERS(Irec);
    Lrec.MARKEDONLY(TRUE);
    IF Lrec.FINDSET THEN
      REPEAT
        OdecTotal += Lrec."job remaining qty";
      UNTIL Lrec.NEXT = 0;
    
    Basically, I do the same thing as in your last example that works with the difference that I use a local variable, leaving the original record-position untouched. With your code, the form will select the latest record to show.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • jversusjjversusj Member Posts: 489
    thank you Kriki, but for some reason, when i use your code it does not work. your method returns 0 records?

    when i do it the way i posted, it finds the records and totals as desired. I'm sure there is an explanation, but it seems to work so i guess i am satisfied.
    kind of fell into this...
  • krikikriki Member, Moderator Posts: 9,110
    Probably it doesn't copy the marks. I checked the help when I wrote the solution to be sure the filter on marks works with it. And in the example I found on the help, it seemed it should work. But in reality it seems the marks aren't copied.

    The important thing is that it works. :D
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.