Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:35:40+00:00 2026-06-02T16:35:40+00:00

I have a need to have a method to return Id in case of

  • 0

I have a need to have a method to return Id in case of success and list of errors in case of fail.
ex code snippet:

def save = {
   def errors = []
    if(Employee.save(flush:true)){
      return Employee.id
    }else{
      errors.add("Can't be saved")
      return errors.
    }

   } 

In Service class
ICalling
Employee.save() – .. so how to check if it is error or id that save method returns

Any suggestions around would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:35:43+00:00Added an answer on June 2, 2026 at 4:35 pm

    Don’t do this – even if you can make it somewhat more usable with Groovy, it’s a bad idea. In this case though, there are a few simple solutions. If you’re just passing the Employee instance and saving it in the service method, you don’t need to return anything:

    void save(Employee employee) {
       employee.save(flush:true)
    }
    

    This is because if it’s successful, the id will be set on the instance you passed in, and if not there will be one or more validation errors in the errors property (there’s no need for you to return a generic error message when there are actually useful error messages available).

    For example this would be the code you’d have in a controller calling the service:

    def employee = new Employee(...)
    fooService.save(employee)
    if (employee.hasErrors()) {
       // do something with employee.errors
    }
    else {
       // success - use the id if you need via employee.id
    }
    

    If you want to pass in the data to create and save the new instance and return an Employee (this is the approach I usually take), it’s similar:

    Employee save(String name, int foo, boolean bar, ...) {
       Employee employee = new Employee(name: name, foo: foo, bar: bar, ...)
       employee.save(flush:true)
       return employee
    }
    

    In this second case it’s important to separate the save call and the return, since if there is a validation error save returns null and you want to always return a non-null instance. So do not do this:

    return employee.save(flush:true)
    

    If you separate them you can check the errors and/or the id.

    Also, make sure that you do not use closures in services like you have in your code (def save = { ...). Only methods will be transactional since the Spring transaction handling doesn’t know about Groovy closures – they’re just fields that Groovy calls as if they were methods, but they’re not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code: def some_method(): #i need to get some attrs :( return
I have the following code: switch (objectType) { case ObjectType.UserReview: return MyMethod<UserReview>(); case ObjectType.ProfessionalReview:
I have the following method that returns void and I need to use it
I have a method which returns single word as a String. I need to
Hi I have a helper method, which returns a string, where i need to
I have a method I need to unit test, which as the subject implies,
i have a string and i need a utility method which looks for bad
I have 3 buttons to change language. So I need to make method, where
I have method in a class that I need to make sure is only
I need to call a method I have defined as: -(IBAction)next:(id)sender{ ... } I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.