FrancescoDS
FrancescoDS

Reputation: 995

grails: findallbyidinlist method

In my grails project, I've used the method Object.findAllByIdInList(), passing a list as parameter. The code used is the following:

def allSelectedIds = ReceiptItem.findAllByIdInList(par)

In which the ReceiptItem is a domain class defined as follows:

class Receipt {
    double totalAmount;
    Date releaseDate;
    int vatPercentage;
    int discount;
    Boolean isPayed;
    Boolean isInvoice;
    static belongsTo = [patient:Patient]
    static hasMany = [receiptItems:ReceiptItem]

    static constraints = {
        receiptItems(blank:  false)
        patient(blank: false)
        totalAmount(blank: false)
        vatPercentage(blank: false, nullable: false)
    }
}

and par is the list of ids defined as follows:

def par = params.list("receiptItemsSelected")

receiptItemsSelected is defined in the gsp page into the remoteFunction() as follows:

params: '\'receiptItemsSelected=\' + jQuery(this).val()'

The problem is that the line above throws the following exception:

java.lang.String cannot be cast to java.lang.Long. Stacktrace follows:
Message: java.lang.String cannot be cast to java.lang.Long

I don't understand why it is throwing this exception.

Thanks for your help

Upvotes: 0

Views: 620

Answers (1)

dmahapatro
dmahapatro

Reputation: 50275

Probably list par has ids as String. Generally id for domain objects is stored as Long. Try this instead

ReceiptItem.findAllByIdInList(par*.toLong())

*Also make sure that id represented as string isNumber().

assert !'C'.isNumber()
assert '4'.isNumber()

Upvotes: 1

Related Questions