Skip to main content

Gmail - Gmail - Automatically File Caresquare Ingest Replies

Two Gmail setups — archive successfully processed replies today, or file CS- status codes into separate labels once unsuccessful ingest replies are on.

This guide uses Google Apps Script to file Caresquare replies out of the main inbox. Which script you use depends on whether Caresquare has enabled unsuccessful ingest replies for your organisation.

  • Unsuccessful replies off (default today): you only receive a reply when an invoice is ingested. The body says Successfully processed. There is no CS-PROCESSED code. Use Section A.

  • Unsuccessful replies on: every outcome gets a reply. The first line is a status code (CS-PROCESSED, CS-DUPLICATE, CS-NON-INVOICE, or CS-FAILED). Use Section B.

If you are not sure which you have, look at a recent Caresquare reply. If the first line is a CS-… code, use Section B. If it only says Successfully processed, use Section A.

Both scripts only look at unread emails from [email protected].


Steps that are the same for both sections

Open Google Apps Script

Navigate to Google Scripts and click New project.

Save the project

After pasting a script, click the Save project icon (💾) next to Run. Name it something like Caresquare Inbox Management.

(Optional) Process existing emails

  1. Click Run.

  2. The first time, Google will ask you to authorise the script. Grant the Gmail permissions, then click Run again.

Create an automatic trigger

  1. Click Triggers in the left-hand sidebar.

  2. Click Add Trigger.

  3. Use these settings:

Setting

Value

Choose which function to run

moveCaresquareProcessed

Deployment

Head

Event source

Time-driven

Type of time based trigger

Minutes timer

Select minute interval

Every minute

Click Save.

If you later turn unsuccessful replies on, delete this trigger and replace the script with Section B, then add the trigger again.


Section A — Unsuccessful ingest replies are off

Use this section if Caresquare has not enabled unsuccessful ingest replies. You will not see CS-PROCESSED or any other CS-… code. Success replies only contain the text Successfully processed. Duplicates, non-invoices, and processing errors do not get a reply, so there is nothing extra to file.

This script labels those success replies Caresquare Processed, marks them as read, and archives them out of the main inbox.

Delete any existing code in the editor and paste:

function moveCaresquareProcessed() {
var sender = '[email protected]';

var labelName = 'Caresquare Processed';
var label = GmailApp.getUserLabelByName(labelName) || GmailApp.createLabel(labelName);

var threads = GmailApp.search('from:' + sender + ' is:unread');

threads.forEach(function (thread) {
var messages = thread.getMessages();
var lastMsg = messages[messages.length - 1];

if (!lastMsg.isUnread()) return;

var body = lastMsg.getPlainBody() || lastMsg.getBody();
if (body.indexOf('Successfully processed') !== -1) {
thread.addLabel(label);
thread.markRead();
thread.moveToArchive();
}
});
}

Then follow the shared save, run, and trigger steps above.


Section B — Unsuccessful ingest replies are on

Use this section only after Caresquare has enabled unsuccessful ingest replies for your organisation. In that mode, every outcome gets a reply, and the first line is a status code — including CS-PROCESSED on successful invoices. That code is not added until this option is turned on.

File each outcome under its own label, then archive it out of the main inbox so staff work from the labels:

First line

Meaning

Label

CS-PROCESSED

The document was ingested as an invoice

Caresquare Processed

CS-DUPLICATE

Already in Caresquare

Caresquare Duplicate

CS-NON-INVOICE

Not processed as an invoice (a Reason: line follows)

Caresquare Non-Invoice

CS-FAILED

Could not be read, or a processing error

Caresquare Failed

The script still accepts Successfully processed as a fallback so any older success replies (from before the option was turned on) are filed as processed.

Use Failed uploads in Caresquare for more detail on unsuccessful ingestions.

Delete any existing code in the editor and paste:

function moveCaresquareProcessed() {
var sender = '[email protected]';

function getLabel(name) {
return GmailApp.getUserLabelByName(name) || GmailApp.createLabel(name);
}

var labelProcessed = getLabel('Caresquare Processed');
var labelDuplicate = getLabel('Caresquare Duplicate');
var labelNonInvoice = getLabel('Caresquare Non-Invoice');
var labelFailed = getLabel('Caresquare Failed');

var threads = GmailApp.search('from:' + sender + ' is:unread');

threads.forEach(function (thread) {
var messages = thread.getMessages();
var lastMsg = messages[messages.length - 1];

if (!lastMsg.isUnread()) return;

var body = lastMsg.getPlainBody() || lastMsg.getBody();
var label = null;

if (body.indexOf('CS-PROCESSED') !== -1 || body.indexOf('Successfully processed') !== -1) {
label = labelProcessed;
} else if (body.indexOf('CS-DUPLICATE') !== -1) {
label = labelDuplicate;
} else if (body.indexOf('CS-NON-INVOICE') !== -1) {
label = labelNonInvoice;
} else if (body.indexOf('CS-FAILED') !== -1) {
label = labelFailed;
}

if (!label) return;

thread.addLabel(label);
thread.markRead();
thread.moveToArchive();
});
}

Then follow the shared save, run, and trigger steps above.


Frequently Asked Questions

Which section should I use?

Open a recent reply from [email protected]. If the first line is CS-PROCESSED (or another CS-… code), use Section B. If the body only says Successfully processed, use Section A. CS-PROCESSED is not added until unsuccessful ingest replies are enabled.

Will this delete my emails?

No. Emails are only archived out of the main inbox. Open the matching Caresquare label, or All Mail, to find them.

Can I undo this later?

Yes. Remove the trigger or delete the Apps Script project. You can drag labelled threads back to the inbox at any time.

Does this affect other emails?

No. The script only checks unread emails from [email protected].

What if we turn unsuccessful replies on later?

Replace the Section A script with Section B and keep the same trigger function name (moveCaresquareProcessed). Existing Caresquare Processed labels can stay; the new script adds Duplicate, Non-Invoice, and Failed labels when it first runs.

Did this answer your question?