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
Jeff GillisJeff Gillis 

Web service to get related opportunity Ids based on converted lead records

Hi,

I am trying to setup a web service where I call to get a list of opportunity records based on converted leads from a specific source.  I am trying to create a list of opportunity ids based on a SOQL query against the lead object, but am getting the "Illegal assignment from List to List" error and had try to google that without any luck.

My simple httpget is below:
@RestResource(urlMapping='/getOpportunityInfo/*')
global with sharing class ZoomInfoAPI {

    
    @HttpGet
    global static List<Opportunity> doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        List<String> convertOppList = new List<String>();
		convertOppList = [SELECT ConvertedOpportunityId FROM Lead WHERE LeadSource = 'ZoomInfo' AND Status = 'Converted']; 
        List <Opportunity> oppList = [SELECT Id, Name, Amount, Owner_Name__c, Contact_Name__c, AccountId, Account_Name__c, Account.Name, Account.ShippingStreet, Account.ShippingCity, Account.ShippingState, Account.ShippingPostalCode, Account.ShippingCountry, Stagename, IPG_Market_Code__c, IPG_App_Code__c, CloseDate, Opp_Shipping_Address__c, IPG_Industry_Type__c, Createddate FROM Opportunity WHERE Id IN :convertOppList];
        System.debug(oppList);
        return oppList;
        
    }
    
}
I am new to apex and am likely making some fundamental mistake.  Any quick advise would be greatly appreciated!

Thank you!
SwethaSwetha (Salesforce Developers) 
HI Jeff,
I see you also posted this ask on https://salesforce.stackexchange.com/questions/375715/web-service-to-get-related-opportunity-ids-based-on-converted-lead-records


According to this, your issue is here
List<String> convertOppList = new List<String>();
convertOppList = [SELECT ConvertedOpportunityId FROM Lead WHERE LeadSource = 'ZoomInfo' AND Status = 'Converted'];

Your query is returning a List<Lead>, and you're trying to store that in a variable of type List<String>. Lead and String are incompatible types, so you get an error. The "Illegal assignment from List to List" error is just an unfortunate effect of Salesforce's attempt to strip out HTML (the <String> and <Lead> parts look like HTML, and there's not really a good way to automatically tell that it isn't HTML).

Since you're looking to use these opp Ids to drive your next query, you should be iterating over the results of the first query and storing the Ids in a List or a Set.
// List or Set, it doesn't really matter which you use in this scenario
Set<Id> targetOppIds = new Set<Id>();
// Queries can directly feed for loops like this. Salesforce calls this a
//   "SOQL for loop", and it's a good way to retrieve results while keeping
//   heap space utilization low
for(Lead theLead :[SELECT ConvertedOpportunityId FROM Lead WHERE ...]){
    targetOppIds.add(theLead.ConvertedOpportunityId);
}

List <Opportunity> oppList = [SELECT Id, ... FROM Opportunity WHERE Id IN :targetOppIds];

In this particular situation though, you could save yourself a little typing by mashing those two queries into a single query. The first query you have would become a semi-join (basically, a nested query that appears in the WHERE clause).
 
List<Opportunity> oppList = [
    SELECT Id, ... 
    FROM Opportunity 
    WHERE Id IN 
        (SELECT ConvertedOpportunityId FROM Lead WHERE LeadSource = 'ZoomInfo' AND Status = 'Converted')
];

Please consider closing the thread marking answer as best so that others facing the same issue can also find it helpful. Thank you