Expand my Community achievements bar.

SOLVED

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

Avatar

Level 5

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;
    }
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.



View solution in original post

10 Replies

Avatar

Community Advisor

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

Avatar

Level 5

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

Avatar

Employee Advisor

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.

Avatar

Correct answer by
Community Advisor

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.



Avatar

Level 5

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

Aaron_Dempwolff_0-1713284157221.png

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;

Avatar

Community Advisor

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;
}

Avatar

Level 5

I added this code but only brings the original created page date, isn't bringing the published date

Avatar

Community Advisor

Hello @Aaron_Dempwolff ,

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

Avatar

Level 5

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

Aaron_Dempwolff_0-1713364237086.png

 

Avatar

Community Advisor

@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