0

Getting an error while I upload the Opportunity records using data loader.

ERROR: dlrs_OpportunityTrigger: execution of AfterInsertcaused by: System.QueryException: List has more than 1 row for assignment to SObject()

Here is the trigger code:

trigger dlrs_OpportunityTrigger on Opportunity (before delete, before insert, before update, after delete, after insert, after undelete, after update) {
    {
        dlrs.RollupService.triggerHandler(Opportunity.SObjectType);
    }

    if (trigger.isafter&& trigger.isInsert) {
        Set<id> oId = new set<id>();
        for(Opportunity opp : trigger.new) {
            oId.add(opp.Id);
        }

        Opportunity opp = [select Id ,NRR_value__c ,CurrencyIsoCode,RecordTypeId,recordtype.name from Opportunity where ID =: oId];
        system.debug('opp************'+opp);

        PriceBookEntry p = [SELECT Id, Product2Id, Product2.Id, Product2.Name, CurrencyIsoCode
                        FROM PriceBookEntry
                        WHERE  Product2Id='01t0J00000IDOpDQAX' and CurrencyIsoCode =: opp.CurrencyIsoCode
                        and Pricebook2Id = '01s0J000002SCKDQA4' LIMIT 1];
        system.debug('p***************'+p);
    
        string recordtypename = Schema.SObjectType.Opportunity.getRecordTypeInfosById().get(opp.recordtypeid).getname();
        system.debug(' recordtypename*************'+recordtypename);
    
        List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        for(Opportunity op : trigger.new){
            if(opp.RecordType.Name == 'Services Record Type'){
                OpportunityLineItem oli = new OpportunityLineItem();
                oli.OpportunityId = opp.Id;
                oli.Quantity = 1;
                oli.PricebookEntryId = p.Id;
                //oli.name = opp.name;
                
                oli.UnitPrice = opp.NRR_value__c;
                oliList.add(oli);
                system.debug('oliList------------'+oliList);
            }   
        }
        insert oliList;
    }
}

1 Answer 1

0

Your query is returning more than one record, but the variable 'opp' is a single instance and not a List of Opportunities. If you change the code as following, it will work.

 List<Opportunity> opp = new List<Opportunity>([select Id ,NRR_value__c ,CurrencyIsoCode,RecordTypeId,recordtype.name from Opportunity where ID =: oId]);

You must log in to answer this question.

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