1

I've spent several hours today trying to figure this out and running through several online resources here and SFDC forums to no avail.

I keep getting the following error:

List has no rows for assignment to SObject An unexpected error has occurred. Your development organization has been notified.

Here is my code:

public class SubscriptionAddProdExtension {

    private Final Subscription__c sub;

    public ApexPages.StandardController thisAddProdController {get; set;}
    public Subscription_Additional_Products__c sAddProd {get; set;}

    public ApexPages.StandardController standardContactController;

    public SubscriptionAddProdExtension(ApexPages.StandardController cntrl) { 

        this.sub = (Subscription__c)cntrl.getRecord();

        List<Subscription_Additional_Products__c> sap = [SELECT Name, Id, Expiry_Date__c, Subscription__c, Tax_Type__c, Name__c FROM Subscription_Additional_Products__c WHERE Subscription__c =:this.sub.Id];
        if(sap.size() >0 ){
            system.debug('Retrieved Subscription_Additional_Products__c ' + SAddProd.id);
            return;
        }
        if(sap.size() == null ){
            system.debug('Retrieved Subscription_Additional_Products__c ' + SAddProd.id);
            return;
        }
        else{

        sAddProd = [SELECT Name, Id, Expiry_Date__c, Subscription__c, Tax_Type__c, Name__c
        FROM Subscription_Additional_Products__c WHERE Subscription__c = :this.sub.Id ];
        system.debug('Displaying ' + SAddProd.id);

        thisAddProdController = new ApexPages.StandardController(sAddProd);
        }
    }
        public pageReference doSaveAll() {
            standardContactController.save();
            thisAddProdController.save();
            return null;
        }
}

VisualForce Markup:

<apex:pageBlock title="Aditional Products" mode="edit" >
    <apex:pageBlockButtons>
        <apex:commandButton action="{!save}" value="Save"/>
    </apex:pageBlockButtons>
    <apex:pageBlockSection title="Product Details" columns="2" >
        <apex:outputField value="{!sAddProd.Name}"/>
        <apex:outputField value="{!sAddProd.Tax_Type__c}"/>
        <apex:outputField value="{!sAddProd.Expiry_Date__c}"/>
        <apex:outputField value="{!sAddProd.Name__c}"/>
    </apex:pageBlockSection>
</apex:pageBlock>
2
  • Try instantiating the list sAddProd instead, as you have for sap list
    – Girbot
    Jun 13, 2017 at 19:35
  • sap.size() will never be null.
    – sfdcfox
    Jun 13, 2017 at 19:39

1 Answer 1

1

You should just use cntrl.getId() instead. Calling cntrl.getRecord() only pulls in the fields included on your page. Everywhere you have this.sub.Id, change it to cntrl.getId().

That said, you are never guaranteed to have a child record. The standard approach to querying for a single record whose existence is not guaranteed is to query to a list, then check if it is empty:

List<ChildObject__c> candidates = [
    SELECT ... FROM ChildObject__c
    WHERE Parent__c = :controller.getId()
    LIMIT 1
];
if (!candidates.isEmpty())
    childRecord = candidates[0];
4
  • Hi @AdrianLarson, I did as you said, made those changes, minus the getId. It doesn't give the error, still works, but those that don't have a child record show data. Interesting, I'm sure it has to do with getId. Side note, I tried your method getId and it gave me an issue. Not sure if I should write a separate question for that, or I can try to add it again and let you know what error it gave me. Appreciate the tips!
    – Louis HM
    Jun 13, 2017 at 19:57
  • @LouisHM Did you try to use controller.getId() instead of cntrl.getId()? My post is actually crafted to discourage copy pasta.
    – Adrian Larson
    Jun 13, 2017 at 20:01
  • I too am still learning. I've moved this conversation to chat so we can continue it without cluttering the comments.
    – Adrian Larson
    Jun 13, 2017 at 20:08
  • This is what I get --> ***classes/SubscriptionAddProdExtension.cls: Incompatible types since an instance of String is never an instance of Subscription__c (Line: 12, Column: 20) ***
    – Louis HM
    Jun 13, 2017 at 20:09

You must log in to answer this question.

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