Skip to main content
Skip table of contents

Annotate Document

A common ask in approval workflows is to stamp a document approved when the document has completed the process. Live Fields solves this problem by calling the GlobalSearch API to add a new annotation to the active document. This is an advanced example designed to illustrate the power of Live Fields as a feature.

The top of this script includes 4 default parameters that should be changed to fit a customer’s specific environment. The values here must match exactly or results will be unexpected. Unless you are comfortable with authoring and debugging Javascript, a rule of this complexity shouldn’t be altered outside of setting the default parameters.

CODE
//Change these defaults
const approvalStatusField = "Approval Status APWF";  //The name of the list we are checking.
const fieldTriggerValue = "Approved"; //The list value that triggers annotation placement.
const fieldCompleteValue = "Release to Dynamics"; //The list value to change it to on approval.
const stampValue = "Approved"; //The text of the stamp.

const doc = $$inject.properties.document;

if (doc.id === 0) {
  return "Approval flow disabled during import.";
}

const token = $$inject.properties.authToken;
const approvalKey = 'approval-' + doc.hash;
var hasStoredApprovalKey = !!sessionStorage.getItem(approvalKey);
const approvalValue = $$inject.fields[approvalStatusField];

//debugger;
// Clear the session key if the document is not approved, so that it can be reset without a new session.
if (hasStoredApprovalKey && approvalValue !== fieldTriggerValue && approvalValue !== fieldCompleteValue) {
  console.debug("LiveField: Clearing annotated session flag, document is no longer approved.");
  sessionStorage.removeItem(approvalKey);
  hasStoredApprovalKey = false;
}

const needsStamp = !hasStoredApprovalKey && approvalValue == fieldTriggerValue;

if (!needsStamp) {
  console.debug("LiveField: Skipping annotation, document doest not require stamp.");
  return 'Document does not require stamp.';
}

const annotationData = {
  "0": [
      {
          "type": "stamp",
          "x": 816.1113834891682,
          "y": 1565.9614899051453,
          "height": 401.76366322008863,
          "width": 856.0945347119645,
          "rotation": 319,
          "username": "",
          "cornerradius": 0,
          "burn": false,
          "outline": {
              "color": "#f08080",
              "opacity": 1,
              "width": 15
          },
          "fill": {
              "color": "",
              "opacity": 0
          },
          "text": {
              "value": stampValue,
              "align": "left",
              "font": {
                  "bold": true,
                  "italic": false,
                  "strike": false,
                  "underline": false,
                  "color": "#f08080",
                  "family": "Arial",
                  "size": 140
              }
          }
      }
  ],
  "hasBurned": false
}

const xhr = new XMLHttpRequest();
xhr.open("PUT",window.location.origin + "/square9api/api/UpdateDocument/databases/"+doc.databaseId+"/archive/"+doc.archiveId+"/document/"+doc.id+"/AnnotationData?pageCount=1&filename="+doc.fileId, false);
xhr.withCredentials = true;
xhr.setRequestHeader("auth-token", token);
xhr.setRequestHeader("secureid", doc.hash);
xhr.setRequestHeader("session-actions","annotationdata");
xhr.setRequestHeader("session-id","00000000-0000-0000-0000-000000000000");
xhr.setRequestHeader("content-type","application/json;charset=UTF-8");
xhr.setRequestHeader("accept","application/json, text/plain, */*");
xhr.send(JSON.stringify(annotationData));

console.debug("LiveField: Added approval stamp, storing session flag.");
sessionStorage.setItem(approvalKey, true);

// Update the field value to approved and save in 2 seconds.
$$inject.fields[approvalStatusField] = fieldCompleteValue;
console.debug("LiveField: Setting approved and saving.");
setTimeout(()=>{
  // Ensure annotation is loaded to viewer.
  angular.element(document).injector().get('annotationsService').reload();
},0);

return "Approval stamp added. Document Approved.";
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.