-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.

1
  • 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<Account> accs = [SELECT Id FROM Account WHERE Name =: objAcc.Name]; if(accs.size() > 0) {}.... Commented Jul 19, 2023 at 16:10

1 Answer 1

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];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.