1

My situation is:

Assume I am selling computers, so each computer has an Asset. Also, each computer has components like monitor, motherboard, cpu, etc. Each component has an Asset too, and they all have a look up field pointing to the computer Asset. The computer does NOT have look up field for component Assets.

Now for some reasons, people buy two computers, later they combine the components, so all component Assets look up field pointing to computer Asset 1, and computer Asset 2 is "childless", because it has no component Asset child.

I would like to find out all computer Assets like Asset 2 in previous example. Is there a way to get the childless Assets?

  • 1
    What is the problem in firing a SOQL using a subquery to the children and then checking the size. If it is zero, you can say the asset is orphaned. – Shailesh Patil Feb 5 '16 at 6:37
  • orphaned means an object has no parent; I think, and please confirm, that you are looking for top level assets that are childless. If yes, then @DerekF has the answer – cropredy Feb 5 '16 at 21:12
  • @crop1645 Updated the question. In my company they named such "childless" assets "orphan" because they are not in a bigger system we used. – xis Feb 5 '16 at 21:14
0

As Shailesh Patil mentioned in the comment on your question, this can be achieved with a parent-child subquery.

To do this, you'll need to know the relationship name for the self-lookup between your Component (Assets) and your Computer (Asset). For sake of example, I'll assume this relationship is called Components__r.

The query would look something like this:

[SELECT Id, (SELECT Id FROM Components__r) FROM Asset]

Naturally, you'll want to figure out something you can use to filter this query in a WHERE clause so the query becomes selective. If you have separate record types for Computers and Components, that would be a splendid choice.

You can then loop through the results, and check for the number of Components like so:

for(Asset asst :[SELECT Id, (SELECT Id FROM Components__r) FROM Asset]){
    if(asst.Components__r.size() == 0){
        // The Asset which enters this block has no Components
    }
}

I imagine this would be of most use in a Before Update trigger on Asset, which would allow you to filter the query with a list of Computer Asset Ids, and detect your 'Orphaned' computers as they become orphaned.

  • of course, this will return leaf childless components as well; OP needs some way to know, like you suggested with recordTypes or the like, to distinguish between top level assets and component assets – cropredy Feb 5 '16 at 21:17

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.