I am trying to display an error when updating a child record. Here's my scenario Parent Bank__c Child Branch__c Relationship field on child BankId__c Updating field on child checkbox Primary_Branch__c

My requirement is only one child Branch can be a primary branch for a Bank, if a user tries to update another branch as primary branch where there is an existing branch it should display an alert stating that there is an existing bank which is primary.

public static string validatePrimarybranch(String branchId, String bankId){
    List<branch__c> branchList = new List<branch__c>();
    branchList  = [SELECT Primary_branch__c FROM branch__c WHERE bankId__c =: bankId];
    String displayString = '';
    return displayString;

debug Log:

branchlist: (branch__c:{Primary_branch__c=true, Id=a0L2g0000006y6dTREY}, branch__c:{Primary_branch__c=false, Id=a0L2g0000006y6dTREY})

I am getting list of branches with data, but how can I just grab that primary branch data from that list and add it to string, so that I can display an error when that field is being updated.

Any suggestions, thanks!

1 Answer 1


Your query is getting back data that you don't need. If all you want is the details of the one primary branch, or the information that there is no primary branch, filter your query by that field:

branchList  = [SELECT Name FROM branch__c WHERE bankId__c =: bankId WHERE Primary_branch__c = true];

Then you can just check branchList.size() - if it's 0, there's no primary branch; if it's nonzero, the data for the primary branch is located in branchList[0], e.g., branchList[0].Name.

  • oh yea, I did not think in that way.. It worked!! Thanks a lot David!
    – user62694
    Jul 10, 2019 at 20:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .