Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

How to invoke sling model from dialog?

Avatar

Level 4

Hi,

My requirement is to generate a custom unique ID and save it in JCR on submission of dialog properties in "/content/dam". 

I am planning to add a hidden field and write the logic in java for the same. But, how to invoke this model on submission of dialog without html?

 

Thanks and regards,

Vijaya Kumar A

1 Accepted Solution

Avatar

Correct answer by
Level 3
6 Replies

Avatar

Community Advisor

Hi @vjleo94 

Your problem is not very clear. Could you please try to explain it in detail? Which dialog you are trying to imply in /content/dam? 

Is your requirement to create a unique component ID for a component and save it into JCR? If that is the case then you don't need to add a field in dialog until you want to see it in the dialog as well(which can be easily done by making the field disabled and giving the same name which you are setting from the backend then it would be prepopulated on page load), You can write your logic in Sling Model of your component and use ModifiableValueMap to save it into JCR.

Regards,

Arpit

Avatar

Community Advisor

Hi @vjleo94 ,

 

The requirement is not clear but I hope you understand that /content/dam is the path where we keep assets and, /content/<project_name> is where we keep content pages (html, where we configure component). Now, your requirement is to trigger Sling Model from the component without HTL, but why? 

 

Sling Model is used to map the POJOs  from Sling objects/resources and then use it on HTML/view.

 

If you just want to save something on JCR, then you can simply use Sling servletOSGI Service for the same. 

 

In below video I have explained how can we trigger resource based sling servlet from script-

https://www.youtube.com/watch?v=iQu65qNN428&list=PLzwWOJSdKWBf7zLga8P7-ARxFQRwGGd94&index=6

 

Thanks,

Ritesh Mittal

 

 

Avatar

Level 4

Hi @Ritesh_Mittal and @ArpitVarshney ,

Thanks for the reply.

Firstly, it is not the component.

 

I have a custom metadata created dialog and added as properties for the dam assets. However, as part of this metadata I want to generate an unique ID and save it. 

 

@Ritesh_Mittal , From your reply I see we can do it via the script by invoking the servlet. I will try it.

 

Thanks and regards,

Vijaya Kumar A

Avatar

Community Advisor

you can do it with javascript in the dialog on load event(if dialog is opening first time) and add keep value in the field either readonly or hidden.



Arun Patidar

Avatar

Correct answer by
Level 3

You can add a id on submission of the dialog with a PostProcessor: 

 

https://sling.apache.org/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-po...