cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Return PK after saving a model

0 Likes
1,059

Hello,

Is there a way to get the PK of the model which is saved, e.g in below code I want the PK of address since address does not have a 'code' or 'uid'

 modelService.save(billingAddressModel);

How can I get the PK of billingAddressModel?

Accepted Solutions (1)

Accepted Solutions (1)

arvind-kumar_avinash
Active Contributor
0 Likes

It is billingAddressModel.getPk() e.g. please test the following groovy code:

 p = modelService.create('Product')
 p.code = "12345"
 p.catalogVersion = catalogVersionService.getCatalogVersion('Default', 'Staged')
 modelService.save(p)
 print p.getPk();
arvind-kumar_avinash
Active Contributor
0 Likes

Hi - did it help?

0 Likes

Hi Arvind, yup it did, thanks. So basically hybris creates a pk value even before modelService.save() method is called.

arvind-kumar_avinash
Active Contributor
0 Likes

You are most welcome.

So basically hybris creates a pk value even before modelService.save() method is called.

No.

Please check the comment given below:

 p = modelService.create('Product')
 p.code = "12345"
 p.catalogVersion = catalogVersionService.getCatalogVersion('Default', 'Staged')
 println p.getPk(); // returns null
 modelService.save(p)
 println p.getPk(); // returns the PK e.g. 8796355264513

Answers (1)

Answers (1)

agrabovskis
Participant
0 Likes
 billingAddressModel.getPk()