Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to I remove spaces and blank lines from a multiline text field?

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I have a custom workflow which is now working below that creates multiple records from a multi-line text field (line by line). 

Problem: People will type in telephone numbers into this field in a list however some people put in spaces into phone numbers.  I want to be able to remove any spaces in these phone numbers. 

Another problem is that people put blank lines in the text field and this blank line will create a new record in the said custom workflow below.

I need to know how to manipulate the code below so that the mutliline text field data is cleansed before it creates the records.

So for example:

08888 888888                   
09888 888888
<Blank Line>
09988 888 888

<Blank Line>

<Blank Line>

<Blank Line>

Becomes

08888888888                   
09888888888
09988888888<<<<<<Array/Multiline is trimmed at this point.

Code I have used so far for the custom workflow is:

using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Activities;

using Microsoft.Xrm.Sdk.Workflow;

using Microsoft.Xrm.Sdk;

using System.Text.RegularExpressions;

namespace CreateLineRecords

{

public class CreateRecords : CodeActivity

{

[Input("Phone Numbers")]

[RequiredArgument]

public InArgument<string> LinesString { get; set; }

[Input("Billing Account")]

[RequiredArgument]

[ReferenceTarget("account")]

public InArgument<EntityReference> Accountfromorder { get; set; }

[Input("Order")]

[RequiredArgument]

[ReferenceTarget("salesorder")]

public InArgument<EntityReference> Orderfromorder { get; set; }

protected override void Execute(CodeActivityContext context)

{

IWorkflowContext workflowcontext = context.GetExtension<IWorkflowContext>();

IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();

IOrganizationService service = serviceFactory.CreateOrganizationService(workflowcontext.InitiatingUserId);

var lines = LinesString.Get<string>(context).Split('\n').ToList<string>(); ;

foreach (var line in lines)

{

var lineEntity = new Entity("rp_line");

var accountforline = Accountfromorder.Get<EntityReference>(context);

var orderforline = Orderfromorder.Get<EntityReference>(context);

lineEntity["rp_cli"] = line;

lineEntity["new_billingaccount"] = accountforline;

lineEntity["new_order"] = orderforline;

service.Create(lineEntity);

}

}

}

}

*This post is locked for comments

  • Colin365Bell Profile Picture
    Colin365Bell 40 on at
    RE: How to I remove spaces and blank lines from a multiline text field?

    Hi Ryan,

    Could you help me with this at all?

    I'm trying to replace spaces with a dash i.e. "Test Account" as "Test-Account"

    It allows me to replace characters but not Spaces?

    Any Ideas where Im going wrong? here is the screen shot

    0044.Capture.JPG

    UPDATE: I see my mistake please disregard, thank you

    TIA

    Colin

  • bpr_admin Profile Picture
    bpr_admin on at
    RE: How to I remove spaces and blank lines from a multiline text field?

    Hi Ryan, I am unable to remove blank lines with the regex replace utility. Do you have any insight to the issue? I don't need to remove spaces, just blank lines and blank lines that might have spaces. Here's the RegEx that I am using. No matter what I still have the first blank line.

    (^(\r\n|\n|\r)$)|(^(\r\n|\n|\r))|^[ \t\r\n]*$|^\s*$/gm

    composite.png composite.png

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to I remove spaces and blank lines from a multiline text field?

    Hi John,

    if you solved the problem thanks to an answer, please mark the response as verified to encourage the community to provide more and more a better support. Thank you. Francesco

    If you found the answer helpful, please mark as Verified 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to I remove spaces and blank lines from a multiline text field?

    Hi John,

    have you solved the problem?

    Please let us know.

    If you found the answer helpful, please mark as Verified 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Suggested answer
    Ryan Maclean Profile Picture
    Ryan Maclean 3,070 on at
    RE: How to I remove spaces and blank lines from a multiline text field?

    Hi John,

    You could use the RegEx Replace step:

    8880.Capture.png

    I've done a brief experiment with this and managed to remove all of the excess spaces and/or all newlines in a given expression

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to I remove spaces and blank lines from a multiline text field?

    Put before lineEntity["rp_cli"] = line; the following code:

    line = Regex.Replace(line, @"\s+", "");

    If you need to remove also tab and newline just add "\n\r\t";

    Hope it helps!

    If you found the answer helpful, please mark as Verified 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Verified answer
    Ivan Ficko Profile Picture
    Ivan Ficko 1,380 on at
    RE: How to I remove spaces and blank lines from a multiline text field?

    Replace line

    lineEntity["rp_cli"] = line;

    with

    lineEntity["rp_cli"] = line.Replace(" ", string.Empty);


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to I remove spaces and blank lines from a multiline text field?

    I did look at this Ryan but you can't replace spaces with it :( - Well I couldn't see a way you could.  It seams to allows you to add spaces though.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to I remove spaces and blank lines from a multiline text field?

    That worked a treat for getting rid of blank lines but it doesn't work to remove spaces in the phone numbers.

    Also if you have a space on a blank line it doesn't get rid of the line and it still creates a blank record

  • Suggested answer
    Ivan Ficko Profile Picture
    Ivan Ficko 1,380 on at
    RE: How to I remove spaces and blank lines from a multiline text field?

    You can just add .Where(x => x != "") to the split line.

    var lines = LinesString.Get<string>(context).Split('\n').ToList<string>().Where(x => x != "");


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans