Wednesday, August 17, 2011

Dynamics AX 2009 - removing new line character from string (note/memo/text) fields

So this is my first post on this dynamics AX blog of mine. I'm going to use this to keep track of the little programming solutions I develop. Maybe it will help someone else too. Who knows?

My customer was having problems with a report showing the delivery address in excel. When a new line character is reached, it moves to another line. This made the excel format untenable for our 3rd party vendor that needed the data. So I wrote this code on the report to strip the new line character out and replace it with a space.

display str DeliveryStreet()
{
TextBuffer txt = new TextBuffer();
;

txt.setText(dataSource.DeliveryStreet);
txt.replace('\n',' ');
dataSource.DeliveryStreet = txt.getText();

return(dataSource.DeliveryStreet);
}

The DeliveryStreet field on the report calls this method instead of calling the field on the datasource.

16 comments:

  1. Thanks! I really need this :)

    ReplyDelete
  2. Thanks a lot this help me when transfer address in to csv File

    ReplyDelete
    Replies
    1. Yes, we had the same problem. Sometimes it's something small that causes you a lot of trouble. Good luck!

      Delete
  3. You could use the function strRem.

    strRem(dataSource.DeliveryStreet, '\n')

    ReplyDelete
    Replies
    1. Yes, that will work to remove the new line character, but if you want to replace the new line character with a space, you can use my code above. If you don't replace it with a space, you could end up with words concatenated when you don't wish them to be.

      Delete
  4. HI amber,
    thanks your blog helped me

    ReplyDelete
  5. Thanks.This is Himanshu Sinha from India. I need to append newline character to text buffer, please give me some suggestion.
    Thanks in advance

    ReplyDelete
    Replies
    1. You should be able to do something simple like:
      NewText = text + '\n';

      Delete
  6. Thanks it helped me

    ReplyDelete
  7. Wow! Totally fixed a problem I was having passing a web template. Thanks!

    ReplyDelete