-1

**This is An Apex Class Calling From Parent JS.

public with sharing class accountProvider {
   
    @AuraEnabled

    public static List<Contact> searchContact(Account objAcc){
     
      Id accId =  [select Id,Name from Account where Name =: objAcc.Name].Id;

      System.debug('Received Contact = '+objAcc);
      System.debug('ID '+accId);


       System.debug('contactsColumn'+ ([select Id, Name,Email,Level__c from Contact where Contact  =: accId]));

       return ([select Id, Name,Email,Level__c from Contact where Contact =: accId]);
    }
  }

Solution on LWC interruption.

NedlaV
  • 18
  • 2
  • I'm not quite sure what your question is on this post. Could you please provide more details? From what I can see, your initial query will throw an error if no records are found. This is not a safe way to write the query. Try to add fail safes into your code to account for situations where no records are returned, i.e.: `List accs = [SELECT Id FROM Account WHERE Name =: objAcc.Name];` `if(accs.size() > 0) {}....` – Tyler Edwards Jul 19 '23 at 16:10

1 Answers1

0

Id accId = [select Id,Name from Account where Name =: objAcc.Name].Id; this makes assumption that exactly 1 account will have that Name. If it's 0 - error. If it's 2 or more - error.

It's better to write something like this (well, it's minimally better. If you have 3 accounts with Name = "Acme" - 1 will be selected at random.

List<Account> accs = [SELECT Id FROM Account WHERE Name = :objAcc.Name LIMIT 1];
if(!accs.isEmpty()){
...
}

Now... what do you need to return? Contacts that belong to this account? A more "pro" way would be:

@AuraEnabled(cacheable=true)
public static List<Contact> searchContact(Account objAcc){
    return [SELECT Id, Name, Email,Level__c FROM Contact WHERE Account.Name = :objAcc.Name];
}
eyescream
  • 18,088
  • 2
  • 34
  • 46