validating data before writing them to a table

asemberengasembereng Member Posts: 220
I have a history table but i want to validate whether the data already exists on that table before it writes to it. If it exists it ask you whether you want to reprocess it else it writes to it. Below is what i have written so far.. Can someone help me why is not displaying the message when it finds the record?
payMonthYear := FORMAT(RunDate,0,'<Month text> <Year4>');
history.SETRANGE(empid,employee."No.");
history.SETRANGE(MonthYear,payMonthYear);
IF NOT history.FINDLAST THEN BEGIN


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{  payMonthYear := FORMAT(RunDate,0,'<Month text> <Year4>');
 // message(payMonthYear);
  hisVal.SETRANGE(empid,loan.empid);

  hisVal.SETRANGE(MonthYear,payMonthYear);
 // message(hisVal.MonthYear);
//IF NOT hisVal.FINDFirst THEN BEGIN

}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// Save the last ID of table History (I suppose it is unique)
history.RESET;
IF history.FINDLAST THEN
  LintHistory_ID := history.id;

loan.RESET;
//loan.SETCURRENTKEY("Freeze Loan",balance);
loan.SETRANGE("Freeze Loan",FALSE);
loan.SETFILTER(balance,'>0');
IF loan.FINDSET(TRUE,TRUE) THEN // TRUE,TRUE because you want to change the table
  REPEAT
    loan2 := loan; // if you change records inside a loop, you NEED a second variable in which you change the record
    op_bal := loan2.balance;
    cl_bal := loan2.balance - loan2.monthly_pay;
    paidoff := loan2.loan_amount - cl_bal;
    loan2.paidoffamount := paidoff;
    loan2.balance := cl_bal;
    loan2.MODIFY; // no need for 2 modifies of the same table

    //insert into loan history table.
    LintHistory_ID += 1;

    CLEAR(history);
    history.id := LintHistory_ID;
    history.op_balance := op_bal;
    history.cl_balance := cl_bal;
    history.ded_amount := loan2.monthly_pay;
    history.int_amount := loan2.monthly_int;
    history.loan_id := loan2.id;
    history.empid := loan2.empid;
    history."Run Date" := RunDate;
    history.MonthYear :=FORMAT(RunDate,0,'<Month text> <Year4>');
    history.INSERT(FALSE); // the record doesn't exist, so you need to INSERT it and not MODIFY it
  UNTIL loan.NEXT = 0;
//END ELSE BEGIN
//MESSAGE('Loan already Process for the Month of: '+'%1',payMonthYear);
//exit;
//CurrReport.SKIP;

//display result on a report
view.RUN;
//END;
END ELSE BEGIN
ERROR('This month loan has already been process');
END;

Comments

  • DaveTDaveT Member Posts: 1,039
    Hi

    which message are you expecting ?

    is it the
    ERROR('This month loan has already been process');

    Have you tried using the debugger?
    Dave Treanor

    Dynamics Nav Add-ons
    http://www.simplydynamics.ie/Addons.html
  • SavatageSavatage Member Posts: 7,142
    Are you using Key's - that's what key's do. They allow data that's unique.

    So you would use IF & GET if it GET's it then it already exists and your message telling you it already exists will pop-up.
    Would you like to overwrite (dialog) if yes then map your fields then end in (MODIFY) if no EXIT.

    If the get does not find a match - you will map your fields & use (INSERT)

    hope that make sence.
Sign In or Register to comment.