validating "Sell-to Customer No." after field dea

viljar_pviljar_p Member Posts: 7
There's a need to give a default value to "Sell-to Customer No." field on Quotes form. As I didn't want to save it to the Sales Header record, I created a new variable SellToCustNo and gave it my desired default value.

Now, I'd like to validate "Sell-to Customer No." when I leave the the SellToCustNo field. I wrote the code to the onDeactivate trigger

SellToCustNo - OnDeactivate()
IF SellToCustNo <> "Sell-to Customer No." THEN
BEGIN
VALIDATE("Sell-to Customer No.",SellToCustNo);
END;

... the validation stops on DocDim.LOCKTABLE; with the error "you cannot make any changes in the database until a transaction has been started"

...is there any chance to make it work? :-k

Comments

  • krikikriki Member, Moderator Posts: 9,110
    I think it is best to do a validate of the field in the OnInsert-trigger of the table.
    Remember to do this only IF the document type is Quote and the field is blank.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • viljar_pviljar_p Member Posts: 7
    If I would validate the "Sell-to Customer No." on insert then I would be questioned "do you want to change the Customer No." later on when I choose a different customer. That's why I want to use my own variable on the form.
  • SavatageSavatage Member Posts: 7,142
    How about something simple like this on the Quote form:
    OnInsertRecord(BelowxRec : Boolean) : Boolean
    IF CONFIRM('Would you like to use default customer no?',TRUE)
    THEN VALIDATE("Sell-to Customer No.",'YourCustNoHere');

    if you say yes, the default value is filled and validated - if you say no - it will let you enter a different number
  • viljar_pviljar_p Member Posts: 7
    Thanks, Savatage, it could be one solution. Or I can make a button to enter the default value.

    Do you know what is wrong with the OnDeactivate() trigger, that I can't use it?
  • SavatageSavatage Member Posts: 7,142
    viljar_p wrote:
    Or I can make a button to enter the default value.

    Add a command button
    OnPush()
    VALIDATE("Sell-to Customer No.",'YourCustNoHere');
    

    Is this what you mean by adding a button?

    ps : whats the obession with the deactivate trigger? :-k
  • SavatageSavatage Member Posts: 7,142
    Anyway as Kriki said it's always better to put the code on the table.

    this is how I would do it.
    1)Create A new field in the sales setup table called "Quote Default Cust No" type code size 20.
    2)add it to my sales setup form and fill it in with a value.
    3) go to the sales header table and add on the Sell-to Customer No. - OnValidate() trigger (the end)
    SalesSetup.GET;
    IF ("Document Type" =  "Document Type"::Quote) AND ("Sell-to Customer No." <> SalesSetup."Quote Default Cust No")
     THEN BEGIN
      IF CONFIRM('Would you like to use default customer no?',TRUE)
      THEN VALIDATE("Sell-to Customer No.",SalesSetup."Quote Default Cust No");
    END;
    

    Now this lets you ENTER a customer number on the quote form. If it matches the default number then you just go on about your business.
    If the number entered is different it will ask you if you want to use the default. if you say no it will use the number you entered. if you say yes the cust no is changed to the default - which is set in the sales setup table. ( I add it to the setup table so the field is accessible for changes in the future without the need for code changes and hardcoding)

    Raise the fist of the metal child! AHH!
  • ara3nara3n Member Posts: 9,256
    I want to add that you should be careful about adding the code especially on oninsert. The reason would CreditWarning and running into RUNMODAL ERROR.
    Adding it to the form would be safer solution.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • SavatageSavatage Member Posts: 7,142
    Yes I tested it an the onvalidate trigger of the sell-to cust no is really the best way to go.

    Note if you do use the method of creating a deault no in the setup tabel don't forget to change the table relation property of the field to "Customer"
    that way your lookup works nice & neatly.
  • viljar_pviljar_p Member Posts: 7
    Savatage wrote:
    viljar_p wrote:
    Or I can make a button to enter the default value.

    Add a command button
    OnPush()
    VALIDATE("Sell-to Customer No.",'YourCustNoHere');
    

    Is this what you mean by adding a button?

    Yes, that's it
    Savatage wrote:
    ps : whats the obession with the deactivate trigger? :-k

    The idea is to force field validation when I move from the field, but it seems to be impossible. Let's say that I would like to make my own lookup code

    SellToCustNo - OnLookup(VAR Text : Text[1024];) : Boolean
    CLEAR(CustList);
    CustList.LOOKUPMODE := TRUE;
    IF Cust.GET(SellToCustNo) THEN
    CustList.SETRECORD(Cust);
    IF CustList.RUNMODAL = ACTION::LookupOK THEN 
     BEGIN
      CustList.GETRECORD(Cust);
      SellToCustNo := Cust."No.";
    //  VALIDATE("Sell-to Customer No.",SellToCustNo);
     END;
    

    ... when I puch lookup, select a customer and push OK, it will enter the value in SellToCustNo. So far it works like a standard, because it doesn't validate the field yet (it's commented out). Now when I move away from SellToCustNo field, it should start validate, but that doesn't happen and there's no way to make it happen.
  • viljar_pviljar_p Member Posts: 7
    Savatage wrote:
    Yes I tested it an the onvalidate trigger of the sell-to cust no is really the best way to go.

    What if I want to make an automated import in Sales Header table later on? Then it would ask to enter my default value in every record. To prevent this, I should change the code:
    SalesSetup.GET;
    IF (CurrFieldNo = 2) AND ("Document Type" =  "Document Type"::Quote) AND ("Sell-to Customer No." <> SalesSetup."Quote Default Cust No")
     THEN BEGIN
      IF CONFIRM('Would you like to use default customer no?',TRUE)
      THEN VALIDATE("Sell-to Customer No.",SalesSetup."Quote Default Cust No");
    END;
    
  • viljar_pviljar_p Member Posts: 7
    Finally, I saved my default Customer from the Form in the Rec and turned the ValidationDialog off before Customer changing:
    SellToCustNo - OnAfterValidate()
    SetHideValidationDialog(TRUE);
    VALIDATE("Sell-to Customer No.",SellToCustNo);
    

    ...I still had to use my custom field SellToCustNo in the form to SetHideValidationDialog TRUE.

    Thank you for your advises!
  • SavatageSavatage Member Posts: 7,142
    viljar_p wrote:
    What if I want to make an automated import in Sales Header table later on? Then it would ask to enter my default value in every record.

    There are many ways of handling this. For example we have many ways of entering orders..
    Manually, Dataport, EDI, XMLport, Internet, etc.

    We've simply created a option field that "marks" each order as one or the other.

    You could use something like this and add it to the code to bypass certain critera or only have it fire when "Entry Type" = Manual.

    or Perhaps, using the setup table again create a "Use Quote Default" boolean on the setup table.
    then simply change the code to
    If "Use Quote Default" then begin... to wrap the code.
    On the dataport to import orders, the first line would be to get the current value of"Use Quote Default" Save the value in a variable. Clear the "Use Quote Default" boolean. fill the dtaport fields. restore the variable back to the "use Quote Default"

    IN other words if you want that code not to effect imported orders.
    ->i was thinking clear it at the start of the dataport
    ->fill the header fields
    ->put the value back

    * I'm sure there are other suggestions too
Sign In or Register to comment.