Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X
How can I create a new document using a template from a trigger? I see the LocalTriggerManager.ScriptServerBean.postNewIssue(java.lang.String)
function which looks like it can be used to create a new issue but it doesn't appear to give the option to use a template. Is there a way to do this?
Solved! Go to Solution.
There isn't a defined "copy node" method but you could accomplish that through the conventional trigger beans. The template items will not have their IDs change so you can hard-code those into the trigger (unless you retire and replace some, then you need to also update the script).
1) Use the getFieldValue() method in the IssueBean to read data from your template document ID.
2) Use the createSegment() method in the IssueDeltaBean to create the new document, supplying the field data from Step 1.
3) Use the createContent() method in the IssueDeltaBean to create the new nodes, supplying the field data from Step 1 and a parent ID of the new document from Step 2.
Hi Nolin,
There is a trigger bean to create a new segment (document) but not from a template like the GUI can. I think you could do this through the API via the "rq branchsegment --nobranch --project=[Doc Project] [Template Item ID]" command but you would need to ensure you have all the mandatory fields filled-in as part of that operation.
As usual, please be careful about using API calls in triggers and use them in Post-triggers only.
I've never called the API in a trigger. Can you point me to an example (calling API in a trigger)? I've used the API extensively with C# though so I'm not completely unfamiliar with it.
I would suggest looking at the trigger bean documentation which exists on the server (http://<hostname>:<port>/documentation/javadocs/triggers/index.html). Scroll down the left menu until you see ScriptEnvironmentBean and select it to bring-up the methods on the right panel. The CreateAPISessionBean class has the warnings and details about using it in triggers.
Here is a simple example of using an API call in a trigger to issue an "im issues" command and query the server. Note that there are no credentials set in this script so it will use the default API user set in is.properties.
var hostname = "server.company.com";
var port = "7001";
var eb = bsf.lookupBean("siEnvironmentBean");
function print(s)
{
Packages.mks.util.Logger.message("DEBUG", 1, s);
}
function main()
{
var cmd = new Packages.com.mks.api.Command("im", "issues");
cmd.addOption(new Packages.com.mks.api.Option("queryDefinition", "(field[Type]=Change Request)"));
executeCommand(cmd);
}
function executeCommand(command)
{
var api = eb.createAPISessionBean();
var response;
try
{
response = api.executeCmd(command);
}
catch (e)
{
print("------------- Error executing command: " + e.toString());
}
}
main();
Is there a way to copy a Node in a trigger? What I'm thinking is I can have a trigger that just reads the template, creates a new Segment, and then creates new Nodes in that new segment that match the template document. I'm thinking this may be simpler (or at least safer) than going the API route. Would it make sense to attempt that scenario before the API?
There isn't a defined "copy node" method but you could accomplish that through the conventional trigger beans. The template items will not have their IDs change so you can hard-code those into the trigger (unless you retire and replace some, then you need to also update the script).
1) Use the getFieldValue() method in the IssueBean to read data from your template document ID.
2) Use the createSegment() method in the IssueDeltaBean to create the new document, supplying the field data from Step 1.
3) Use the createContent() method in the IssueDeltaBean to create the new nodes, supplying the field data from Step 1 and a parent ID of the new document from Step 2.