One Click Package.XML Creation Using Excel

 Hi All,

Being working on large projects on Salesforce, We always require project metadata to be migrated from one environment to another environment at the end of our build. This metadata migration requires to log all the components created/edited during the lifespan of the project.

Most of us have experienced the tedious process of creating a package.xml manually. It always takes time to arrange all the components & prepare the very lengthy package.xml file.

There are several ways to to retrieve the metadata from the source org and deploy them to the destination org.

  1. Change Set
  2. ANT Migration Tool
  3. Salesforce Workbench
  4. Salesforce CLI

If the source and target orgs are connected, then it is easy for us to use change set to migrate the metadata. Actual problem comes where both the orgs are not connected, at that time we have to create a long Package.xml file and use that to retrieve the metadata from source org and deploy to the destination org.

Let’s explore an excel based simplified way to create the package.xml in a single click through Macro.

I have used a simple structure to make this process user friendly & easy.

Scenario

Let us take the following scenario. I am taking a very small example to avoid any confusion.

Let say we have created following metadata in our sandbox org.

1.    Custom Object

2.    Custom Field

3.    Visualforce Page

4.    Apex Class

5.    Custom Tab

We want all above mentioned metadata to be deployed to another sandbox.

Let us see how the package.xml will look like for migrating above mentioned metadata.

No alt text provided for this image

We have taken a simple example to start with and thus our package.xml looks really small and seems like it can be created manually, but consider a scenario where there are lots of components which needs to be migrated and it will take a lot time to prepare the package.xml manually by adding ‘<members> & <types>’ for each metadata type.

Excel as a base

Steps:

1.  Create blank excel file & save it. Make sure you put the extension as ‘.xlsm’. (This is mandatory otherwise your macro will not be saved & you have to again create a new macro when you reopen the excel file.)

2.  Rename Sheet1 to ‘CustomObject’ and add ‘Begin’ & ‘End’ text in first two rows in Column A.

No alt text provided for this image

Note: The Worksheet Name will work as a metadata type for our package.xml and a single WorkSheet can only contain the metadata which are of the same type mentioned in the WorkSheet name.

3.  Add a new row after begin & add the custom object API name that you want to migrate.

No alt text provided for this image

4.  Create a new Worksheet & rename it as ‘CustomField’

  •    Add Begin in the first row in column A
  • Add all your custom field API name that you want to migrate on separate line
  •    Add End at the end.
No alt text provided for this image

Note: Make sure you add the object API name before the field Name.

5. Create separate Worksheet for each metadata type you want to migrate. Put ‘Begin’ at the start of your metadata list & ‘End’ at the end of the list. In our case our excel file with all Worksheet will look like following:

No alt text provided for this image

Each Worksheet will have following entry in Column A for related types.

  • Begin in the first row.
  • List of metadata type member to migrate 1 on each line
  • End at the end of the list.

6.  Go to View >Macros and click on ‘Record Macro’.

No alt text provided for this image

7. Provide the basic details & click 'Ok'.

8. Go to the macro that we just created and click Edit. It will open a Visual Basic form with blank Subs.

9. Add Following code between Sub <NameOfYourMacro> () & End Sub and Save it.

Dim WB_Count As Integer

Dim I As Integer

Dim J As Integer

Dim VALUE As String

Dim TextFile As Integer

Dim FilePath As String

Dim ampPosition As Integer

Dim ampChar As Integer

 

 

WB_Count = ActiveWorkbook.Worksheets.Count

 

FilePath = Application.DefaultFilePath & "\Package.xml"

MsgBox FilePath

TextFile = FreeFile

Open FilePath For Output As TextFile

 

Print #TextFile, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & " encoding=" & Chr(34) & "UTF-8" & Chr(34); "?>"

Print #TextFile, "<Package xmlns=" & Chr(34) & "http://soap.sforce.com/2006/04/metadata" & Chr(34) & " > """

For I = 2 To WB_Count

            If ActiveWorkbook.Worksheets(I).Cells(1, 1).VALUE = "Begin" Then

                        Print #TextFile, vbTab & "<type>"

                        For J = 2 To ActiveWorkbook.Worksheets(I).Cells(Rows.Count, "A").End(xlUp).Row

                                    VALUE = ActiveWorkbook.Worksheets(I).Cells(J, 1).VALUE

                                    If VALUE <> "End" Then

                                                ampPosition = InStr(VALUE, "&")

                                                If ampPosition > 0 Then

                                                            VALUE = Replace(VALUE, "&", "&amp;")

                                                            Print #TextFile, vbTab & vbTab & "<members>" & VALUE & "</members>"

                                                End If

                                                If ampPosition = 0 Then

                                                            Print #TextFile, vbTab & vbTab & "<members>" & VALUE & "</members>"

                                                End If

                                    End If

                        Next J

                        Print #TextFile, vbTab & vbTab & "<name>" & ActiveWorkbook.Worksheets(I).Name & "</name>"

                        Print #TextFile, vbTab & "</type>"

            End If

Next I

Print #TextFile, vbTab & "<version>48.0</version>"

Print #TextFile, "</Package>"

 

Close #TextFile

MsgBox "I have created your file, Enjoy!!!"

10. Create a new Worksheet name it as ‘Action’ & put it at as the first worksheet. Add a shape with text ‘Generate'.

No alt text provided for this image

11. Right click on Shape & click Assign macro. Select the macro which we created & click Ok.

No alt text provided for this image

You are done with Macro Setup.

11.  Click on ‘Generate’ & it will create a Package.XML file for you. By Default, The Package.xml will be created in Document Folder.

No alt text provided for this image

12.  Open the Package.xml file & see the magic. Seems to be exact same as we used to create manually?

No alt text provided for this image

I hope this will save a lot of your time for metadata migration in Salesforce echo system.


Kindly also subscribe to my youtube channel SFDC Learning to get learning from video tutorials.

Thanks,

Mehul Parmar


Comments

Popular posts from this blog

Pricing Waterfall Model - Salesforce CPQ

Summary Variable - Salesforce CPQ

Percentage of Total Pricing - Salesforce