0

We are trying to test backup service and delete as much as we can and then restore it. The restoration process works in a way that restores all the child objects when you restore the parent object and if you choose to restore parent object and child objects the process lasts much longer.

What I need then is to find all the objects that have no parent objects, so I can restore only them and not their child objects.

I have this code, but it only finds parent objects of one particular object, so it's not really what I need.

for(Schema.SobjectField strFld: Account.SobjectType.getDescribe().fields.getMap().Values()){
    if(strFld.getDescribe().getType() == Schema.DisplayType.REFERENCE){
        system.debug(‘==parent object=’+strFld.getDescribe().getReferenceTo());
    }
}

Can you help me find code that would list all objects that have no parent object, so I can do the restoration process with them?

1
  • "Parent" is not a strictly defined term. That could be Parent Accounts, which is just a lookup from one Account to another, and it could also be objects that are a master in a master-detail relationship. The code you have will identify any Lookup and M-D fields on the Account, so that's a start, but not all lookup fields imply a parent-child relationship. Does that make sense? I would look at your data model and determine where those relationships are first.
    – nbrown
    Nov 13 '20 at 13:58
2

I think this goal is misguided. Almost all key objects in the standard schema have parents of one kind or another:

  • Account
  • Contact
  • Case
  • Opportunity
  • Asset
  • Lead
  • Campaign

... and so on; this is likely to be the case in your custom schema as well. Plus, if you consider OwnerId a relationship (which is is, technically speaking) - well, that brings in almost everything, so scraping the schema using Describe calls is not likely to bring you much usable data.

Perhaps the standard object least likely to have a populated parent reference (although it does have a couple of relationships), and thus most responsive to your actual question, is Product2.

That said, restoring objects without restoring relationships isn't a very complete test of a Salesforce backup system, because in real-world use cases it is almost certain that any data you are restoring will either need children restored or will need relationships to other objects that were not themselves deleted restored.

1
  • Also: CaseTeamRole, Holiday, Individual, Pricebook2 have no parents (other than OwnerId); There are probably others
    – cropredy
    Nov 13 '20 at 20:30

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.