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
Sandra RadzimskaSandra Radzimska 

error in Apex Class:clsOpportunityTeamLayout

Hi,
I have an error in Apex Class:clsOpportunityTeamLayout.
error message: "List has more than 1 row for assignment to SObject Error is in expression '{!DeleteTeamMember}' in page opportunityteamlayout: Class.clsOpportunityTeamLayout.DeleteTeamMember: line 41, column 1 An unexpected error has occurred. Your development organization has been notified."

Apex Class:clsOpportunityTeamLayout:

public class clsOpportunityTeamLayout {
    Id oppid {get;set;}
     public Boolean flag{get;set;}
  List<OpportunityTeamMember__c> oppTeam ;
    ApexPages.StandardController stdCtrl;
    public string SelectedOppTeamId { get; set; }
    
    public clsOpportunityTeamLayout(ApexPages.StandardController controller) {
        //this.accTeam = (AccountTeamMember__c)controller.getRecord();
        stdCtrl = controller;
         
    }
    
       public List<OpportunityTeamMember__c> getoppTeam() {
           
           List<Opportunity> recOpp = [Select Id, Owner.Id, Name from Opportunity WHERE Id = :ApexPages.currentPage().getParameters().get('id') AND Owner.Id = :userinfo.getUserId()];
        if(recOpp.size()>0 && recOpp[0].Owner.Id == userinfo.getUserId()) {
            flag=false;
        }else {
            flag=true;
        }
           oppid = ApexPages.currentPage().getParameters().get('id');
          // system.debug('IDDDDDDDDDD'+oppid);
        oppTeam = [SELECT Opportunity_Access__c, Opportunity__r.Id, 
                   User__r.Name,Notes_Attachments_Access__c,OpportunityTeamMemberRole__c 
                   from OpportunityTeamMember__c WHERE Opportunity__r.Id  = :oppid];

          // system.debug('accTEammmmmmmmmmmmmmmm'+oppTeam);
           stdCtrl.save();
           return oppTeam;
        }
    
     public PageReference DeleteTeamMember() {
         oppid = ApexPages.currentPage().getParameters().get('id');
   // system.debug('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'+SelectedOppTeamId);
        OpportunityTeamMember__c tobeDeleted = null;
        OpportunityShare oppShare;
      for(OpportunityTeamMember__c a : oppTeam)
       if (a.Id == SelectedOppTeamId) {
          tobeDeleted = a;
           oppShare = [select id,OpportunityId, UserOrGroupId from OpportunityShare where OpportunityId = :oppid and UserOrGroupId = :a.User__c];
           //system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+tobeDeleted);
          //break;
       }
      
      //if opportunity record found delete it
      if (tobeDeleted != null) {
      Delete oppShare;
       Delete tobeDeleted;
          
      }
      PageReference retPage = new PageReference('/apex/OpportunityTeamLayout?id='+oppid);
        retPage.setRedirect(true);
             return retPage;
    }
    
    public PageReference DeleteAll() {
    
        Delete oppTeam;
        PageReference retPage = new PageReference('/apex/OpportunityTeamLayout?id='+oppid);
        retPage.setRedirect(true);
         return retPage;
    }
    
    public pagereference DefaultOppTeam()
    {
        Opportunity recOpp=[select OwnerId from Opportunity where id=:ApexPages.currentPage().getParameters().get('id')];
        List<UserTeamMember> listUTM=[Select Id, OwnerId, UserId, OpportunityAccessLevel, TeamMemberRole FROM UserTeamMember where OwnerId=:recOpp.OwnerId];
        List<OpportunityTeamMember__c> listOTM=new List<OpportunityTeamMember__c>();
        List<OpportunityTeamMember__c> listExistOTM=[select id,User__c from OpportunityTeamMember__c where Opportunity__c=:ApexPages.currentPage().getParameters().get('id')];
        Set<Id> setUsrIds=new Set<Id>();
            if(listExistOTM!=null && listExistOTM.size()>0)
            for(OpportunityTeamMember__c recOTM:listExistOTM)
                setUsrIds.add(recOTM.User__c);
        if(listUTM!=null && listUTM.size()>0)
        {
            for(UserTeamMember recUTM:listUTM)  
            {
                if(setUsrIds!=null && setUsrIds.contains(recUTM.UserId))
                {
                    // if user is already present dont add the user in the list
                }
                else
                {
                    OpportunityTeamMember__c recOTM=new OpportunityTeamMember__c(Opportunity__c=ApexPages.currentPage().getParameters().get('id'));
                    recOTM.User__c=recUTM.UserId;
                                      
                    if(recUTM.OpportunityAccessLevel=='Read' )
                    { recOTM.Opportunity_Access__c='Read Only';}
                    else
                    {recOTM.Opportunity_Access__c='Read/Write';}
                    
                    
                    recOTM.OpportunityTeamMemberRole__c =recUTM.TeamMemberRole;
                    recOTM.Notes_Attachments_Access__c='Read Only';
                    listOTM.add(recOTM);
                }
            }
            if(listOTM!=null && listOTM.size()>0)
                    insert listOTM;
        }
        pagereference par=new Pagereference('/apex/OpportunityTeamLayout?id='+oppid);
        return par;
    }

    
    static testmethod void testTeamMember() {
       Boolean flag = true;
        List<Account> accnts = new List<Account>();
         Account account2 = new Account(Name='Test Account 3', Region__c = 'Asia', Country__c = 'CHINA', OWCODE__c = '13');
        accnts.add(account2);
        insert accnts;
        
        List<Opportunity> opps = new List<Opportunity>();
        Opportunity opp1 = new Opportunity(Name='Test opp 1', accountId = account2.Id, StageName = 'Qualifying', CloseDate =Date.Today());
        opps.add(opp1);
        insert opps;
        
        List<OpportunityTeamMember__c> otms = new List<OpportunityTeamMember__c>();
        OpportunityTeamMember__c otm = new OpportunityTeamMember__c(Opportunity__c=opps[0].Id,Opportunity_Access__c = 'Read Write');
        otms.add(otm);
        insert otms;
        
        List<OpportunityShare> oss = new List<OpportunityShare>();
        OpportunityShare oss1 = new OpportunityShare(OpportunityAccessLevel = 'Edit',OpportunityId=opps[0].Id,UserOrGroupId=UserInfo.getUserId()); 
        oss.add(oss1);
       // insert oss;
        
     /*     if(accnts[0].Owner.Id == UserInfo.getUserId() && accnts.size() > 0) {
            flag=false;
        }else{
            flag = true;
        }*/
        
        ApexPages.currentPage().getParameters().put('id',opps[0].Id);
         ApexPages.StandardController stdContrlr= new ApexPages.StandardController(otm);
        clsOpportunityTeamLayout ObjRef=new clsOpportunityTeamLayout (stdContrlr);
        
        ObjRef.getoppTeam();
        ObjRef.DeleteTeamMember();
        ObjRef.DeleteAll();
        ObjRef.DefaultOppTeam();
        
    }

}

Any help would be appreciated.

Thanks
Tugce SirinTugce Sirin
oppShare = [select id,OpportunityId, UserOrGroupId from OpportunityShare where OpportunityId = :oppid and UserOrGroupId = :a.User__c];
This query returns more than 1 record. This is way you are getting the error. Try using LIMIT 1 at the end of the query ot change this line
 OpportunityShare oppShare;
to this:
 List<OpportunityShare> oppShare;
Sandra RadzimskaSandra Radzimska
Hi,

Thanks for the help.
I have replaced the line. Could you please confirm that I have replaced correct line.

BEFORE:
}
   
     public PageReference DeleteTeamMember() {
         oppid = ApexPages.currentPage().getParameters().get('id');
   // system.debug('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'+SelectedOppTeamId);
        OpportunityTeamMember__c tobeDeleted = null;
        OpportunityShare oppShare;
      for(OpportunityTeamMember__c a : oppTeam)
       if (a.Id == SelectedOppTeamId) {
          tobeDeleted = a;
           oppShare = [select id,OpportunityId, UserOrGroupId from OpportunityShare where OpportunityId = :oppid and UserOrGroupId = :a.User__c];
           //system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+tobeDeleted);
          //break;
       }
AFTER:
 
}
   
     public PageReference DeleteTeamMember() {
         oppid = ApexPages.currentPage().getParameters().get('id');
   // system.debug('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'+SelectedOppTeamId);
        OpportunityTeamMember__c tobeDeleted = null;
         List<OpportunityShare> oppShare;
      for(OpportunityTeamMember__c a : oppTeam)
       if (a.Id == SelectedOppTeamId) {
          tobeDeleted = a;
           oppShare = [select id,OpportunityId, UserOrGroupId from OpportunityShare where OpportunityId = :oppid and UserOrGroupId = :a.User__c];