sudhangi | Posts: 59

Create Package from Template

0 votes
Hi, I have a requirement where I have a template with placeholders for signers. I need to create a new package from this template and replace the signer Placeholders with actual signer info. I referred to the CreatePackageFromTemplateWithReplacingPlaceholderExample but the problem I face is that instead of replacing the existing placeholder it creates a new signer for the package. My requirement will have 2 or more signers so I have to loop through a MAP to get all the signer info. Here is a sample of my code:
public class CreatePackageFromTemplateController {
	Void PackageFromTemplate(Map templateData) {
		SignerBuilder signer;

		String successMessage;
		
		String tempIdString = templateData.getAt("templateId");	
		PackageId tempPackageId = new PackageId(tempIdString);
		
		
		EslClient dssEslClient(/*this will have my URL and API key*/);

		/*Create the Package.*/
		PackageBuilder packageFromTemplate = newPackageNamed(templateData.packageName);
		packageFromTemplate.withSenderInfo(SenderInfoBuilder.newSenderInfo(templateData.senderEmail));

		def signersArray = templateData.signers;

		for (int j = 0; j  signersArray.size(); j++) {
			
			signer = SignerBuilder.newSignerWithEmail(signersArray[j].getAt("signerEmail"))
					.withFirstName(signersArray[j].getAt("signerFirstName"))
					.withLastName(signersArray[j].getAt("signerLastName"))
					.replacing(new Placeholder(signersArray[j].getAt("placeHolder")));

			if ((templateData.containsKey(signersArray[j].getAt("noteToSigner")))||(StringUtils.isNotEmpty(signersArray[j].getAt("noteToSigner")))) {
				signer.withEmailMessage(signersArray[j].getAt("noteToSigner"));
			}
			packageFromTemplate.withSigner(signer);
		}

		try {
			/*The package attribute is set to the organization name using withAttributes() method*/
			DocumentPackage completePackage = packageFromTemplate
					.withSettings(DocumentPackageSettingsBuilder.newDocumentPackageSettings().withInPerson())
					.withAttributes(newDocumentPackageAttributes()
					.withAttribute("orgName", templateData.orgName)
					.build())
					.build();
			
			PackageId packageId = dssEslClient.getTemplateService().createPackageFromTemplate(tempPackageId, completePackage);
			successMessage = "Package created and available in the draft folder."

		}
		catch (Exception e) {
			printStackTrace();
		}
	}
}

harishaidary | Posts: 1812

Reply to: Create Package from Template

0 votes
Hi Sudhangi, Did you create your template through the ui or programmatically using the sdk?

sudhangi | Posts: 59

Reply to: Create Package from Template

0 votes
I created the template through the UI Thanks, Sudhangi

harishaidary | Posts: 1812

Reply to: Create Package from Template

0 votes
Then you will need to retrieve the id of the placeholders first. Because when you create templates from the UI, eSignLive assigns randomly generated ids. It is not the same as the placeholder name. You could retrieve the placeholder ids this way:
PackageId packageId = new PackageId("ISwwFvZcAJaVGl0rptNxkPA-U6Q="); //id of your template
		
DocumentPackage pack = eslClient.getPackageService().getPackage(packageId);
		
List placeholders = pack.getPlaceholders();
		
for (Signer placeholder : placeholders) {
         System.out.println(placeholder.getId());
}
Once you have the ids, you can do:
.replacing(new Placeholder(PLACEHOLDER_ID))

sudhangi | Posts: 59

Reply to: Create Package from Template

0 votes
Awesome! That works. Thanks, Sudhangi

harishaidary | Posts: 1812

Reply to: Create Package from Template

0 votes
Good to hear :)

detroyer | Posts: 35

Reply to: Create Package from Template

0 votes
I created a template through the UI. How do I then get the package id to retrieve the template? My goal is to take this template as a starting point for further tweaking.

harishaidary | Posts: 1812

Reply to: Create Package from Template

0 votes
You can either retrieve it from the URL by logging to your eSignLive account and browsing to your template or you can retrieve it programmatically:
		int index = 1;
		
		Page templates = eslClient.getPackageService().getTemplates(new PageRequest(index, 50));
		
		while (templates.getNumberOfElements() > 0){
			
			for (DocumentPackage template : templates){
				System.out.println(template.getName() + " " + template.getId());
				index++;
			}
			
			templates = eslClient.getPackageService().getTemplates(new PageRequest(index, 50));
		}

detroyer | Posts: 35

Reply to: Create Package from Template

0 votes
Thank you!

detroyer | Posts: 35

Reply to: Create Package from Template

0 votes
Wouldn't it be easier just to edit the json package template file in a text editor such as NotePad++?

harishaidary | Posts: 1812

Reply to: Create Package from Template

0 votes
There's lots of different ways you can go about it. The only reason I gave you a solution with the Java SDK is because the post is in the Java SDK section =]. If you want to update your template through the REST API, you would make a PUT request to "https://sandbox.esignlive.com/api/packages/{templateId}" with your updated json payload, if you're testing on sandbox for example.

Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off