Insert record if not exists, NAVISION 2009

Hi,

I have a xml file that you can insert with a xml port.

and I want to insert record if the record doesnt exists. I have a check box for Digital email facurering. If the checkbox with the name:

“”.“Digital shipment”

is checked(true) then a new recod has to be inserted with the name: 'Crediteuren administratie.

I have it now like this:

BussinesContactName.SETFILTER(BussinesContactName.“No.”,"".“No.” );

IF BussinesContactName.FINDFIRST THEN;

“”.“Country/Region Code” := ‘NL’;

“”.“Fax No.” := Rec.“Fax No.”;

//loops through all the contacts connected with a company contact

WHILE (LoopCount <> 0) DO

BEGIN

KlantContact.GET(FirstContact);

KlantContact.VALIDATE(KlantContact.“Company No.”, BussinesContactName.“Contact No.”);

IF KlantContact.Website = TRUE THEN

KlantContact.“E-Mail Verification” := 1

ELSE

KlantContact.“E-Mail Verification” := 0;

IF “”.“Digital shipment” = TRUE THEN BEGIN

//MESSAGE(‘ja’);

KlantContact.INIT;

KlantContact.Type := 1;

KlantContact.“No.” := ‘1123’;

KlantContact.Name := ‘Crediteuren administratie’;

//MESSAGE(KlantContact.“No.”);

// END;

KlantContact.VALIDATE(KlantContact.“Company No.”, BussinesContactName.“Contact No.”);

// KlantContact.INSERT;

IF “”.“Digital shipment” = FALSE THEN

MESSAGE(‘Nee’);

END;

KlantContact.MODIFY;

LoopCount := LoopCount - 1;

FirstContact := INCSTR(FirstContact);

END;

But

The xml looks like this:

<?xml version="1.0" encoding="utf-8"?>

sdfsdf

sdfsdf

sdfsdf

123456

sdfsd

nengelen@online.nl

1234567890

Eenmanszaak

12345678

dfgdfgdfg

niet_verplicht

niet_verplicht

dfgdfgdfgdfg

dfgdfgdfgdfg

kontant

nee

Ja

nengelen@online.nl

Niels

Engelen

nengelen@onlie.nl

1234567890

dfgdfg

dfgdfg

Ja

Ja

Ja

Ja

Ja

Ja

Ja

But the problem is now that the contactperson: Niels will not be inserted. ONly the new record: 'Crediteuren administratie

will be inserted.

Thank you

Niels,

Too much lines in your issue to get a good overview. And your code is quite challenging, not to say atypical NAV and soemwhat messy with all the out-commented lines (not in the least the INSERT statement even though you describe at the start of your issue that you want to insert a record). BTW: see also my remarks on one of your other posts: http://dynamicsuser.net/forums/p/88901/476670.aspx#476670).

Let me only pose questions here that pop-up just looking at your code. This might clarify what your are trying to achieve and at the same show you why it’s not simple to reply to your issue (with too much lines and unclean code).

  1. Line 2: Why are you retrieving the first BussinesContactName record? You are not using it except for it’s “Contact No.” field, which value you already know because (see SETFILTER statement) it’s equal to Customer.“No.”, so you can use the latter instead
  2. Lines 3 & 4: Why are you populating these two fields of the Customer record when you do not use the Customer record at all further up in the code?
  3. Line 6: looping through records using a WHILE construction? This is typically not the way we do that in NAV. But maybe you do something atypical which needs this, but I dare to argue that.
    NAV standard is to use the REPEAT-UNTIL NEXT construction, that follows a FIND(’-’) or FINDSET statement. There are zillion of examples of that in standard NAV code.
  4. Line 8: KlantContact.GET. This is part of your loop, I guess. As said atypical and it seems also very inefficient as it looks your might be looping through KlantContact records, which I dare to say that is not needed. What KlantContact records do you actually need? Have you asked yourself that question?
    With a database application like NAV, you always strive to read as little as possible records to make it work efficient
  5. Line 9: Why are you changing the field “Company No.” on each KlantContact record you find to exactly the same value (you only retrieved the BussinesContactName record once so BussinesContactName.“Contact No.” is always the same after Line 2)?
    This potentially results in all KlantContact records to be associated with the same company.
  6. Last but one line: Are you sure that all KlantContact records have a primary key (i.e. “No.”) subsequently ordered without gaps? If this is not the case your GET statement (line 8) will break somewhere as it will not find the next logical record.

As you might understand (and what I already wrote on the other post), “Niels, it seems you are trying to do your best, but, if I may say so, you have to still learn a lot about C/AL programming”.

But keep on trying. And do yourself a favor an try to join some formal classes. NAV data is to precious to trial and error.