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
KaityKaity 

Triiger Issue-Incompatible element type Id for collection of Opportunity​

Hi, While writing this trigger, I am getting this error below  marked as underline-
Incompatible element type Id for collection of Opportunity​


Please help to resolve the issue

 
trigger  UpdateOpportunity on Top_X_Designation__c (after Insert, after Update, after Delete) {
List<Opportunity> lstopp = new List<Opportunity>();
List<Opportunity> lstOppUpdated = new List<Opportunity>();

  // Set<Id> lstOppUpdated = new Set<Id>();

Map <Id, Id> docAttachTrue = new Map<id,id>();
Map <Id, Id> docAttachFalse= new Map<id,id>();
Map <Id, Id> docAttachDel = new Map<id,id>();

Set<id> opptySetId = new Set<id>();
Set<id> opptySetDel = New Set<id>();

if(Trigger.IsInsert || Trigger.IsUpdate){
for(Top_X_Designation__c txd: Trigger.new){
  if(txd.Type__c=='Contract Flow Down/Handoff' && txd.Document_Attached__c == True ){
   docAttachTrue.put(txd.Opportunity__c, txd.id);
   opptySetId.add(txd.Opportunity__c);
  }
  else
   docAttachFalse.put(txd.Opportunity__c, txd.id);
   opptySetId.add(txd.Opportunity__c);
    }
    }
    
    if(Trigger.IsDelete){
    for(Top_X_Designation__c txdold: Trigger.old){
    docAttachDel.put(txdold.Opportunity__c, txdold.id);
    opptySetId.add(txdold.Opportunity__c);
    opptySetDel.add(txdold.Opportunity__c);
    }
  
}


lstopp = [SELECT ID, Handoff_Attached__c FROM Opportunity WHERE id in : opptySetId];
 if(lstopp.size()>0 && lstopp!= null){
   for(Opportunity opp:lstopp){
    if(docAttachTrue.containsKey(opp.id)){
      opp.Handoff_Attached__c = 'Yes';
 }
 if(docAttachFalse.containsKey(opp.id)){
      opp.Handoff_Attached__c = 'No';
 }
 
 if(opptySetDel.contains(opp.id)){
      opp.Handoff_Attached__c = '';
 }
 lstoppUpdated.add(opp.id);  //Error: Incompatible element type Id for collection of Opportunity​
}
  if(lstoppUpdated.size()>0 && lstoppUpdated != null){
    update lstoppUpdated;
    }
  }
  
 }

Thanks,
Kaity
Hemant_JainHemant_Jain
Hello,

The list lstoppUpdated is of Opportunity type. You are trying to add Id into the Opportunity list.

You can change the code to:
 
lstoppUpdated.add(opp);

or you can change the type of the list to Id type at the top, like
List<Id> lstOppUpdated = new List<Id>();

Hope this resolves. If it does kindly mark it as the best answer :)
KaityKaity
Thanks for your reply. But it gives me a different error in Line 53:

DML requires SObject or SObject list type: List<Id>
 
trigger  UpdateOpportunity on Top_X_Designation__c (after Insert, after Update, after Delete) {
List<Opportunity> lstopp = new List<Opportunity>();
   //List<Opportunity> lstOppUpdated = new List<Opportunity>();

  // Set<Id> lstOppUpdated = new Set<Id>();
List<Id> lstOppUpdated = new List<Id>();

Map <Id, Id> docAttachTrue = new Map<id,id>();
Map <Id, Id> docAttachFalse= new Map<id,id>();
Map <Id, Id> docAttachDel = new Map<id,id>();

Set<id> opptySetId = new Set<id>();
Set<id> opptySetDel = New Set<id>();

if(Trigger.IsInsert || Trigger.IsUpdate){
for(Top_X_Designation__c txd: Trigger.new){
  if(txd.Type__c=='Contract Flow Down/Handoff' && txd.Document_Attached__c == True ){
   docAttachTrue.put(txd.Opportunity__c, txd.id);
   opptySetId.add(txd.Opportunity__c);
  }
  else
   docAttachFalse.put(txd.Opportunity__c, txd.id);
   opptySetId.add(txd.Opportunity__c);
    }
    }
    
    if(Trigger.IsDelete){
    for(Top_X_Designation__c txdold: Trigger.old){
    docAttachDel.put(txdold.Opportunity__c, txdold.id);
    opptySetId.add(txdold.Opportunity__c);
    opptySetDel.add(txdold.Opportunity__c);
    }
  
}


lstopp = [SELECT ID, Handoff_Attached__c FROM Opportunity WHERE id in : opptySetId];
 if(lstopp.size()>0 && lstopp!= null){
   for(Opportunity opp:lstopp){
    if(docAttachTrue.containsKey(opp.id)){
      opp.Handoff_Attached__c = 'Yes';
 }
 if(docAttachFalse.containsKey(opp.id)){
      opp.Handoff_Attached__c = 'No';
 }
 
 if(opptySetDel.contains(opp.id)){
      opp.Handoff_Attached__c = '';
 }
 lstoppUpdated.add(opp.id);
}
  if(lstoppUpdated.size()>0 && lstoppUpdated != null){
    update lstoppUpdated;
    }
  }
  
 }

 
Hemant_JainHemant_Jain
Sorry, I didn't see there is a update statemet. you can update only Sobjects.

So you need to keep the list of Opportunity type only and change the line no 50 to:
 
lstoppUpdated.add(opp);
And keep the list as it was before:
List<Opportunity> lstOppUpdated = new List<Opportunity>();
KaityKaity
Thanks Hemant, its working now. Appreciate your quick help!
Hemant_JainHemant_Jain
Great! Don't forget to mark it as the best answer :)