2014-10-16 69 views
0

相當新的APEX(Salesforce的開發),我發現一個問題,用SOQL語句之一...最多2半連接子選擇允許(Salesforce的)

Error Message

這是代碼創造這個問題..我做了一些研究,我發現,我需要建立一個集ID的使用OpportunityId另一個SELECT語句,然後只引用它的查詢,而不是...

String sumAvgQuery = 'SELECT StageName, COUNT(ID) opps, SUM(Amount) total, AVG(SVC_Svc0StageAge__c) Svc0, AVG(SVC_Svc1StageAge__c) Svc1, ' + 
           'AVG(SVC_Svc2StageAge__c) Svc2, AVG(SVC_Svc3StageAge__c) Svc3, AVG(SVC_Svc4StageAge__c) Svc4, ' + 
           'AVG(SVC_Svc5StageAge__c) Svc5, AVG(SVC_Svc6StageAge__c) Svc6, AVG(SVC_Svc7StageAge__c) Svc7, ' + 
           'AVG(SVC_Svc8StageAge__c) Svc8, AVG(SVC_Svc9StageAge__c) Svc9 ' + 
           'FROM Opportunity ' + 
           'WHERE StageName in (' + BTG_Utility.OPPORTUNITY_STAGES + ') ' + 
           'AND ID in (SELECT OpportunityId FROM OpportunityTeamMember WHERE UserId = \'' + String.escapeSingleQuotes(userId) + '\') ' + 
           ((cluster != null && cluster != '') ? 'AND SVC_AccountCluster__c = \'' + String.escapeSingleQuotes(cluster) + '\' ' : '') + 
           ((region != null && region != '') ? 'AND SVC_AccountRegion__c = \'' + String.escapeSingleQuotes(region) + '\' ' : '') + 
           ((country != null && country != '') ? 'AND CARE_AccountCountry__c = \'' + String.escapeSingleQuotes(country) + '\' ' : '') +                
           ((product != null && product != '') ? ' AND Id in (SELECT OpportunityId FROM OpportunityLineItem Where Product2.Name = \'' + String.escapeSingleQuotes(product) + '\' and Opportunity.IsClosed = FALSE) ' : '') + 
           'GROUP BY StageName)'; 

你能請幫助我如何做到這一點?真的很感謝幫助!

回答

0

如果你想使用像Apex集合那樣的集合,你需要首先填充集合,然後你可以在你的SOQL查詢中使用變量名稱:在它前面:

因此,例如:

// First populate the Set with ID's using SOQL 
set<Account> inputSet = new set<Account>([SELECT Id FROM Account LIMIT 5]); 

//Alternatively manually populate the set 
// set<String> inputSet = new set<String>(); 
// List<Account> accounts = new List<Account>([SELECT custom_id__c FROM Account LIMIT 5]); 
// for (Account acc : accounts) { 
//  inputSet.add(acc.custom_id__c); 
// } 
System.debug('inputSet: ' + inputSet); 

// Use the set with the data 

List<Contact> contacts = [SELECT id FROM Contact where custom_id__c in :inputSet]; 
System.debug('contacts: ' + contacts); 

希望有所幫助。

Mohamed Imran