1

I have below controller where SaveOutcome method throws error :

"System.QueryException: List has more than 1 row for assignment to SObject"

Can someone please let me know the root cause.

Apex controller:

public class ComplianceIncidentAttestCont_sb {


        public Set<Id> citIds {get;set;}//List of Ids
    public Compliance_Incident_Transaction_abv__c  oInc {        get;        set;    }
        public list<Compliance_Incident_Transaction_abv__c>  oIncList {        get;        set;    }
    public Boolean refreshPage {        get;        set;    }

    public string nextlink {        get;        set;    }

    public boolean showMsg {        get;        set;    }

    public String targetid {        get;        set;    }

    public String saveTransactionType {     get;    set;    }

    public boolean stopmsg {        get;        set;    }

    ApexPages.StandardSetController contr {        get;        set;    }

    Id recId;   public List < Compliance_Incident_Transaction_abv__c > oIncTranList {       get;        set;    }

    //Other lists to be ref. in PDF

    public List < Compliance_Incident_Transaction_abv__c > oIncTranListHCPDetail {      get;        set;    }

    public List < Compliance_Incident_Transaction_abv__c > oIncTranListHCI {        get;        set;    }

    public List < Compliance_Incident_Transaction_abv__c > oIncTranListPharmacy {       get;        set;    }

    public List < Compliance_Incident_Transaction_abv__c > oIncTranListHCPSamples {     get;        set;    }

    public List < Compliance_Incident_Transaction_abv__c > oIncTranListHCPSpeaker {     get;        set;    }

    //public List < Incident_Transaction_abv__c > oIncTranListSynch {     get;        set;    }

    public List < Compliance_Incident_Transaction_abv__c > oIncTranListGEN {        get;        set;    }

    public List < Compliance_Incident_Transaction_abv__c > check {      get;        set;    }

     public ComplianceIncidentAttestCont_sb(ApexPages.StandardController scontroller) {
     }



    public ComplianceIncidentAttestCont_sb(ApexPages.StandardSetController controller) {//NO List contrller

        targetid = '';

        stopmsg = false;
        //citIds='';
        //Initialize
        citIds = new Set<Id>();

        //Code added Spandan
        /*for(Compliance_Incident_Transaction_abv__c inc:(List<Compliance_Incident_Transaction_abv__c>)controller.getSelected())
        {
            if(Ids == '')
                recId =inc.Id;
            else
                recId = Ids + ';' +inc.Id;
        }*/// REVERTED


        oIncList =controller.getSelected();
        //recId = ApexPages.CurrentPage().getparameters().get('Ids');

       // oInc = (Compliance_Incident_Transaction_abv__c ) controller.getRecord();//OINC is a single instance

        contr = controller;
        //Iterate 
        for(Compliance_Incident_Transaction_abv__c oInc : oIncList)
            {
            //Ids- Fetch & fill

            citIds.add(oInc.Id);
            System.debug('CIT LIST-----------'+citIds);

            }
        oIncTranList = [SELECT Id,

        Name, RecordTypeId, RecordType.Name, Transaction_Type_abv__c, Account_abv__c, Product_abv__c, Call_abv__c,

         Audit_Reason_abv__c, Outcome_abv__c

        FROM Compliance_Incident_Transaction_abv__c where Id in :citIds order by RecordType.Name];

                 for(Compliance_Incident_Transaction_abv__c oInc : oIncTranList )
            {
            //Ids- Fetch & fill




        if (oInc.Outcome_abv__c == null) showMsg = true;

        else showMsg = false;
        }


        //HCP DETAIL CALLS

        oIncTranListHCPDetail = [SELECT Name, ABS_on_Call_abv__c, Account_abv__c, Transaction_Type_abv__c, Product_abv__c,  Audit_Reason_abv__c,

        //Attendee_Role_CIA__c ,City_abv__c,Zip_abv__c,Call_abv__r.Associated_Prescriber_ID_abv__r.Name,Account_Name_abv__c ,

        //Compliance_Incident_Transaction_abv__c.Address_Line2_abv__c,Incident_Transaction_abv__c,Compliance_Incident_Transaction_abv__c.Date_of_Last_Sync_abv__c ,Compliance_Incident_Transaction_abv__c.Date_of_Prev_Sync_abv__c , Compliance_Incident_Transaction_abv__c.Days_bwn_Sync_abv__c ,Compliance_Incident_Transaction_abv__c.Outcome_abv__c , Compliance_Incident_Transaction_abv__c.RecordType.Name ,                               

        // Compliance_Incident_Transaction_abv__c.Prof_First_Name_abv__c,Compliance_Incident_Transaction_abv__c.Prof_Middle_Name_abv__c,Compliance_Incident_Transaction_abv__c.Prof_Last_Name_abv__c,Compliance_Incident_Transaction_abv__c.Address_Line1_abv__c,

        Call_Detail_abv__r.Product_vod__r.Name,     Call_Sample_abv__r.Product_vod__r.Name,     Call_Sample_abv__r.Quantity_vod__c,

        Call_abv__r.Territory_vod__c,       Call_abv__r.account_vod__c,     Call_abv__r.account_vod__r.Preferred_Name_vod__c,

        Call_abv__r.account_vod__r.Name,        Call_abv__r.account_vod__r.Abbott_Best_Specialty_ABT__c,        Call_abv__r.Address_Line_1_vod__c,

        Call_abv__r.Address_Line_2_vod__c,      Call_abv__r.City_vod__c,        Call_abv__r.State_vod__c,       Call_abv__r.Zip_vod__c,

        Call_abv__r.Call_Date_vod__c,       Call_abv__r.Professional_First_Name_ABT__c,     Call_abv__r.Professional_Last_Name_ABT__c,

        Call_abv__r.Professional_Middle_Name_ABT__c,        Call_abv__r.Attendee_Role_CIA__c,       Call_abv__r.Professional_Type_ABT__c,

        Call_abv__r.Professional_Abbott_Best_Specialty_ABT__c,      Call_abv__r.Attendee_Associated_Prescriber_CIA__r.Name,

        Call_abv__r.Attendee_Associated_Prescriber_ABS_CIA__c,      Call_abv__r.RecordType.Name,        Call_abv__r.Call_Type_vod__c,

        Call_abv__r.Call_Datetime_vod__c, id, recordtype.name, Outcome_abv__c, Time_of_Event_ABS_abv__c

        FROM Compliance_Incident_Transaction_abv__c where Id in :citIds  and

        recordtype.name like 'HCP Detail %'];



        //HCI/HCO CALLS TRXN TYPE

        oIncTranListHCI = [SELECT Name, ABS_on_Call_abv__c, Account_abv__c, Transaction_Type_abv__c, Product_abv__c,

        Audit_Reason_abv__c,

        //Attendee_Role_CIA__c ,City_abv__c,Zip_abv__c,Call_abv__r.Associated_Prescriber_ID_abv__r.Name,Account_Name_abv__c ,

        //Compliance_Incident_Transaction_abv__c.Address_Line2_abv__c,Incident_Transaction_abv__c,Compliance_Incident_Transaction_abv__c.Date_of_Last_Sync_abv__c ,Compliance_Incident_Transaction_abv__c.Date_of_Prev_Sync_abv__c , Compliance_Incident_Transaction_abv__c.Days_bwn_Sync_abv__c ,Compliance_Incident_Transaction_abv__c.Outcome_abv__c , Compliance_Incident_Transaction_abv__c.RecordType.Name ,                               

        // Compliance_Incident_Transaction_abv__c.Prof_First_Name_abv__c,Compliance_Incident_Transaction_abv__c.Prof_Middle_Name_abv__c,Compliance_Incident_Transaction_abv__c.Prof_Last_Name_abv__c,Compliance_Incident_Transaction_abv__c.Address_Line1_abv__c,

        Call_Detail_abv__r.Product_vod__r.Name,     Call_Sample_abv__r.Product_vod__r.Name,     Call_Sample_abv__r.Quantity_vod__c,

        Call_abv__r.Territory_vod__c,       Call_abv__r.account_vod__c,     Call_abv__r.account_vod__r.Preferred_Name_vod__c,

        Call_abv__r.account_vod__r.Name,        Call_abv__r.account_vod__r.Abbott_Best_Specialty_ABT__c,        Call_abv__r.Address_Line_1_vod__c,

        Call_abv__r.Address_Line_2_vod__c,      Call_abv__r.City_vod__c,        Call_abv__r.State_vod__c,       Call_abv__r.Zip_vod__c,

        Call_abv__r.Call_Date_vod__c,       Call_abv__r.Professional_First_Name_ABT__c,     Call_abv__r.Professional_Last_Name_ABT__c,

        Call_abv__r.Professional_Middle_Name_ABT__c, recordtype.name, Call_abv__r.Attendee_Role_CIA__c,       Call_abv__r.Professional_Type_ABT__c,

        Call_abv__r.Professional_Abbott_Best_Specialty_ABT__c,      Call_abv__r.Attendee_Associated_Prescriber_CIA__r.Name,

        Call_abv__r.Attendee_Associated_Prescriber_ABS_CIA__c,      Call_abv__r.RecordType.Name,        Call_abv__r.Call_Type_vod__c,

        Call_abv__r.Call_Datetime_vod__c, id,   Outcome_abv__c, Time_of_Event_ABS_abv__c

        FROM Compliance_Incident_Transaction_abv__c where Id in :citIds and

        recordtype.name = 'HCI/HCO Calls'];





        //PHARMACY CALLS



        oIncTranListPharmacy = [SELECT Name, ABS_on_Call_abv__c, Account_abv__c, Transaction_Type_abv__c, Product_abv__c,

        Audit_Reason_abv__c,

        //Attendee_Role_CIA__c ,City_abv__c,Zip_abv__c,Call_abv__r.Associated_Prescriber_ID_abv__r.Name,Account_Name_abv__c ,

        //Compliance_Incident_Transaction_abv__c.Address_Line2_abv__c,Incident_Transaction_abv__c,Compliance_Incident_Transaction_abv__c.Date_of_Last_Sync_abv__c ,Compliance_Incident_Transaction_abv__c.Date_of_Prev_Sync_abv__c , Compliance_Incident_Transaction_abv__c.Days_bwn_Sync_abv__c ,Compliance_Incident_Transaction_abv__c.Outcome_abv__c , Compliance_Incident_Transaction_abv__c.RecordType.Name ,                               

        // Compliance_Incident_Transaction_abv__c.Prof_First_Name_abv__c,Compliance_Incident_Transaction_abv__c.Prof_Middle_Name_abv__c,Compliance_Incident_Transaction_abv__c.Prof_Last_Name_abv__c,Compliance_Incident_Transaction_abv__c.Address_Line1_abv__c,

        Call_Detail_abv__r.Product_vod__r.Name,     Call_Sample_abv__r.Product_vod__r.Name,     Call_Sample_abv__r.Quantity_vod__c,

        Call_abv__r.Territory_vod__c,       Call_abv__r.account_vod__c,     Call_abv__r.account_vod__r.Preferred_Name_vod__c,

        Call_abv__r.account_vod__r.Name,        Call_abv__r.account_vod__r.Abbott_Best_Specialty_ABT__c,        Call_abv__r.Address_Line_1_vod__c,

        Call_abv__r.Address_Line_2_vod__c,      Call_abv__r.City_vod__c,        Call_abv__r.State_vod__c,       Call_abv__r.Zip_vod__c,

        Call_abv__r.Call_Date_vod__c,       Call_abv__r.Professional_First_Name_ABT__c,     Call_abv__r.Professional_Last_Name_ABT__c,

        Call_abv__r.Professional_Middle_Name_ABT__c,        Call_abv__r.Attendee_Role_CIA__c,       Call_abv__r.Professional_Type_ABT__c,

        Call_abv__r.Professional_Abbott_Best_Specialty_ABT__c,      Call_abv__r.Attendee_Associated_Prescriber_CIA__r.Name,

        Call_abv__r.Attendee_Associated_Prescriber_ABS_CIA__c, recordtype.name,Call_abv__r.RecordType.Name,        Call_abv__r.Call_Type_vod__c,

        Call_abv__r.Call_Datetime_vod__c, id,       Outcome_abv__c, Time_of_Event_ABS_abv__c

        FROM Compliance_Incident_Transaction_abv__c where Id in :citIds and

        recordtype.name = 'Pharmacy Calls'];





        //SAMPLE calls

        oIncTranListHCPSamples = [SELECT Name, ABS_on_Call_abv__c, Account_abv__c, Transaction_Type_abv__c, Product_abv__c,

        Audit_Reason_abv__c,

        //Attendee_Role_CIA__c ,City_abv__c,Zip_abv__c,Call_abv__r.Associated_Prescriber_ID_abv__r.Name,Account_Name_abv__c ,

        //Compliance_Incident_Transaction_abv__c.Address_Line2_abv__c,Incident_Transaction_abv__c,Compliance_Incident_Transaction_abv__c.Date_of_Last_Sync_abv__c ,Compliance_Incident_Transaction_abv__c.Date_of_Prev_Sync_abv__c , Compliance_Incident_Transaction_abv__c.Days_bwn_Sync_abv__c ,Compliance_Incident_Transaction_abv__c.Outcome_abv__c , Compliance_Incident_Transaction_abv__c.RecordType.Name ,                               

        // Compliance_Incident_Transaction_abv__c.Prof_First_Name_abv__c,Compliance_Incident_Transaction_abv__c.Prof_Middle_Name_abv__c,Compliance_Incident_Transaction_abv__c.Prof_Last_Name_abv__c,Compliance_Incident_Transaction_abv__c.Address_Line1_abv__c,

        Call_Detail_abv__r.Product_vod__r.Name,     Call_Sample_abv__r.Product_vod__r.Name, Call_Sample_abv__r.Quantity_vod__c,

        Call_Sample_abv__r.Product_vod__r.Product_Type_vod__c,      //  Call_Sample_abv__r.Quantity_vod__c ,

        Call_abv__r.Territory_vod__c,       Call_abv__r.account_vod__c,     Call_abv__r.account_vod__r.Preferred_Name_vod__c,

        Call_abv__r.account_vod__r.Name, recordtype.name,  Call_abv__r.account_vod__r.Abbott_Best_Specialty_ABT__c,        Call_abv__r.Address_Line_1_vod__c,

        Call_abv__r.Address_Line_2_vod__c,      Call_abv__r.City_vod__c,        Call_abv__r.State_vod__c,       Call_abv__r.Zip_vod__c,

        Call_abv__r.Call_Date_vod__c,       Call_abv__r.Professional_First_Name_ABT__c,     Call_abv__r.Professional_Last_Name_ABT__c,

        Call_abv__r.Professional_Middle_Name_ABT__c,        Call_abv__r.Attendee_Role_CIA__c,       Call_abv__r.Professional_Type_ABT__c,

        Call_abv__r.Professional_Abbott_Best_Specialty_ABT__c,      Call_abv__r.Attendee_Associated_Prescriber_CIA__r.Name,

        Call_abv__r.Attendee_Associated_Prescriber_ABS_CIA__c,      Call_abv__r.RecordType.Name,        Call_abv__r.Call_Type_vod__c,

        Call_abv__r.Call_Datetime_vod__c, id,       Outcome_abv__c, Time_of_Event_ABS_abv__c

        FROM Compliance_Incident_Transaction_abv__c where Id in :citIds and

        recordtype.name = 'HCP Sample Calls'];



        //SPEAKER PROG ATTENDEES



        oIncTranListHCPSpeaker = [SELECT Name, ABS_on_Call_abv__c, Account_abv__c, Transaction_Type_abv__c, Product_abv__c,

        Audit_Reason_abv__c,

        //Attendee_Role_CIA__c ,City_abv__c,Zip_abv__c,Call_abv__r.Associated_Prescriber_ID_abv__r.Name,Account_Name_abv__c ,

        //Compliance_Incident_Transaction_abv__c.Address_Line2_abv__c,Incident_Transaction_abv__c,Compliance_Incident_Transaction_abv__c.Date_of_Last_Sync_abv__c ,Compliance_Incident_Transaction_abv__c.Date_of_Prev_Sync_abv__c , Compliance_Incident_Transaction_abv__c.Days_bwn_Sync_abv__c ,Compliance_Incident_Transaction_abv__c.Outcome_abv__c , Compliance_Incident_Transaction_abv__c.RecordType.Name ,                                

        // Compliance_Incident_Transaction_abv__c.Prof_First_Name_abv__c,Compliance_Incident_Transaction_abv__c.Prof_Middle_Name_abv__c,Compliance_Incident_Transaction_abv__c.Prof_Last_Name_abv__c,Compliance_Incident_Transaction_abv__c.Address_Line1_abv__c,

        Call_Detail_abv__r.Product_vod__r.Name,     Call_Sample_abv__r.Product_vod__r.Name,     Call_Sample_abv__r.Quantity_vod__c,

        Call_abv__r.Territory_vod__c,       Call_abv__r.account_vod__c,     Call_abv__r.account_vod__r.Preferred_Name_vod__c,

        Call_abv__r.account_vod__r.Name,        Call_abv__r.account_vod__r.Abbott_Best_Specialty_ABT__c,        Call_abv__r.Address_Line_1_vod__c,

        Call_abv__r.Address_Line_2_vod__c,      Call_abv__r.City_vod__c,        Call_abv__r.State_vod__c,       Call_abv__r.Zip_vod__c,

        Call_abv__r.Call_Date_vod__c,       Call_abv__r.Professional_First_Name_ABT__c,     Call_abv__r.Professional_Last_Name_ABT__c,

        Call_abv__r.Professional_Middle_Name_ABT__c,        Call_abv__r.Attendee_Role_CIA__c,       Call_abv__r.Professional_Type_ABT__c,

        Call_abv__r.Professional_Abbott_Best_Specialty_ABT__c,      Call_abv__r.Attendee_Associated_Prescriber_CIA__r.Name,

        Call_abv__r.Attendee_Associated_Prescriber_ABS_CIA__c,      Call_abv__r.RecordType.Name,        Call_abv__r.Call_Type_vod__c,

        Call_abv__r.Call_Datetime_vod__c, id, recordtype.name,Outcome_abv__c, Time_of_Event_ABS_abv__c

        FROM Compliance_Incident_Transaction_abv__c where Id in :citIds and

        recordtype.name like 'Speaker%'];



        //oIncTranListSynch- obsolete







        if (ApexPages.CurrentPage().getparameters().get('Saved') == 'Success') {

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Successfully Saved'));

            ApexPages.CurrentPage().getparameters().put('Saved', '');

        }

    }



    public pagereference saveOutcome()

    {

        List < Compliance_Incident_Transaction_abv__c > saveList = new List < Compliance_Incident_Transaction_abv__c >();

        Compliance_Incident_Transaction_abv__c saveRec;

        if(saveTransactionType == 'HCP Detail Calls')

            saveList = oIncTranListHCPDetail ;

        else if(saveTransactionType == 'HCI/HCO Calls')

            saveList = oIncTranListHCI ;

        else if(saveTransactionType == 'Pharmacy Calls')

            saveList = oIncTranListPharmacy ;

        else if(saveTransactionType == 'HCP Sample Calls')

            saveList = oIncTranListHCPSamples ;

        else if(saveTransactionType == 'Speaker program Attendees')

            saveList = oIncTranListHCPSpeaker ;    

        for(Compliance_Incident_Transaction_abv__c CITRec: saveList)

        {

            if(CITRec.Id == targetid)

                saveRec = CITRec;

            break;

        }

        if(saveRec!=null)

            update saveRec;



        oInc = [SELECT

            Attested_By_abv__c, Attested_Datetime_abv__c, Confirm_Attestation_abv__c, //ConnectionReceivedId,

            Id, Manager_Name_abv__c, // Outcome_Blank_Tran_Count_abv__c,

            OwnerId, Publish_Date_abv__c, Rep_abv__c, Rep_Franchise_abv__c, Report_Publish_Date_abv__c, Status_abv__c FROM Compliance_Incident_Transaction_abv__c

            where Id in :citIds];  // This line throws exception

           /* if (oInc.Outcome_abv__c == null) showMsg = true;

            else showMsg = false;*/





        return null;

    }

    public pagereference savei() {

        //stopmsg=false;

        update oIncTranList;//DML

        //Update all lists into a gen. list



        oIncTranListGEN = new List < Compliance_Incident_Transaction_abv__c > ();





        //Addall Lists for TrxnLI

        oIncTranListGEN.addAll(oIncTranListHCPDetail);

        oIncTranListGEN.addAll(oIncTranListHCI);

        oIncTranListGEN.addAll(oIncTranListPharmacy);

        oIncTranListGEN.addAll(oIncTranListHCPSamples);

        oIncTranListGEN.addAll(oIncTranListHCPSpeaker);

        System.debug('S$$$$$$$$$$$$$$$$$$$' + oIncTranListGEN);

        if (oIncTranListGEN.SIZE() > 0) {

            List < Database.SaveResult > res = Database.update(oIncTranListGEN, false);

            stopmsg = true;

            /*for (Database.SaveResult s : res) {

     if(s.isSuccess())

     {stopmsg=true;}

    }*/

            //  check.addall(oIncTranListGEN);

            //update oIncTranListGEN;

        }





        oInc = [SELECT

        Attested_By_abv__c, Attested_Datetime_abv__c, Confirm_Attestation_abv__c, //ConnectionReceivedId,

         Id, Manager_Name_abv__c, //Outcome_Blank_Tran_Count_abv__c,

          OwnerId, Publish_Date_abv__c, Rep_abv__c, Rep_Franchise_abv__c, Report_Publish_Date_abv__c, Status_abv__c FROM Compliance_Incident_Transaction_abv__c

        where Id in :citIds];

        if (oInc.Outcome_abv__c == null) showMsg = true;

        else showMsg = false;

        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Successfully Saved.'));

        stopmsg = true;

        /* nextlink = '/apex/AttestCompIncident3?Id=' + recId + '&Saved=Success';

        PageReference nextPage = new PageReference(nextlink);

        nextPage.setRedirect(true);



        refreshPage = true;     */

        return null;

    }



    public pagereference reset() {



        return null; // Same page

    }

    public PageReference confirmAttest() {

        if (oInc == null) {

            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Incident is not available');

            ApexPages.addMessage(msg);

        } else {

            oInc.Attested_Datetime_abv__c = DateTime.Now();

            oInc.Attested_By_abv__c = UserInfo.getUserId();

            oInc.Status_abv__c = 'Closed';

            update oInc;

            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Successfully Attested');

            ApexPages.addMessage(msg);

        }

        return null;

    }

}

1 Answer 1

3

in saveOutcome(), you are assigning a list to a single oInc sObject. Your SOQL is returning more than 1 record. You can try this :

list<Compliance_Incident_Transaction_abv__c> oIncList = [SELECT Attested_By_abv__c, Attested_Datetime_abv__c, Confirm_Attestation_abv__c, //ConnectionReceivedId,

     Id, Manager_Name_abv__c, //Outcome_Blank_Tran_Count_abv__c,

      OwnerId, Publish_Date_abv__c, Rep_abv__c, Rep_Franchise_abv__c, Report_Publish_Date_abv__c, Status_abv__c FROM Compliance_Incident_Transaction_abv__c

    where Id in :citIds]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .