Replace entire field in script

Hi, I am definitely not a coder, so hoping I can find some help here. My goal is to search for specific text somewhere within a field, and if it’s found, then replace the entire field with a specific string. Specifically, if it finds “UPS” or “FedEx” anywhere within the field, replace the entire field with “Shipping & Delivery”.

I started with the Find and Replace script that I installed from the Marketplace. This script replaces the text you are searching for with alternative text within the string, So, if my input field has “UPS Store #12345”, it will produce “Shipping & Delivery Store 12345”, rather than setting the field to “Shipping & Delivery”.

I tried modifying this line: let newValue = originalValue.replace(findText, replaceText); to somehow replace the entire field but I just can’t get it to work. Any help would be appreciated!

You need a script with different logic from the example Find and Replace.

The Find and Replace example calculates the new value based on the replacement, then compares the string with the replacement with the original to determine if the record needs updating.

Instead, you need to simply test if the original value includes the string you are looking for. The actual replacement occurs much later in the script when updating the records.

if (originalValue.includes(findText)) {
    replacements.push({
            record,
            before: originalValue,
            after: replaceText,
    });
}

If you need more assistance than this snippet of code, decide if you want to learn to code, or if you just want working code.

If you want to learn to code, your goal is actually a very good beginning project. However, this particular script has a lot going just to show the preview to the user, which makes reading and understanding the code much harder for a beginner. Without the preview, the code would be much simpler. I find the code snippets in the “API reference” (versus the “Examples”) to be better training tools for people who do not have coding experience.

On the other hand, if you just want working code (and have budget), it will probably be easier to hire someone to write the code for you.

1 Like

Thanks for the code snippet, I will look at the API reference to learn more. I actually accomplished what I needed to using formula fields with IF, OR, and Find but I thought to give it a shot through a script as a learning exercise. I will review other code examples along with the API reference to learn as much as I can. Thanks again!

1 Like