2

So here's an interesting one. I've got a 2.1.1 Grails application with straigt-forward domains and controller with default scaffolding. My show() method works just find and retrieves the domain object with def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id) However, when I call the edit() method I get a java.lang.IllegalArgumentException - argument type mismatch on the exact same call def quarterEndAdjustmentInstance = QuarterEndAdjustment.findByID(params.id) I've confirmed the params map is passing in the ID, and I've tried all variations of get(id), get(params), findByID(id), findByID(params) yadayadayada

Here's the form submit in the show.gsp that calls the edit method in the controller:

    <g:form>
        <fieldset class="buttons">
            <g:hiddenField name="id" value="${quarterEndAdjustmentInstance.id}" />
            <g:link class="edit" action="edit" id="${quarterEndAdjustmentInstance.id}"><g:message code="default.button.edit.label" default="Edit" /></g:link> 
            <%-- <g:actionSubmit class="edit" action="edit" value="${message(code: 'default.button.edit.label', default: 'Edit')}"  /> --%>
        </fieldset>
    </g:form>

Here are two closures from my controller. show() works fine, edit() throws the exception.

 def show() 
{   
    //params.each() { key, value -> println "${key} = ${value}" };
    def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id) //here are your inbound params
    if(!quarterEndAdjustmentInstance)
    {
        flash.message = "Quarter End Metric record not found with ${params}"
        redirect(action:"list", params: params)
    }
    else
    {
        quarterEndAdjustmentInstance.setFrName(mriUtils.getCompRecipient(quarterEndAdjustmentInstance.getCompPayeeID()))
        return [quarterEndAdjustmentInstance: quarterEndAdjustmentInstance]
    }
}

def edit() 
{
    def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id)

    if(!quarterEndAdjustmentInstance)
    {
        flash.message = "Quarter End M12 Adjustment not found with ${params}"
        redirect(action:"list", params:params)
    }
    else
    {
        quarterEndAdjustmentInstance.setFrName(mriUtils.getCompRecipient(quarterEndAdjustmentInstance.getCompPayeeID()))
        return [quarterEndAdjustmentInstance: quarterEndAdjustmentInstance]
    }   
}
john_solo
  • 25
  • 1
  • 8
  • To help those who can answer, always include snipped of code which has issues. Please post the code from controller which is causing the issue. And the code which works. – Sudhir N Apr 28 '13 at 09:11

1 Answers1

4

By default, Grails creates a Long attribute for your domain class.

If I'm not mistaken get() is the only method that will transform your String into the required Long. For the others you need to cast to long:

def quarterEndAdjustmentInstance = QuarterEndAdjustment.findByID(params.long('id'))
  • `get(params.id)` works because Grails internally convert from String to Long. –  Apr 26 '13 at 17:39
  • Right, but that is the issue I am facing. I get the `IllegalArgumentExcepton` on `get(params.id)`, `get(id)`, etc. But the exact same call works in other methods in the controller. – john_solo Apr 26 '13 at 18:12
  • I'm accepting this answer because Sergio was correct. Although that what I had in my code all along (using params.id) it was only after several cleans that it started working. – john_solo Apr 28 '13 at 23:42