(422) Unprocessable Entity on template with merge fields

  • 1
  • Question
  • Updated 6 months ago
  • (Edited)
I'm running into an issue when I try to send a document with merge fields in it using the API. This is the API call I'm using - https://api.rightsignature.com/public/v1/reusable_templates/id/send_document

This works great for me with templates that don't have merge fields but when I try to send a document with merge fields, I get a (422) Unprocessable Entity error.

The JSON request my code generates looks like this:
{
  "name": "Test Document Name",
  "message": "Please sign this document ",
  "expires_in": "30",
  "roles": [
    {
      "name": "parent_1",
      "signer_email": "joe.test@example.org",
      "signer_name": "Joe Test"
    },
    {
      "name": "parent_2",
      "signer_email": "jane.test@example.org",
      "signer_name": "Jane Test"
    }
  ],
  "merge_field_values": [
    {
      "id": "207572e0-a08a-45f1-a6c0-0b33be8e471e",
      "value": "parent_2 value"
    },
    {
      "id": "72a0e3c1-f33f-4e03-98fb-62718000c874",
      "value": "parent_2 email value"
    },
    {
      "id": "3e5fdd15-ace3-4fb7-8bec-4aa1a2bcba04",
      "value": "parent_1 email value"
    },
    {
      "id": "c2e88672-a1e5-42d1-9be6-6feab5a3c4fe",
      "value": "LastFirst"
    }
  ]
}



Am I formatting the merge fields correctly?
Thanks!
Photo of Travis Cullitan

Travis Cullitan

  • 6 Posts
  • 0 Reply Likes

Posted 10 months ago

  • 1
Photo of Saravanan Kannan

Saravanan Kannan, Employee

  • 39 Posts
  • 6 Reply Likes
Hi Travis,

One reason I could see we raising the 422 is because if the given role name does not match with role name of the reusable template. Otherwise the formatting of the merge fields looks correct to me.

Thanks
Saravanan
Photo of Travis Cullitan

Travis Cullitan

  • 6 Posts
  • 0 Reply Likes
I figured out my own issue. I didn't know that the ID's of both the document template and the merge fields get changed when you re-save a template in RightSignature. I fixed both sets of ID's and it's working now.

I'm a bit surprised that I didn't get a "Merge field ids do not match the reusable template's merge fields." error.

Thanks!
Photo of Justin Sirpilla

Justin Sirpilla

  • 2 Posts
  • 0 Reply Likes
Where do you find the ID of a merge field? I cannot seem to find it
Photo of Travis Cullitan

Travis Cullitan

  • 6 Posts
  • 0 Reply Likes
Justin,

Each merge field including its name and ID are included within the JSON response for each template. Here is the code I used to keep track of the merge field names and ID's in my C# project. My overall workflow was: 1) get a list of all the templates and guids for our company templates
2) Have a user pick which template they want to use
3) Pass the guid for that template into the function below to get a list of names and ID's for the merge fields
4) Match the names of the merge fields to merge field names from a seperate (Excel) file
5) Merge in the data and send the contract

I hope that helps!
 private void getTemplateMergeFields(string myguid)
        {
            string URL = "https://api.rightsignature.com/public/v1/reusable_templates/"+myguid;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Headers.Add("authorization", "Basic " + api_token);
            request.Method = "GET";

            string jsonResponse;

            using (System.IO.Stream st = request.GetResponse().GetResponseStream())
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(st))
                {
                    jsonResponse = sr.ReadToEnd();
                }
            }

            dynamic stuff = JsonConvert.DeserializeObject(jsonResponse);

            dynamic template = stuff["reusable_template"];
        
            foreach (dynamic t in template["merge_field_components"])
            {
                if (templatemergefields.ContainsKey((string)t.name))
                {
                    //Duplicate merge field in template
                   //Application specific processing
                }
                else
                {
                    templatemergefields.Add((string)t.name, (string)t.id);
                }
            }

        }
Photo of Justin Sirpilla

Justin Sirpilla

  • 2 Posts
  • 0 Reply Likes
This helped, thanks!