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
MaggieSumitMaggieSumit 

getting bulkification issues on SOQL query () : Error List has more than 1 row for assignment to SObject 20

Getting error on Line no 13.
  1. public class CloneInternshipOpportunity {
  2.    
  3.     public static void createInternshipOpportunity(List<alu_Internship_Cycle__c> internCycle) {
  4.         /*try{*/
  5.             
  6.             Map<Id,alu_Internship_Cycle__c> mapIdbyNewInternCyc = new  Map<Id,alu_Internship_Cycle__c>(internCycle);
  7.             System.debug('mapIdbyNewInternCyc'+mapIdbyNewInternCyc);
  8.  
  9.             Map<Id,alu_Internship_Cycle__c> mapIdbyOldInternCyc = new Map<Id,alu_Internship_Cycle__c> ([
  10.                 SELECT Id, Name, Start_Date__c 
  11.                 FROM alu_Internship_Cycle__c 
  12.                 WHERE Id NOT IN: mapIdbyNewInternCyc.keySet() 
  13.                 AND Start_Date__c =: mapIdbyNewInternCyc.values().Start_Date__c.addYears(-1)]);
  14.             
  15.             System.debug('mapIdbyOldInternCyc '+mapIdbyOldInternCyc);
  16.             
  17.             List<Opportunity> oppListByOldCyc = [
  18.                 SELECT id, Name, RecordTypeId, AccountId, Account.Name, Internship_Cycle__c, Internship_Cycle__r.Name, CloseDate,  StageName 
  19.                 FROM Opportunity 
  20.                 WHERE Internship_Cycle__c IN: mapIdbyOldInternCyc.KeySet()
  21.             ];
  22.             
  23.              System.debug('oppListByOldCyc '+oppListByOldCyc);
  24.             
  25.             //Set<Opportunity> oppsToClone = new Set<Opportunity>();
  26.             List<Opportunity> oppsToClone = new List<Opportunity>();
  27.             
  28.             for(Id tmp : mapIdbyNewInternCyc.keySet()){                 
  29.                 for(Opportunity opp : oppListByOldCyc){
  30.                     Opportunity opps = new Opportunity();
  31.                     opps.Name = 'Clone Opportunity';
  32.                     opps.RecordTypeId = opp.RecordTypeId;
  33.                     opps.CloseDate = opp.CloseDate;
  34.                     opps.StageName = opp.StageName;
  35.                     opps.AccountId = opp.AccountId;
  36.                     opps.Internship_Cycle__c = tmp;
  37.                     oppsToClone.add(opps);
  38.                     
  39.                 }
  40.             }
  41.             
  42.             //List<Opportunity> op = new List<Opportunity>(oppsToClone);
  43.             insert oppsToClone;
  44.             
  45.             System.debug('Opportunity>>>'+oppsToClone);
  46.             
  47.             /*}catch (Exception e){
  48.             system.debug('Error '+e.getMessage() +' '+e.getLineNumber());
  49.         }*/
  50.         
  51.      } 
  52.  
  53. }
Waqar Hussain SFWaqar Hussain SF
Hi Sumit,

Actually the code mapIdbyNewInternCyc.values() return a list from map values. Try below code 
 
public class CloneInternshipOpportunity {
   
    public static void createInternshipOpportunity(List<alu_Internship_Cycle__c> internCycle) {
        /*try{*/
            
            Map<Id,alu_Internship_Cycle__c> mapIdbyNewInternCyc = new  Map<Id,alu_Internship_Cycle__c>(internCycle);
            System.debug('mapIdbyNewInternCyc'+mapIdbyNewInternCyc);
 
            list<date> StartDates = new list<date>();
		for(alu_Internship_Cycle__c abc : mapIdbyNewInternCyc.values()){
			if(abc.Start_Date__c != null)
			StartDates.add(abc.Start_Date__c.addYears(-1));
		}
		Map<Id,alu_Internship_Cycle__c> mapIdbyOldInternCyc = new Map<Id,alu_Internship_Cycle__c> ([
                SELECT Id, Name, Start_Date__c 
                FROM alu_Internship_Cycle__c 
                WHERE Id NOT IN: mapIdbyNewInternCyc.keySet() 
                AND Start_Date__c IN :StartDates]);
            
            System.debug('mapIdbyOldInternCyc '+mapIdbyOldInternCyc);
            
            List<Opportunity> oppListByOldCyc = [
                SELECT id, Name, RecordTypeId, AccountId, Account.Name, Internship_Cycle__c, Internship_Cycle__r.Name, CloseDate,  StageName 
                FROM Opportunity 
                WHERE Internship_Cycle__c IN: mapIdbyOldInternCyc.KeySet()
            ];
            
             System.debug('oppListByOldCyc '+oppListByOldCyc);
            
            //Set<Opportunity> oppsToClone = new Set<Opportunity>();
            List<Opportunity> oppsToClone = new List<Opportunity>();
            
            for(Id tmp : mapIdbyNewInternCyc.keySet()){                 
                for(Opportunity opp : oppListByOldCyc){
                    Opportunity opps = new Opportunity();
                    opps.Name = 'Clone Opportunity';
                    opps.RecordTypeId = opp.RecordTypeId;
                    opps.CloseDate = opp.CloseDate;
                    opps.StageName = opp.StageName;
                    opps.AccountId = opp.AccountId;
                    opps.Internship_Cycle__c = tmp;
                    oppsToClone.add(opps);
                    
                }
            }
            
            //List<Opportunity> op = new List<Opportunity>(oppsToClone);
            insert oppsToClone;
            
            System.debug('Opportunity>>>'+oppsToClone);
            
            /*}catch (Exception e){
            system.debug('Error '+e.getMessage() +' '+e.getLineNumber());
        }*/
        
     } 
 
}