External Document No. on Sales Order Header (Nav 2018)

KwajahatKwajahat Member Posts: 10
Hi,

I would like to know how to write a simple piece of code where under sales order header there is an field called External Document No.; If for a customer A, I have already entered the external doc no. as Testing and post the order, that's fine since it is the first time entry with such a external document number. What if I select the same customer, create another sales order for him but use the same external document no. as Testing, I want Nav to validate that duplicate external document no and prompt a message for me to say that such document number was already used

I am a functional guy but since this request came from one of my customer I would like to know if this change can be done by me if anyone of you can guide me with proper steps of doing it. I hope to see some replies on this topic

Regards
Kwaja

Answers

  • PhoguePhogue Member Posts: 76
    edited 2020-01-02
    In "OnValidate" of External Document No. on Sales Header table:
    salesheader2: record of subtype sales header
    
    SalesHeader2.SETRANGE("Document Type", SalesHeader."Document Type"::Order);
    SalesHeader2.SETRANGE("Sell-to Customer No.", "Sell-to Customer No."); //from the current record
    SalesHeader2.SETRANGE("External Document No.", "External Document No."); //from the current record
    
    IF NOT SalesHeader2.ISEMPTY THEN
        Error('Sales order for customer X with external document number Y already exists');
    
  • KwajahatKwajahat Member Posts: 10
    Hi Phogue

    Thank you so much for the reply and also for the small code that you have given for me to write. I shall copy and paste it on the sales header table under the onvalidate of external doc no. and test it out

    Thanks

    Regards
    Kwaja
  • ShaiHuludShaiHulud Member Posts: 228
    First, you should make an exclusion when External Document No. is blank. Then, you need to make sure to exclude the current order itself in case some code validates the field without actually changing the value. Another suggestion from me would be to also look at Posted Sales Invoices. I think that should be done because one user may have already posted an order with for such Customer with such External Document No. and your check would ignore that. So your full code would become:
    IF "External Document No." = '' THEN 
      EXIT;
    SalesHeader2.SETRANGE("Document Type", SalesHeader."Document Type"::Order);
    SalesHeader2.SETRANGE("Sell-to Customer No.", "Sell-to Customer No."); //from the current record
    SalesHeader2.SETRANGE("External Document No.", "External Document No."); //from the current record
    SalesHeader2.SETFILTER("No.", '<>%1', "No."); //if somebody manages to VALIDATE "External Document No." without changing it, you would get an error due to the order itself
    
    IF NOT SalesHeader2.ISEMPTY THEN
        Error('Sales order for customer %1 with external document number %2 already exists', "Sell-to Customer No.", "External Document No.");
    
    SalesInvoiceHeader.SETRANGE("Sell-to Customer No.", "Sell-to Customer No."); //from the current record
    SalesInvoiceHeader.SETRANGE("External Document No.", "External Document No."); //from the current record
    IF NOT SalesInvoiceHeader.ISEMPTY THEN
        Error('Sales order for customer %1 with external document number %2 has already been posted', "Sell-to Customer No.", "External Document No.");
    

Sign In or Register to comment.