function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Semira@gmail.comSemira@gmail.com 

backup custom object before sandbox refresh

Hi, 

I was just wondering, can you backup custom object, it's field and security setting before doing a refresh? I just lost a custom object I was working on in Fullcopy sandbox. Without realizing it's not in production, I did a refresh and lost all my work. 

I know you can backup data means each records of the object. However, I haven't seen any post regarding the object backup. 
Best Answer chosen by Semira@gmail.com
srlawr uksrlawr uk
If you mean, before you next do a refresh, is there any way to "back up" any work first (ie. objects in Sandbox but not production) what you want to be looking at is the METADATA API. Metadata is the code that defines most of the structure and configuration in Salesforce, and you can download it from an org over the API, it's a big collection of XML files.

The easiest way to explain it would be to suggest getting the force.com IDE, in which you can create a "New Force.com Project" and select to download the metadata for all your standard and custom objects (by default I think you also get classes, pages and triggers). How to do this is all well documented on the internet.

Once you have this lark downloaded to your machine, go and find the object file for the custom object you want to preserve, it will look something like this, but much longer (as there is a lot of data even in the simplest objects)

<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <actionOverrides>
        <actionName>Delete</actionName>
        <type>Default</type>
    </actionOverrides>
    <actionOverrides>
        <actionName>Edit</actionName>
        <type>Default</type>
    </actionOverrides>
    <actionOverrides>
        <actionName>View</actionName>
        <type>Default</type>
    </actionOverrides>
    <deploymentStatus>Deployed</deploymentStatus>
    <enableActivities>false</enableActivities>
    <enableFeeds>false</enableFeeds>
    <enableHistory>false</enableHistory>
    <enableReports>true</enableReports>
    <fields>
        <fullName>Invoice__c</fullName>
        <externalId>false</externalId>
        <label>Invoice</label>
        <referenceTo>Invoice__c</referenceTo>
        <relationshipLabel>Line Items</relationshipLabel>
        <relationshipName>Line_Items</relationshipName>
        <relationshipOrder>0</relationshipOrder>
        <reparentableMasterDetail>false</reparentableMasterDetail>
        <type>MasterDetail</type>
        <writeRequiresMasterRead>false</writeRequiresMasterRead>
    </fields>
    <fields>
        <fullName>Line_Item_Total__c</fullName>
        <externalId>false</externalId>
        <formula>Unit_Price__c * Quantity__c</formula>
        <formulaTreatBlanksAs>BlankAsZero</formulaTreatBlanksAs>
        <label>Line Item Total</label>
        <precision>18</precision>
        <required>false</required>
        <scale>2</scale>
        <type>Currency</type>
    </fields>
    <label>Line Item</label>
    <nameField>
        <label>Line Item Number</label>
        <type>Text</type>
    </nameField>
    <pluralLabel>Line Items</pluralLabel>
    <searchLayouts/>
    <sharingModel>ControlledByParent</sharingModel>
    <validationRules>
        <fullName>Validate_Quantity</fullName>
        <active>true</active>
        <errorConditionFormula>(Quantity__c &lt;= 0)</errorConditionFormula>
        <errorDisplayField>Quantity__c</errorDisplayField>
        <errorMessage>You must order at least one item</errorMessage>
    </validationRules>
</CustomObject>


If you save that file away somewhere, and once you have done a Sandbox refresh (and then refreshed the project in Eclipse) if you copy those files BACK into the object folder, you will find, once correctly synced, that the custom object and all its fields and settings are now back and safe in your sandbox! Getting some more complicated permissions stuff like permissions sets takes another level of metadata scraping, but this should get you on the road to saving and restoring your custom objects.

Saving the meta data of an object and it's fields can be a good way to backup/version control them as well.

All Answers

Deepak RamaDeepak Rama
I am not sure, but you could try asking salesforce to rollback your sandbox (by giving them a reason as to why you need to do it). Ask helpdesk to roll it back (if it is less than 24 hours).
srlawr uksrlawr uk
If you mean, before you next do a refresh, is there any way to "back up" any work first (ie. objects in Sandbox but not production) what you want to be looking at is the METADATA API. Metadata is the code that defines most of the structure and configuration in Salesforce, and you can download it from an org over the API, it's a big collection of XML files.

The easiest way to explain it would be to suggest getting the force.com IDE, in which you can create a "New Force.com Project" and select to download the metadata for all your standard and custom objects (by default I think you also get classes, pages and triggers). How to do this is all well documented on the internet.

Once you have this lark downloaded to your machine, go and find the object file for the custom object you want to preserve, it will look something like this, but much longer (as there is a lot of data even in the simplest objects)

<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <actionOverrides>
        <actionName>Delete</actionName>
        <type>Default</type>
    </actionOverrides>
    <actionOverrides>
        <actionName>Edit</actionName>
        <type>Default</type>
    </actionOverrides>
    <actionOverrides>
        <actionName>View</actionName>
        <type>Default</type>
    </actionOverrides>
    <deploymentStatus>Deployed</deploymentStatus>
    <enableActivities>false</enableActivities>
    <enableFeeds>false</enableFeeds>
    <enableHistory>false</enableHistory>
    <enableReports>true</enableReports>
    <fields>
        <fullName>Invoice__c</fullName>
        <externalId>false</externalId>
        <label>Invoice</label>
        <referenceTo>Invoice__c</referenceTo>
        <relationshipLabel>Line Items</relationshipLabel>
        <relationshipName>Line_Items</relationshipName>
        <relationshipOrder>0</relationshipOrder>
        <reparentableMasterDetail>false</reparentableMasterDetail>
        <type>MasterDetail</type>
        <writeRequiresMasterRead>false</writeRequiresMasterRead>
    </fields>
    <fields>
        <fullName>Line_Item_Total__c</fullName>
        <externalId>false</externalId>
        <formula>Unit_Price__c * Quantity__c</formula>
        <formulaTreatBlanksAs>BlankAsZero</formulaTreatBlanksAs>
        <label>Line Item Total</label>
        <precision>18</precision>
        <required>false</required>
        <scale>2</scale>
        <type>Currency</type>
    </fields>
    <label>Line Item</label>
    <nameField>
        <label>Line Item Number</label>
        <type>Text</type>
    </nameField>
    <pluralLabel>Line Items</pluralLabel>
    <searchLayouts/>
    <sharingModel>ControlledByParent</sharingModel>
    <validationRules>
        <fullName>Validate_Quantity</fullName>
        <active>true</active>
        <errorConditionFormula>(Quantity__c &lt;= 0)</errorConditionFormula>
        <errorDisplayField>Quantity__c</errorDisplayField>
        <errorMessage>You must order at least one item</errorMessage>
    </validationRules>
</CustomObject>


If you save that file away somewhere, and once you have done a Sandbox refresh (and then refreshed the project in Eclipse) if you copy those files BACK into the object folder, you will find, once correctly synced, that the custom object and all its fields and settings are now back and safe in your sandbox! Getting some more complicated permissions stuff like permissions sets takes another level of metadata scraping, but this should get you on the road to saving and restoring your custom objects.

Saving the meta data of an object and it's fields can be a good way to backup/version control them as well.
This was selected as the best answer
James LoghryJames Loghry
I would second Simon's suggestion of using an IDE to pull all your metadata from the sandbox as a backup.  In addition to the Force.com IDE, you could also use Eclipse Force.com plugin or Mavensmate.  There are also 3rd party appexchange tools like "Snapshot" you could use as well.  In addition to the IDE and saving it to your local workspace, I would start looking at a Source Control solution, like github or bitbucket.  Stick your metadata in there, and then you have a versioned backup for the next refresh.