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
Anoop Patel 7Anoop Patel 7 

I'm trying to populate a field from Opportunity to my custom object using a trigger and receiving a error

I'm trying to populate a field from Opportunity to my custom object using a trigger and receiving the following error;

Error: Compile Error: Illegal assignment from LIST<Opportunity> to MAP<Id,Opportunity> at line 13 column 7

My code;

trigger populateAccountfromOpp on Service_Item__c(after insert, after update){

      Set<Id> oppIds = new Set<Id>();
      Map<Id, Opportunity> oppMap;
    
      for(Service_Item__c ser : trigger.new){
        if(ser.Opportunity__c!= null){
          oppIds.add(ser.Opportunity__c);
        }  
    
      }
    
      oppMap = [select Id, Account_Type__c from Opportunity where Id in : oppIds];
    
      for(Service_Item__c ser : trigger.new){
        if(ser.Opportunity__c != null && oppMap.containsKey(ser.Opportunity__c)){  
         ser.Account__c = oppMap.get(ser.Opportunity__c).Account_Type__c;
        }
      }
    }

Thanks in advance for any help that can be provided
Best Answer chosen by Anoop Patel 7
Denis VakulishinDenis Vakulishin
Hello,
Replace this line
oppMap = [select Id, Account_Type__c from Opportunity where Id in : oppIds];

with this line
oppMap = new Map<Id,Opportunity([select Id, Account_Type__c from Opportunity where Id in : oppIds]);


All Answers

Denis VakulishinDenis Vakulishin
Hello,
Replace this line
oppMap = [select Id, Account_Type__c from Opportunity where Id in : oppIds];

with this line
oppMap = new Map<Id,Opportunity([select Id, Account_Type__c from Opportunity where Id in : oppIds]);


This was selected as the best answer
Denis VakulishinDenis Vakulishin
Additional information:
Standard SOQL request returns List<sObject> collection, not Map<K,V>
Anoop Patel 7Anoop Patel 7
Hello Denis,

Should it be like this?

oppMap = new Map<Id,Opportunity>([select Id, Account_Type__c from Opportunity where Id in : oppIds]);
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
yes anoop you are right
Denis VakulishinDenis Vakulishin
hi Anoop,
Yes, it should be like this.