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
Merry SMerry S 

List has no rows for assignment to SObject

Hello,

 

I found a controller that will clone all fields for an object. I am using this controller to help me clone and opp if specific requirements are met. (StageName = '09-Win' and type = 'Reseller / Partner Registration'). The works fine if there is an opp that meets the requirements but if I try to change that op (or update any op that does not meet the requirements) I get the error:

Error:Apex trigger partnerOppClone caused an unexpected exception, contact your administrator: partnerOppClone: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.partnerOppClone: line 2, column 1

 

I understand that it is telling me that since it does not meet the requirements it had no rows - I am just stumped at how to go about fixing it.

 

Thanks in advance.

 

 

 

 

trigger partnerOppClone on Opportunity (after update) {
Opportunity originalOpp = [select Id from Opportunity where 
                                StageName = '09-Win' and type = 'Reseller / Partner Registration'];

    sObject originalSObject = (sObject) originalOpp;

        List<sObject> originalSObjects = new List<sObject>{originalSObject};
      
            List<sObject> clonedSObjects = SObjectAllFieldCloner.cloneObjects(
                                      originalSobjects,
                                      originalSobject.getsObjectType());
                                     
    Opportunity clonedOpp = (Opportunity)clonedSObjects.get(0);
  clonedOpp.StageName='training';
if (clonedOpp != null)
insert clonedOpp;
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

Something like this should do:

 

trigger partnerOppClone on Opportunity (after update) {
	Opportunity[] originalOppList = [select Id from Opportunity where StageName = '09-Win' and type = 'Reseller / Partner Registration'];	
	
	if (originalOppList != null && !originalOppList.isEmpty())
	{
		Opportunity originalOpp = originalOppList[0];
		sObject originalSObject = (sObject) originalOpp;
		
		List<sObject> originalSObjects = new List<sObject>{originalSObject};
		
		List<sObject> clonedSObjects = SObjectAllFieldCloner.cloneObjects(
		originalSobjects,
		originalSobject.getsObjectType());
		
		Opportunity clonedOpp = (Opportunity)clonedSObjects.get(0);
		clonedOpp.StageName='training';
		if (clonedOpp != null)
		insert clonedOpp;
	}
}

 I'm also not sure why you have to cast the opportunity to an original SObject list, passing in the list of opportunities should work regardless since SObject is the parent type of opportunity.

All Answers

Sean TanSean Tan

Something like this should do:

 

trigger partnerOppClone on Opportunity (after update) {
	Opportunity[] originalOppList = [select Id from Opportunity where StageName = '09-Win' and type = 'Reseller / Partner Registration'];	
	
	if (originalOppList != null && !originalOppList.isEmpty())
	{
		Opportunity originalOpp = originalOppList[0];
		sObject originalSObject = (sObject) originalOpp;
		
		List<sObject> originalSObjects = new List<sObject>{originalSObject};
		
		List<sObject> clonedSObjects = SObjectAllFieldCloner.cloneObjects(
		originalSobjects,
		originalSobject.getsObjectType());
		
		Opportunity clonedOpp = (Opportunity)clonedSObjects.get(0);
		clonedOpp.StageName='training';
		if (clonedOpp != null)
		insert clonedOpp;
	}
}

 I'm also not sure why you have to cast the opportunity to an original SObject list, passing in the list of opportunities should work regardless since SObject is the parent type of opportunity.

This was selected as the best answer
Merry SMerry S

Thanks!