Jump to content
Server Maintenance This Week. ×

FieldSubstitute() Custom Function


This topic is 7172 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Here is an updated version of a custom function that replaces <<merge fields>> in a text field with the field data.

Example, if the user entered text field has this (Note the mispelling of the company field):

<<Date>>

Dear <<Name>>,

As a result of recent discussions with our team, we have decided to terminate our contract with <<companie>>.

Thank you for your business.

The result text will be this:

1/5/2004

Dear Greg Anderson,

As a result of recent discussions with our team, we have decided to terminate our contract with <Field Missing: "companie">.

Thank you for your business.

Here is the custom function code:


//FieldSubstitute(text)

//By Mike Hackett

//8-22-2004



//This takes a text field with user entered merge fields in the form <<Field Name>> 

//and replaces the merge fields with the field values.



Let(

[

   l = Length(text);

   p1 = Position(text;"<<";1;1);

   p2 = Position(text;">>";p1;1);

   fieldText = Middle(text;p1+2;p2-(p1+2))

];

If(p1 * p2; //If both sets of brackets exist

   Left(text;p1-1) & 

   If(IsValid(GetField(fieldText)); 

      GetField(fieldText); //field exists, get it's value

      "<Field Missing: "" & fieldText & "">") & FieldSubstitute(Right(text;l-(p2+1))); //field does not exist

   text//no matching brackets

)//if

)//let

This could be used where you want users to make letters using fields without having to come to you to modify the layouts.

Link to comment
Share on other sites

Woh. What a great idea.

Here is the revised function that can evaluate inline functions too.


//FieldSubstitute(text)

//By Mike Hackett

//8-23-2004



//This takes a text field with user entered merge fields in the form <<Field Name>> 

//and replaces the merge field names with the field values.  It also evaluates inline functions



Let(

[

   l = Length(text);

   p1 = Position(text;"<<";1;1);

   p2 = Position(text;">>";p1;1);

   fieldText = Middle(text;p1+2;p2-(p1+2))

];

If(p1 * p2; //If both sets of brackets exist

   Left(text;p1-1) & 

   If(IsValid(Evaluate(fieldText));

          Evaluate(fieldText);//Evaluate inline function

          "<Field Missing: "" & fieldText & "">") & //field does not exist

   FieldSubstitute(Right(text;l-(p2+1))); 

   text//no matching brackets

)//if

)//let

Though I'm not sure how comfortable users would be writing functions, it's pretty neat that this can be done. Thanks Shaun!

Link to comment
Share on other sites

  • 3 weeks later...

This topic is 7172 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.