Announcements
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
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
UPDATE: I see my mistake please disregard, thank you
TIA
Colin
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
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
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
Hi John,
You could use the RegEx Replace step:
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
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
Replace line
lineEntity["rp_cli"] = line;
with
lineEntity["rp_cli"] = line.Replace(" ", string.Empty);
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.
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
You can just add .Where(x => x != "") to the split line.
var lines = LinesString.Get<string>(context).Split('\n').ToList<string>().Where(x => x != "");
André Arnaud de Cal... 291,359 Super User 2024 Season 2
Martin Dráb 230,370 Most Valuable Professional
nmaenpaa 101,156