function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hermann OuréHermann Ouré 

Can I pass a Standard object as a parameter in a List?

Hello,

I have created a component to search Account's opportunities by keyword. But when pressing the search button on my component, the results return all opportunities, even those not related to the Account.

I have tried to modified my SOQL request but can't find the right way request only the opportunities that belongs to a particular account.

Am I allowed to passed an Object as a paramter in my List?
Thank you?

here is my modified request:
public with sharing class SearchOpportunityController {
	
	@AuraEnabled
 	public static List <Opportunity> getOppty(String searchOppty, Account a) {
  		String accId = a.Id;
        String searchKey = searchOppty + '%';
 		List <Opportunity> oppties = new List <Opportunity>();
  		List <Opportunity> lstOfOppty = [SELECT Id, Name, StageName, Amount, CloseDate FROM Opportunity
                                   		 WHERE Name LIKE: searchKey AND AccountId = :accId];
 		System.debug('lstOfOppty' +lstOfOppty);
        
  		for (Opportunity opp: lstOfOppty) {
   			oppties.add(opp);
            System.debug('oppties' +oppties);
  		}
  		return oppties;
 	}
}

 
AnudeepAnudeep (Salesforce Developers) 
Hi Hermann, 

Can you remove the like operator in your query and see if it returns the results correctly? 

I ran the following queries in my org and only the second one worked
SELECT Id, AccountId, Name, StageName, Amount, CloseDate FROM Opportunity WHERE NAME LIKE:'Grand%' AND AccountId='0017F00000B6qX1QAJ'
SELECT Id, AccountId, Name, StageName, Amount, CloseDate FROM Opportunity WHERE NAME LIKE'Grand%'AND AccountId='0017F00000B6qX1QAJ'

Anudeep
shaik murthujavalishaik murthujavali
Hi,
I think you don't need to pass entire account object here.
It's better to pass account Id as a parameter here istead of passing entire account object as a parameter.So just pass Id value to the method.

If it helps for you pls mark it as a best answer, it helps others too.
Hermann OuréHermann Ouré
Hi Anudeep,
I tried To remove, LIKE but on my side, it doesn't return anything.
Also here you use an hard coded Id AccountId='0017F00000B6qX1QAJ'
Was it only for test purpose? 
As I do want to use any hard coded Id.
Hermann OuréHermann Ouré
Hi Shaik,
When you say to only pass accountId as a parameter?
did you mean something like this:
public static List <Opportunity> getOppty(String searchOppty, Account.Id)

 
AnudeepAnudeep (Salesforce Developers) 
Hi Hermann, 

Yes, it was only for tests. I realized that your 
String accId = a.Id; might be returning null and hence it is returning opportunities related to all the accounts. Can you add a system debug to confirm? If that is the case, can you try with passing a list of objects instead or in a way that is described in this example?

Anudeep

 
Andrew GAndrew G
Ok.  Your main code/SELECT statement works.
example, with hardcoded strings for testing
String accId = '0016D00000L0c6SQAR';
//String accId = '0016D00000KzhVoQAJ';  //alternate test string
String searchKey = 'baba'+ '%';
List <Opportunity> lstOfOppty = [SELECT Id, Name, StageName, Amount, CloseDate FROM Opportunity
                                   		 WHERE Name LIKE :searchKey AND AccountId = :accId];
If(lstOfOppty.IsEmpty()){
    System.debug('NO RECORDS');
} else {
    System.debug('lstOfOppty' + lstOfOppty);    
}

So, where is the problem?
Do a test in your code, change the Account a to a String accId and pass the Id as a string to the method.
This would give indication either way to whether the Account or Account Id would work better.

To answer the first question, yes, you can pass objects , or more correctly records, to methods.  Generally though you would do it when there are multiple attributes from the record that you need for the method.
Consider a method that invokes a SELECT statement such as:
List <Opportunity> Opportunity= [SELECT Id, Name, StageName, Amount, CloseDate FROM Opportunity
                                   		 WHERE Name LIKE :searchKey AND Amount > :oppty.Amount AND CloseDate > :oppty.CloseDate AND StageName LIKE :oppty.StageName];
In this case, the SELECT statement is using 3 different values from the Record to find other Records.  So you could pass the method the 3 individual search elements or you could just pass it the Record that is the reference for the search.

The other thought looking at your code, is that after you have your SELECT statement results (which is a list of Oppty), you then do a loop to simply add (or transfer) the records to another list which you pass back.  Should I assume there is a plan for some sort of validation / filtering of the records in that loop so that you pass back a smaller List that what is returned in the SELECT statement?  If the method should just return the results of the SELECT statement, then I would consider that loop unnecessary.
 
//is this necessary?
  		for (Opportunity opp: lstOfOppty) {
   			oppties.add(opp);
            System.debug('oppties' +oppties);
  		}
  		return oppties;

//why not just 
return lstOfOppty ;



Regards

Andrew