web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Ryan Maclean Profile Picture
    3,070 on at

    If you don't want to use code, you could try using Jason Lattimer's String Workflow Utilities to modify the field via a workflow - github.com/.../CRM-String-Workflow-Utilities

  • Suggested answer
    Ivan Ficko Profile Picture
    1,380 on at

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

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


  • Community Member Profile Picture
    on at

    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

  • Community Member Profile Picture
    on at

    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.

  • Verified answer
    Ivan Ficko Profile Picture
    1,380 on at

    Replace line

    lineEntity["rp_cli"] = line;

    with

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


  • Suggested answer
    Community Member Profile Picture
    on at

    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

  • Suggested answer
    Ryan Maclean Profile Picture
    3,070 on at

    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

  • Community Member Profile Picture
    on at

    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

  • Community Member Profile Picture
    on at

    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

  • bpr_admin Profile Picture
    on at

    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

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

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans