1

I am migrating my changes from one org to another. But when I have retrieved an XML file containing custom fields I wants to remove some fields from that file as I would like to remove it from org as well. So for example:-

Lets say I have following paragraph in XML files and I want to remove this paragraph from other files as well but unfortunately the tag table is not constant it's changing in differwnt files which is the problem. Can anyone please suggest me how I can do this.

<columns>
<checkbydefault>
<field>demo__c</field>
<table>case.contact</table>
</checkbydefault>
</columns>

enter image description here

  • When you say other files, which files are you referring to? – Jayant Das May 9 '18 at 12:35
  • 4
    Removing a custom field from component source XML doesn't remove the custom field from a target org if it already exists there. You need to use destructiveChanges.xml to remove existing components. – David Reed May 9 '18 at 12:50
1

Try using sed. I had a similar need specifically to remove the following section from Admin.profile-meta.xml every time I retrieved source from one of our shared sandboxes.

<userPermissions>
    <enabled>true</enabled>
    <name>ManageSandboxes</name>
</userPermissions>

To do this I put the following sed command in a Bash script, and it works on macOS High Sierra 10.13.6 (17G3025).

# clean.sh scrubs the metadata to make it shiny and friendly

# Get rid of the ManageSandboxes user permission which shows up
# in metadata retrieved from Staging but is not compatible with Developer
# sandboxes
sed -E -i "" -e '/<userPermissions>/{

    # Expect the enabled element
    N

    # Expect the name element
    N

    # Expect the closing userPermissions tag
    N

    # Flag the ManageSandboxes permission
    s/>ManageSandboxes</>ManageSandboxes!</

    # Skip printing these lines to effectively
    # delete the unwanted metadata
    t found

    # Print to preserve the pattern space
    # for all other metadata to keep
    p

    :found

    # Delete the pattern space and move on
    d
}' force-app/main/default/profiles/Admin.profile-meta.xml

A Salesforce DX plugin to handle this would be nice, but as I was poking around with creating a plugin and toying with a configurable .justxml config file similar in concept to .forceignore, going that route would've taken me more time than was available due to lack of familiarity with TypeScript.

1

I've recently started using a Python script for doing these kinds of XML manipulations, and I haven't been looking back ever since! With the appropriate dependencies (ie. Python + lxml module), I can run the script as part of a Bitbucket Pipeline or could probably pretty easily run it in a CLI plugin (in a bash window, I just run the following command: python build/deploy.py).

Here's what it looks like in Python using Marty's example:

from lxml import etree as ET
import os, shutil
import sys

NS = {"sf": "http://soap.sforce.com/2006/04/metadata"}
parser = ET.XMLParser(remove_blank_text=True)

# Loop through all files in profiles folder
for filename in os.listdir("./src/profiles"):

    # Parse XML file
    fxml = ET.parse("./src/profiles/" + filename, parser)

    # Look for any "name" node with text value "ManageSandboxes" 
    for elem in fxml.xpath("//sf:name[text()='ManageSandboxes']/.." , namespaces=NS):

        # Remove its parent
        elem.getparent().remove(elem)

    # Write back to the XML file
    fxml.write("./src/profiles/" + filename, pretty_print=True, encoding='UTF-8', xml_declaration=True)

    continue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.