0
votes

Getting an error when tried to execute workbook apex code

"System.QueryException: List has no rows for assignment to SObject: Class.InvoiceUtilities.renumberLineItems: line 8, column 1 AnonymousBlock: line 1, column 1 AnonymousBlock: line 1, column 1"

public class InvoiceUtilities {

    // class method to renumber Line Items for a given Invoice number
    // returns a string that indicates success or failure
    public static String renumberLineItems(String invoiceName) {

        // create a copy of the target Invoice object and it's Line Items
        Invoice__c invoice =
            [Select i.Name, (Select Name From Line_items__r ORDER BY Name)
             From Invoice__c i
             Where i.Name = :invoiceName LIMIT 1];

        // loop through each Line Item, renumbering as you go
        Integer i = 1;
        for (Line_item__c item : invoice.Line_items__r) {
            item.Name = String.valueOf(i);
            System.debug(item.Name);
            i++;
        }

        // update the Line Items in one transaction, rollback if any problems
        // and return error messages to the calling environment
        try {
            database.update(invoice.Line_items__r);
        } catch (DmlException e) {
            return e.getMessage();
        }

        // on success, return a message to the calling program
        return 'Line items renumbered successfully.';
    }
}
1

1 Answers

1
votes

The error is being thrown because your SOQL query isn't returning any values. SOQL always returns a list of objects and it will only properly be assigned to a single variable if only exactly one is returned. If a SOQL query results in zero results or more than one result, your queries will fail. You have to use List instead.

public class InvoiceUtilities {
        // class method to renumber Line Items for a given Invoice number
        // returns a string that indicates success or failure
        public static String renumberLineItems(String invoiceName) {
            // create a copy of the target Invoice object and it's Line Items
            List<Invoice__c> invoices = [SELECT i.Name,
                                                (SELECT Name 
                                                 FROM Line_items__r 
                                                 ORDER BY Name)
                                        FROM Invoice__c i
                                        WHERE i.Name = :invoiceName LIMIT 1];
            if ( !invoices.isEmpty()) {
                Invoice__c invoice = invoices[0];
                // loop through each Line Item, renumbering as you go
                Integer i = 1;
                for (Line_item__c item : invoice.Line_items__r) {
                    item.Name = String.valueOf(i);
                    System.debug(item.Name);
                    i++;
                }
                // update the Line Items in one transaction, rollback if any problems
                // and return error messages to the calling environment
                try {
                    database.update(invoice.Line_items__r);
                } catch (DmlException e) {
                    return e.getMessage();
                }
                // on success, return a message to the calling program
                return 'Line items renumbered successfully.';
            } else {
                return 'Records for renumbering were not found';
            }
        }
    }