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
Swaroopa Akula 2Swaroopa Akula 2 

Getting below error while I am inserting Opportunities using dataloader.ERROR: dlrs_OpportunityTrigger: execution of AfterInsertcaused by: System.QueryException: List has more than 1 row for assignment to SObject()

Getting below error while I am inserting Opportunities using dataloader.

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

Below is the trigger code:
/**
 * Auto Generated and Deployed by the Declarative Lookup Rollup Summaries Tool package (dlrs)
 **/
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;
   
    }
}

Pls let me know what is the issue
Andrew GAndrew G
The key message in the error message : 
 System.QueryException: List has more than 1 row for assignment to SObject()

You are doing a query which is returning a List which you are trying to assign to a single object.
Your issue is here:
Opportunity opp = [select Id ,NRR_value__c ,CurrencyIsoCode,RecordTypeId,recordtype.name from Opportunity where ID =: oId];

Any SELECT statement will produce a List
So adjust using one of the following options:
Use LIMIT 1
Opportunity opp = [SELECT Id ,NRR_value__c ,CurrencyIsoCode,RecordTypeId,recordtype.name FROM Opportunity WHERE ID =: oId LIMIT 1];
or pass it back to a list and then grab the first record to be used
List<Opportunity> opps = [select Id ,NRR_value__c ,CurrencyIsoCode,RecordTypeId,recordtype.name from Opportunity where ID =: oId];
Opportunity opp = opps[0];

regards
Andrew