尝试执行工作簿顶点代码时出错

时间:2014-02-02 06:43:21

标签: salesforce

尝试执行工作簿顶点代码时出错

  

“System.QueryException:List没有用于赋值给SObject的行:Class.InvoiceUtilities.renumberLineItems:第8行,第1列AnonymousBlock:第1行,第1列AnonymousBlock:第1行,第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 个答案:

答案 0 :(得分:1)

由于您的SOQL查询未返回任何值,因此引发错误。 SOQL总是返回一个对象列表,如果只返回一个,它只能正确地分配给一个变量。如果SOQL查询导致零结果或多个结果,则查询将失败。你必须改用List。

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';
            }
        }
    }