Why I can't had the publish date of a page in stage (QA) environment? | Community
Skip to main content
Level 4
April 16, 2024
Solved

Why I can't had the publish date of a page in stage (QA) environment?

  • April 16, 2024
  • 4 replies
  • 1428 views

Hello, what happens is that I have a component that has its own java sling model, what I need is to get the date of publication of the page (that is, the date on which you create the page and click the publish button).

I already have the code and the logic ready.

I have tested this code in localhost and in dev environment, and in both it works perfectly, but when I deploy it to stage (QA) it doesn't work, it doesn't bring me the value of the publish date.


This is the code I use:

@ScriptVariable private Page currentPage;
@Inject @Named("jcr:created") private Date original_created_page_date;
 
    public String getPublishDate() {
        String dates = "";

        try {
             //  Get the publish date of the page
            ValueMap pageProperties = currentPage.getProperties();
            Date lastReplicated = pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED, Date.class);
            dates = new SimpleDateFormat("yyyy-MM-dd").format(lastReplicated);
        } catch (Exception e) {
             // If fails, return the created page date (the date of the page when was created even if is published or not)
            dates = new SimpleDateFormat("yyyy-MM-dd").format(original_created_page_date);
        }

        return dates;
    }
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Raja_Reddy

Hi @aaron_dempwolff 

It's possible that the issue is related to the replication process. In some environments, the replication process may take longer to complete, which could cause the lastReplicated value to be null.

You could try checking if the page is activated before getting the lastReplicated value. 

@ScriptVariable private Page currentPage; @Inject @Named("jcr:created") private Date original_created_page_date; public String getPublishDate() { String dates = ""; try { // Check if the page is activated if (currentPage != null && currentPage.isActivated()) { // Get the publish date of the page ValueMap pageProperties = currentPage.getProperties(); Date lastReplicated = pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED, Date.class); dates = new SimpleDateFormat("yyyy-MM-dd").format(lastReplicated); } } catch (Exception e) { // If fails, return the created page date (the date of the page when was created even if is published or not) dates = new SimpleDateFormat("yyyy-MM-dd").format(original_created_page_date); } return dates; }

If this doesn't solve the issue, it's possible that there's a configuration difference between your environments that's causing the problem.

4 replies

sravs
Community Advisor
Community Advisor
April 16, 2024

@aaron_dempwolff , please make sure that the page is published on Stage environment.

Level 4
April 16, 2024

I tried but even if I republish the page it's still doesn't bringing the published date page

joerghoh
Adobe Employee
Adobe Employee
April 26, 2024

the order of actions on author is like this:

* replicate the content

* Update the last_replicated* properties

 

That means, that this value will always be off.

Raja_Reddy
Community Advisor
Raja_ReddyCommunity AdvisorAccepted solution
Community Advisor
April 16, 2024

Hi @aaron_dempwolff 

It's possible that the issue is related to the replication process. In some environments, the replication process may take longer to complete, which could cause the lastReplicated value to be null.

You could try checking if the page is activated before getting the lastReplicated value. 

@ScriptVariable private Page currentPage; @Inject @Named("jcr:created") private Date original_created_page_date; public String getPublishDate() { String dates = ""; try { // Check if the page is activated if (currentPage != null && currentPage.isActivated()) { // Get the publish date of the page ValueMap pageProperties = currentPage.getProperties(); Date lastReplicated = pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED, Date.class); dates = new SimpleDateFormat("yyyy-MM-dd").format(lastReplicated); } } catch (Exception e) { // If fails, return the created page date (the date of the page when was created even if is published or not) dates = new SimpleDateFormat("yyyy-MM-dd").format(original_created_page_date); } return dates; }

If this doesn't solve the issue, it's possible that there's a configuration difference between your environments that's causing the problem.

Level 4
April 16, 2024

Hello, I tried the code but when I build it I have the next error:

It says that currentPage doesn't have the method is Activated.

Is there's an alternative for that method or a package that have that method?

I'm using this one:

import com.day.cq.wcm.api.Page;
sravs
Community Advisor
Community Advisor
April 16, 2024

hi @aaron_dempwolff , Try with below snippet

public String getPublishedDate() {
String dates = "";
ValueMap pageProperties = currentPage.getProperties();
if(null != pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED)) {
Date lastReplicated = pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED,
Date.class);
dates = new SimpleDateFormat("yyyy-MM-dd").format(lastReplicated);
}else {
dates = new SimpleDateFormat("yyyy-MM-dd").format(original_created_page_date);
}
}
return dates;
}
Sady_Rifat
Community Advisor
Community Advisor
April 17, 2024

Hello @aaron_dempwolff ,

In your stage environment, I hope you checked the targeted page "jcr:content" node has property "cq:lastReplicated".?

Level 4
April 17, 2024

Sadly I don't have permissions to enter in the crx of stage and prod, only I have permission to do it in localhost and dev env, however, now I check that even in dev is not working, because I make a republish (which make the publish date to update) and don't takes the new publish date, this is the structure of the data:

The page is name how to maximize, and have a jcr:content node, now inside that node is the value cq:lastReplicated that for what I investigate, is the value that have the last date when the page was published

 

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
April 18, 2024

@aaron_dempwolff Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Esteban Bustamante