String Replace function

Sep 2, 2010 at 10:53 AM

Hi there,

I'm just tring to replace a substring from one of the attributes but without success.

I need to know how to use this Replace function.

In my workflow, I'm updating the Contact entity's attribute (baseURL) by appending the differeing values in it which its own working fine. But what I want is to repalce the spaces with %20 as the link in email does not render properly if there are spaces.

So I have added the next step to repalce the substring.

In the first Input Field I have added the dynamci field {baseURL{Contact}}

In the second parameter (old Value) I have put the space by pressing space bar once.

In the 3rd parameter (new value) i have added the %20.

but when I run this workflow against a record, it does not update the baseURL string.

Anybody knows' where I'm doing wrong??

 

Coordinator
Sep 2, 2010 at 6:55 PM

I don't think you are doing anything wrong.  Because it is just a space, it may get lost in the HTML translation.  I wrote a new code block for doing regular expression replacements which would allow you to do exactly what your looking for.  I am planning on including it in the CRM 2011 version when it is ready.  I wrote the file for CRM 4.0, if you can download it, add it to the RegEx folder in the solution and recompile you can use it.


// ==================================================================================
//  Project:	Manipulation Library for Microsoft Dynamics CRM 4.0
//  File:		RegExReplace.cs
// ==================================================================================
using System;
using System.Text.RegularExpressions;
using System.Workflow.ComponentModel;
using System.Workflow.Activities;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Workflow;

namespace ManipulationLibrary.RegEx
{
    [CrmWorkflowActivity("RegEx Replace", "RegEx Utilities")]
    public partial class RegExReplace : SequenceActivity
    {
        public RegExReplace()
        {
            InitializeComponent();
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Invalid = new CrmBoolean { Value = false };
            Found = new CrmBoolean { Value = false };
            var result = Text;

            try
            {
                var regex = new Regex(Pattern);
                result = regex.Replace(Text, Replacement);
            }
            catch (ArgumentException)
            {
                Invalid.Value = true;
                // Syntax error in the regular expression
            }

            Result = result;
            return base.Execute(executionContext);
        }

        public static DependencyProperty InvalidProperty = DependencyProperty.Register("Invalid", typeof(CrmBoolean), typeof(RegExReplace));
        [CrmOutput("Invalid Regular Expression")]
        [CrmDefault("False")]
        public CrmBoolean Invalid
        {
            get { return (CrmBoolean)GetValue(InvalidProperty); }
            set { SetValue(InvalidProperty, value); }
        }

        public static DependencyProperty ResultPropertyFoundProperty = DependencyProperty.Register("Result", typeof(String), typeof(RegExReplace));
        [CrmOutput("Result")]
        public String Result
        {
            get { return (CrmBoolean)GetValue(ResultProperty); }
            set { SetValue(ResultProperty, value); }
        }

        public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(RegExReplace));
        [CrmInput("Text")]
        public String Text
        {
            get { return (String)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static DependencyProperty PatternProperty = DependencyProperty.Register("Pattern", typeof(String), typeof(RegExReplace));
        [CrmInput("Regular Expression Pattern")]
        public String Pattern
        {
            get { return (String)GetValue(PatternProperty); }
            set { SetValue(PatternProperty, value); }
        }

        public static DependencyProperty ReplacementProperty = DependencyProperty.Register("Text", typeof(String), typeof(RegExReplace));
        [CrmInput("Replacement")]
        public String Replacement
        {
            get { return (String)GetValue(ReplacementProperty); }
            set { SetValue(ReplacementProperty, value); }
        }
    }
}

You can then use the pattern /s and replace it with %20 and I think that will work.  
Another option would be to actually URL encode the string. Maybe I'll work on adding that if this doesn't work.  Let me know.  -Carlton
Feb 19, 2011 at 8:03 PM

Good afternoon.

I'm trying to use the replace funcation as well (CRM 4.0).  Essentially I'm trying make sure I strip out any commas  that may be keyed into a particular field.  I've populated the Text field with the name of my field {application id{Exam}}

The Old value has a comma in it ie ,

The New Value is empty.  When I run the workflow, nothing happens.  For testing I changed it so that I was looking for the following string NO and replaced it with YES (just to try and get the function to work.  But still no luck.  The field isnt' updated when I run the workflow (which I set to run on demand).  Am I missing something on the syntex or how I'm trying to use this?  Is there another tool in the library that would work better? 

Thank You,  Jon

Coordinator
Apr 11, 2011 at 5:56 PM

The current code I just updated will now change null values for the new text to an empty string, but it will only work with CRM 2011.  If you add a line in the Replace to check for null and set the new text value to string.empty, then it should work the way it was intended.