Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,300,028
Community Members
 
Community Events
165
Community Groups

How do we move correctly pages using ScriptRunner PageManager and movePageAsChild?

When I move a page I get an error in the log file:

2021-09-22 18:42:29,536 ERROR [http-nio-8090-exec-5] [comalatech.confluence.workflow.DefaultWorkflowActionManager] executeAction Error Processing Workflow Trigger action page: page: new page v.1 (4816912) trigger action: {send-email:user=@Editor in Chief@|subject=Page ready for final approval}
The page @page@ has been approved by [~@user@] and is waiting for your final approval.
{pageactivity}
{send-email} Error Message: h4. Exception ( 391 lines)
{code}
java.lang.NullPointerException: null
at com.comalatech.confluence.workflow.macros.actions.SendEmailMacro.execute(SendEmailMacro.java:107)
at com.atlassian.renderer.v2.macro.ResourceAwareMacroDecorator.execute(ResourceAwareMacroDecorator.java:51)
at com.atlassian.renderer.v2.components.MacroRendererComponent.executeMacro(MacroRendererComponent.java:192)
at com.atlassian.renderer.v2.components.MacroRendererComponent.processMacro(MacroRendererComponent.java:156)
at com.atlassian.renderer.v2.components.MacroRendererComponent.makeMacro(MacroRendererComponent.java:59)
at com.atlassian.renderer.v2.components.WikiContentRendererHandler.handleMacro(WikiContentRendererHandler.java:18)
at com.atlassian.renderer.v2.WikiMarkupParser.makeMacro(WikiMarkupParser.java:120)
at com.atlassian.renderer.v2.WikiMarkupParser.handlePotentialMacro(WikiMarkupParser.java:100)
at com.atlassian.renderer.v2.WikiMarkupParser.parse(WikiMarkupParser.java:60)
at com.atlassian.renderer.v2.components.MacroRendererComponent.render(MacroRendererComponent.java:43)
at com.atlassian.renderer.v2.V2Renderer.render(V2Renderer.java:53)
at com.atlassian.renderer.v2.plugin.PluggableV2Renderer.render(PluggableV2Renderer.java:30)
at com.atlassian.renderer.v2.V2RendererFacade.convertWikiToXHtml(V2RendererFacade.java:57)
at jdk.internal.reflect.GeneratedMethodAccessor1787.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at com.atlassian.spring.interceptors.SpringProfilingInterceptor.invoke(SpringProfilingInterceptor.java:16)
{code}
PageManager pageManager = getComponent(PageManager)
pageManager.movePageAsChild(childPage, newParentPage)

When I do the same using UI interface of confluence I don't get any errors.
I tried to add one more line after parent was changed:

pageManager.refreshPage(childPage)

 but without any luck.

1 answer

1 accepted

1 vote
Answer accepted

Maybe someone finds it useful. 

import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager

import static com.atlassian.sal.api.component.ComponentLocator.getComponent

PageManager pageManager = getComponent(PageManager)

Page childPage = pageManager.getPage(5275745L)
Page newParentPage = pageManager.getPage(4816980L)
Page previousParentPage = childPage.parent

// update list of previousParentPage children
List<Page> updatedListChildren = previousParentPage.children
updatedListChildren.remove(childPage)
previousParentPage.setChildren(updatedListChildren)

// update list of previousParentPage ids
List<Long> updatedListIds = updatedListChildren.collect { page -> page.id }
pageManager.setChildPageOrder(previousParentPage, updatedListIds)

// update list of newParentPage children
childPage.setParentPage(newParentPage)
List<Page> children = newParentPage.children
children.add(childPage)
newParentPage.setChildren(children)

// update list of newParentPage ids
List<Long> childIds = children.collect { page -> page.id }
pageManager.setChildPageOrder(newParentPage, childIds)

// save content and refresh page
pageManager.saveContentEntity(childPage, new DefaultSaveContext(true, true, true))
pageManager.saveContentEntity(newParentPage, new DefaultSaveContext(true, true, true))
pageManager.saveContentEntity(previousParentPage, new DefaultSaveContext(true, true, true))
pageManager.refreshPage(childPage)
pageManager.refreshContentEntity(childPage)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

338 views 2 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you