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
CooldayCoolday 

Why am I getting this error "Illegal assignment from List<Id> to Id"

Can anyone tell me why I am getting this error in my code - 
Illegal assignment from List<Id> to Id -(on this line)
Opportunity opp = new Opportunity(Id = Ids);

Apex class -

    @InvocableMethod
    public static void amount(List<Id> Ids){
        Account acc = [SELECT Email__c FROM Account WHERE Id IN (SELECT AccountId from Opportunity WHERE Id =:Ids)];
        HTTP http = new HTTP();
        HTTPRequest request = new HTTPRequest();
        List<Amount__c> listCS      =       Amount__c.getall().values();   
        if (listCS != null && listCS.size() > 0) {
            String endp                     =       listCS[0].endpoint__c+acc.Email__c;
            String auth                     =       listCS[0].auth__c;
            String authorizationHeader      =       'Basic '+ auth; 
            
            request.setHeader      ('Authorization', authorizationHeader);
            request.setEndpoint    (endp);
            request.setMethod      ('GET');
            request.setHeader      ('Content-Type', 'application/json; charset=utf-8');
            HTTPResponse response = http.send(request);
        
        if(response.getStatusCode() == 200){
            system.debug('The Response Body: '+response.getBody());
            Map<String, Object> deserializedPayload = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
            Decimal tax = (Decimal)deserializedPayload.get('tax');
            Opportunity opp = new Opportunity(Id = Ids);
            opp.tax__c = tax;
            update opp;
        }  }  }
Best Answer chosen by Coolday
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

The variable Ids is List<Id> but in the below line you are assiging list <ids> to single id which is causing the issue.
 
Opportunity opp = new Opportunity(Id = Ids);

You need assign each id in the llist to opportuntiyid or if you feel the list will only have one Id then change the code as below.
 
Opportunity opp = new Opportunity(Id = Ids[0]);

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

The variable Ids is List<Id> but in the below line you are assiging list <ids> to single id which is causing the issue.
 
Opportunity opp = new Opportunity(Id = Ids);

You need assign each id in the llist to opportuntiyid or if you feel the list will only have one Id then change the code as below.
 
Opportunity opp = new Opportunity(Id = Ids[0]);

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
CooldayCoolday
I am facing another error on the same code now - 
An Apex error occurred: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out using invocable method
Sai PraveenSai Praveen (Salesforce Developers) 
CooldayCoolday
Hi Sai,

I have tried the above solution but I cannot figure out this error in my debug logs - 
Future method cannot be called from a future or batch method: ClassName.AmountCalloutAsync(List<Id>)

My Apex Class -

    @InvocableMethod
    public static void amount(List<Id> Ids){
        doAmountCalloutAsync(Ids);
    }
    @future(callout=true)
    public static void AmountCalloutAsync(List<Id> Ids){
        Account acc = [SELECT Email__c FROM Account WHERE Id IN (SELECT AccountId from Opportunity WHERE Id =:Ids)];
        HTTP http = new HTTP();
        HTTPRequest request = new HTTPRequest();
        List<Amount__c> listCS      =       Amount__c.getall().values();   
        if (listCS != null && listCS.size() > 0) {
            String endp                     =       listCS[0].endpoint__c+acc.Email__c;
            String auth                     =       listCS[0].auth__c;
            String authorizationHeader      =       'Basic '+ auth;
            system.debug('--Authorization : '+authorizationHeader);
            
            request.setHeader      ('Authorization', authorizationHeader);
            request.setEndpoint    (endp);
            request.setMethod      ('GET');
            request.setHeader      ('Content-Type', 'application/json; charset=utf-8');
            HTTPResponse response = http.send(request);
            system.debug('--response : '+response.getBody());
        
        if(response.getStatusCode() == 200){
            system.debug('The Response Body: '+response.getBody());
            Map<String, Object> deserializedPayload = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
            Decimal balance = (Decimal)deserializedPayload.get('balance');
            Opportunity opp = new Opportunity(Id = Ids[0]);
            opp.tax__c = tax;
            update opp;
        } }} 
CooldayCoolday
Hi Sai, Got the solution 
Added this condition 
if(!System.isFuture()) 

Thanks for the help!!