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
wadams2010wadams2010 

On mass udpate error getting error: List has more than 1 row for assignment to SObject

Hey everybody,

I am getting the following error when I try and update multiple records (Feature Request custom object) through an enhanced list view. I know it has to do with I am querying multiple records but I don't know the best method to get this fixed. Does it have to do with maps? See apex code below. Thanks!
 
trigger UpdateOFR on Feature_Request__c (after update, after insert) { 

    Feature_Request__c fr = [Select Feature_Request_Status__c, Target_Release__c, Owner.Name from Feature_Request__c where ID IN :trigger.new];
    
    List<Opportunity_Feature_Request__c> lstToUpdate = new List<Opportunity_Feature_Request__c>();
	for(Opportunity_Feature_Request__c obj :[select Feature_Request_Status__c, Feature_Request_Target_Release__c, Feature_Request_Owner__c from Opportunity_Feature_Request__c where Feature_Request__c in : trigger.new]){
		
         
        obj.Feature_Request_Target_Release__c = fr.Target_Release__c;
        obj.Feature_Request_Owner__c = fr.Owner.Name;
        obj.Feature_Request_Status__c = fr.Feature_Request_Status__c;
		lstToUpdate.add(obj);
	}

	if(!lstToUpdate.isEmpty())
		update lstToUpdate;
}
George AdamsGeorge Adams
I'm pretty new to Apex, but I believe you need to pull the records from Trigger.new and place them into a list, then interate over the list.

ie:
trigger UpdateOFR on Feature_Request__c (after update, after insert) { 

    list<Feature_Request__c> frList = new list<Feature_Request__c>([Select Feature_Request_Status__c, Target_Release__c, Owner.Name from Feature_Request__c where ID IN :trigger.new]);

    for (Feature_Request__c fr : frList) {
        //do your actions per-Feature_Request__c here
    }
}
Another approach (I think the better one) would be to iterate over Trigger.new, like:
trigger UpdateOFR on Feature_Request__c (after update, after insert) { 
    for (Feature_Request__c fr : Trigger.new) {
        //do your actions per-Feature_Request__c here
    }
}

Again, I'm pretty new at Apex so take this with a grain of salt.