Replacing Characters in a Dataport

ta5
ta5 Member Posts: 1,164
Hi
I want to replace some characters in a dataport. The field delimiters are ", but this character also occurs in the field values itself. Needless to say, this causes an error then. Changing the import file is not an option.

So my idea is, OnPreDataPort to loop sequential through the File, save it and go on with the Dataport. Anyway, it should be in the same step as the dataport itself and should be transparent to the user.
Is this possible and/or somebody has a better idea? Maybe edit the line on the trigger OnBeforeImportRecord?

Best regards
Thomas

Answers

  • kriki
    kriki Member, Moderator Posts: 9,135
    Put field delimiters to <None>.
    Import the fields in global variables.
    In the OnAfterImportRecord:
    txtVariable := DELCHR(txtVariable,'<>','"');
    
    This deletes the " in the beginning and the end of the string.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • David_Cox
    David_Cox Member Posts: 509
    I would use a codeunit and do the following, rebuild the file by reading it into a varaible and writing it back to a new file then run the dataport on the clean file.
    You will need to use the combination of the 3 chars, then remove the invalid , and " from the selected string (Field)
    This only works if the file has comma's and text delimiters ","
    This is from memory so not tested and may need adjustment

    CLEAR(OldFile);
    OldFile.TEXTMODE := TRUE;
    OldFile.OPEN(FileName);
    WHILE OldFile.POS <> OldFile.LEN DO BEGIN
    //Read the line into a variable LineDetail
    OldFile.READ(LineDetail);
    CLEAR(TempDetail);
    //Use STRPOS to find your string ","
    WHILE STRPOS(LineDetail,'","') > 0 BEGIN
    CLEAR(TempStr);
    Position := STRPOS(LineDetail,'","');
    //Cut the Field from the LineDetail
    TempStr := COPYSTR(LineDetail,1,Position-1);
    //Trim the lineDetail
    LineDetail := COPYSTR(LineDetail,Position+3);
    //Remove Invalid text " and ,
    TempStr := DELCHR(TempStr,'=','"');
    TempStr := DELCHR(TempStr,'=',',');
    //Build the New Line
    IF TempDetail <> '' THEN
    TempDetail := TempDetail +',' +TempStr
    ELSE
    TempDetail := TempStr;
    END;

    //We still may have some text to add and the comma for the last field
    //Remove Invalid text " and ,
    LineDetail := DELCHR(LineDetail,'=','"');
    LineDetail := DELCHR(LineDetail,'=',',');
    //Build the New Line
    IF TempDetail <> '' THEN
    TempDetail := TempDetail +',' LineDetail
    ELSE
    TempDetail := LineDetail;
    NewFile.WRITE(TempDetail);
    END;

    Close your two Files and run your dataport
    So the string "This string is 3" long, I think","Nah it ain,t"
    Will become This string is 3 long I think,Nah it aint

    Phew think that covers it! 8)
    Analyst Developer with over 17 years Navision, Contract Status - Busy
    Mobile: +44(0)7854 842801
    Email: david.cox@adeptris.com
    Twitter: https://twitter.com/Adeptris
    Website: http://www.adeptris.com
  • ta5
    ta5 Member Posts: 1,164
    Thanks for your thoughts on this:

    @David: I'll give it a try, but I actually still hope to do it in an easier workaround :)
    @Alain: Cool stuff, but I now have the problem with Text fields containing a comma. Any cure for this? ](*,)

    Thanks in advance
    Thomas
  • David_Cox
    David_Cox Member Posts: 509
    I have not used INSTREAM, but you might be able to open the file then use CONVERTSTR(MyStream,",","~");

    This should convert from comma seperated "," to tilde "~". then just change the seperator in the dataport.

    Not sure how to code it, without doing it, so its just an Idea!

    The is a common problem so a solution would be worth posting to Tips and Tricks :wink:
    Analyst Developer with over 17 years Navision, Contract Status - Busy
    Mobile: +44(0)7854 842801
    Email: david.cox@adeptris.com
    Twitter: https://twitter.com/Adeptris
    Website: http://www.adeptris.com
  • Captain_DX4
    Captain_DX4 Member Posts: 230
    This may be a silly question, but could you consider or are you able to switch to fixed length?
    Kristopher Webb
    Microsoft Dynamics NAV Developer
  • David_Cox
    David_Cox Member Posts: 509
    No simple solution if you cannot get the file delimeter changed, and you say that is not an option.

    So is the file text and comma delimited?
    "Mr","Fred Flintstone","Cave 1,The Mound","Bedrock","$1,200.00"
    Use CHANGESTR replace the "," with a Tilde ~ and then remove the " and ,

    Or is it only text qualifed when there is a quote or comma in the string?
    Mr,Fred Flintstone,"Cave 1,The Mound",Bedrock,"$1,200.00"
    Use STRPOS this ," to find the start point and ", the end and then remove the , or the " and rebuild.
    Analyst Developer with over 17 years Navision, Contract Status - Busy
    Mobile: +44(0)7854 842801
    Email: david.cox@adeptris.com
    Twitter: https://twitter.com/Adeptris
    Website: http://www.adeptris.com
  • ta5
    ta5 Member Posts: 1,164
    I've managed it now like this.

    OnPreDataport: Replace all " in the file with another special character, except of the " in the middle of a field. Actually as David suggested. Store in a new file.
    Then use the new file in the dataport (Currdataport.file...). :D

    Thank you for your help and ideas!

    Thomas