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
brozinickrbrozinickr 

return null in apex class issue, list with no assignments error

I'm having an issue with one of my classes and triggers and I am hoping someone may be able to help me.  I am getting this error:

 

Error:Apex trigger CreateActualsAndTargets caused an unexpected exception, contact your administrator: CreateActualsAndTargets: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Class.GooalAndActuals.getPaymentType: line 69, column 1

 

I understand that when this error happens because there is no records for it to assign.  I did this on purpose to break my trigger and to make sure it was working correctly.  What I don't understand is that I have a return null statement to account for this situation and its not working.

 

public String getPaymentType()
 {
 if(opportunityNew!=null)
     {
     Recon_Detail__c reconDetail = [Select id, Payment_Type__c From Recon_Detail__c Where Contract_Id__c=:opportunityNew.Contract_ID__c limit 1];  //fails at this line
     return reconDetail.Payment_Type__c;}
 else return null;
 }
 
 public void CreateActual(Id OwnderID,ID GoalID)
 {
          
    if((opportunityNew!=null) && (GoalID!= null))
    {
            
            String getPaymentType = getPaymentType();
            
            l_oActual = new Actual__c();
            l_oActual.Opportunity__c=opportunityNew.Id;
            l_oActual.Opportunity_Amount__c=opportunityNew.Amount;
            l_oActual.AccountName__c=opportunityNew.AccountId;
            l_oActual.Goal__c=GoalID;
            l_oActual.Actual_Owner__c=OwnderID;
            l_oActual.Payment_Type__c=getPaymentType();
            insert l_oActual;
    }
 }

 here's my trigger:

 

trigger CreateActualsAndTargets 
on 
Opportunity (after insert, after update) 
{
 if(Trigger.isUpdate || Trigger.isInsert)   
    {
      Map<ID,Opportunity> l_OpportunityOwners = new Map<ID,Opportunity> ([Select id,o.Owner.ManagerId,Contract_ID__c,StageName,Amount,AccountId,CloseDate, o.OwnerId, Type From Opportunity o where id in:trigger.newMap.keySet()]);
      GooalAndActuals l_oGooalAndActuals = new GooalAndActuals ();
        
      for(Opportunity l_oOpportunityNew:l_OpportunityOwners.values())
      {
        System.debug('********************Trigger Start***********************');
        if(l_oOpportunityNew.StageName=='Closed/Won')
        {
          ID SalesRepGoalID,ManagerGoalID;
          //modified
          l_oGooalAndActuals = new GooalAndActuals ();
          l_oGooalAndActuals.opportunityNew=l_oOpportunityNew;
          
          
          //Goal Creation
          SalesRepGoalID=l_oGooalAndActuals.CreateGoal(l_oOpportunityNew.OwnerId);//SalesRep Goal Creation
          if(l_oOpportunityNew.Owner.ManagerId!= null)
          {
            ManagerGoalID=l_oGooalAndActuals.CreateGoal(l_oOpportunityNew.Owner.ManagerId);//Manager Goal Creation
          }
          
          
          //Actual Deletion
          if(Trigger.isUpdate)
          {
            l_oGooalAndActuals.DeleteActual(l_oOpportunityNew.Id);
            
          }
          
          //Actual Creation
          l_oGooalAndActuals.CreateActual(l_oOpportunityNew.OwnerId,SalesRepGoalID);//SalesRep Actual Creation
          if(l_oOpportunityNew.Owner.ManagerId!= null)
          {
            l_oGooalAndActuals.CreateActual(l_oOpportunityNew.Owner.ManagerId,ManagerGoalID);//Manager Actual Creation
          }
          
          
          
        }
              
        else if(Trigger.isUpdate)
        {
            if(Trigger.oldMap.get(l_oOpportunityNew.Id).StageName == 'Closed/Won' && l_oOpportunityNew.StageName != 'Closed/Won')
            {
                l_oGooalAndActuals = new GooalAndActuals ();
                l_oGooalAndActuals.opportunityNew=l_oOpportunityNew;
                l_oGooalAndActuals.DeleteActual(l_oOpportunityNew.Id);
            }
        }      
        
      }
    }
   
}

 

 

 

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Daniel HaselhanDaniel Haselhan

My bad, I forgot to add the brackets.

That line should read:

if (reconDetails.size() > 0) { 

 

 

All Answers

Daniel HaselhanDaniel Haselhan

The problem is that there is nothing in the Recon_Detail__c table even when opportunityNew is not null. You should store the query result in a list, then check the size of the list and set the object you are trying to set.

 

public String getPaymentType()
 {
	String paymentType = null;
	if(opportunityNew!=null)
	{
		List<Recon_Detail__c> reconDetails = [Select id, Payment_Type__c From Recon_Detail__c Where Contract_Id__c=:opportunityNew.Contract_ID__c limit 1];
		if (reconDetails.size() > 0) {
			paymentType = reconDetails[0].Payment_Type__c;
		}
	}
	return paymentType;
 }

 

 

brozinickrbrozinickr

That got rid of the error I had before but now I am getting this instead:

 

Compile Error: Variable does not exist: rconDetails.size at line 71 column 13

 

public String getPaymentType()
 {
    String paymentType = null;
    if(opportunityNew!=null)
    {
        List<Recon_Detail__c> reconDetails = [Select id, Payment_Type__c From Recon_Detail__c Where Contract_Id__c=:opportunityNew.Contract_ID__c limit 1];
        if (rconDetails.size > 0) {  //line it's failing at
            paymentType = reconDetails[0].Payment_Type__c;
        }
    }
    return paymentType;
 }

 

 

 

 

 

Daniel HaselhanDaniel Haselhan

My bad, I forgot to add the brackets.

That line should read:

if (reconDetails.size() > 0) { 

 

 

This was selected as the best answer
brozinickrbrozinickr

Still having an error at the same line, now it's saying 'Method does not exist or incorrect signature: rconDetails.size()'

 

 

Got it!   reconDetails was spelled rconDetails!

Daniel HaselhanDaniel Haselhan

Ah another mistake, hard to catch things like that when I can't actually compile the code, haha.

 

I have edited my original post to reflect the proper code. Probably best to set that as the answer.