Relation exist check

What is the best way to check if a relation exists? Should I use a try catch block or is it better to use if else?

to check if relation exists:

if(relation_to_relation) {

}

or check if relation exist, and look if there are related records:

if(relation_to_relation && relation_to_relation.getSize()) {

}

Hello,

If you just want to check the existence of relationship then you can pass the relation inside ‘if’ condition. This will return the related foundset if the correct relationship is there, but it will not check for the existence of related data.

if(customers_to_orders){
    
}

It is safe to use utils.hasRecords(customers_to_orders) or databaseManager.hasRecords(customers_to_orders) to check existence, validity and availability of related records in the relation.

if(utils.hasRecords(customers_to_orders)){
     // work on the related data..
}

Tip: you can also pass multiple relations to “utils.hasRecords()” to check all at once. This is very helpful when you are chaining relations in one line of code and need to check all the relations in the chain.