Hi All,
I have written the below Groovy script for setting up the one of the Sitecore Date Reviewed value in the AEM metadata as Date type. But the issue is that date is storing as String Type , not as Date Type.
************************ Below is the Groovy Script *******************
import java.text.SimpleDateFormat
import java.util.Date
def metadataNode = session.getNode(newTopicPath + "/jcr:content/metadata")
if (metadataNode == null) {
LOG.debug("Metadata node missing for the asset ${newTopicPath}")
return
}
else {
metadataNode.setProperty("displayTitle", displayTitle)
SimpleDateFormat reviewDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'")
def dateSiteCore = "20240213T060000Z" // this date I am getting from Sitecore JSON
def nextReviewDate = parseDate(dateSiteCore, reviewDateFormat)
LOG.debug("NONcpmMetadata reviewedDate == ${nextReviewDate}")
metadataNode.setProperty("mc:nextReviewDate", nextReviewDate)
}
session.save()
// Here the method parseDate below
def parseDate(inputTime, dateFormat) {
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
try {
Date inputDate = dateFormat.parse(inputTime)
return outputFormat.format(inputDate)
} catch (ParseException pe) {
/* Empty Catch Statement, for handling multiple date formats.*/
}
return ""
}
After I run the above Script nextReviewDate value is storing as String Type in the metadata but not as Date Type , as shown below screenshot.
Can anyone Please let me Know if I am missing something.
Thanks & Regards
Sonu
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @SUNITACH1,
In general you have to set property type explicitly in other case it will be store as a String, or as a type of variable you are using as value in setProperty method.
To solve the issue you have two options:
metadataNode.setProperty("mc:nextReviewDate", nextReviewDate)
with this linemetadataNode.setProperty("mc:nextReviewDate", nextReviewDate, PropertyType.DATE)
you may also need to add below import to your scriptimport javax.jcr.PropertyType
Views
Replies
Total Likes
Hi Sunitach,
Your 'parseDate' method is returning 'String' object that is why the property value is being set as String.
Instead, can you return 'Calendar' as mentioned here - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/setting-jcr-property-of-ty...
Thanks,
Raju.
Views
Replies
Total Likes
Hi @SUNITACH1,
In general you have to set property type explicitly in other case it will be store as a String, or as a type of variable you are using as value in setProperty method.
To solve the issue you have two options:
metadataNode.setProperty("mc:nextReviewDate", nextReviewDate)
with this linemetadataNode.setProperty("mc:nextReviewDate", nextReviewDate, PropertyType.DATE)
you may also need to add below import to your scriptimport javax.jcr.PropertyType
Views
Replies
Total Likes
Thanks @lukasz-m and @Rajumuddana
I used both the way and it is coming fine
************************** DATE data Type *******************
import javax.jcr.PropertyType
import java.util.Calendar
import java.util.TimeZone
//by using import javax.jcr.PropertyType
SimpleDateFormat reviewDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'")
def dateSiteCore = "20240213T060000Z" // this date I am getting from Sitecore JSON
def nextReviewDate = parseDate(dateSiteCore, reviewDateFormat)
LOG.debug("NONcpmMetadata reviewedDate == ${nextReviewDate}")
//metadataNode.setProperty("mc:nextReviewDate", nextReviewDate)
metadataNode.setProperty("mc:nextReviewDate", nextReviewDate, PropertyType.DATE)
//by using Calendar
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'")
Date date = inputFormat.parse(dateSiteCore)
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"))
calendar.setTime(date)
//child.setProperty(cpropertyName,calendar);
metadataNode.setProperty("nextReviewDatemetadata", calendar)
Views
Replies
Total Likes