Home > Back-end >  Google Apps can't get thread by id if the first message of thread was sent by me
Google Apps can't get thread by id if the first message of thread was sent by me

Time:02-05

In google script, use

var threadById = GmailApp.getThreadById(mailID);

is working like a charm, but if the thread was started by me, it returns null.

Does that mean any conversation started by me can't be treated like a thread?

CodePudding user response:

Found the solution though the reason remains unclear.

var threadId = GmailApp.getMessageById(messageId).getThread().getId();

var thread = GmailApp.getMessageById(messageId).getThread();

Above lines get the threadid or thread.

Maybe the thread started by the mailbox owner is treated as message and only messageid is released before calling getTread().

CodePudding user response:

I did some testing and found out that threads started by the mailbox owner have a unique thread ID which is different from the ID of any of its messages.

On the other hand, threads started by someone else have the same ID of the first message that you received.

You can check it out like this:

var yourThreadId;
var firstMessageId = GmailApp.getThreadById(yourThreadId).getMessages()[0].getId();

/* yourThreadID and firstMessageId are different */
var someoneElsesThreadId;
var firstMessageId = GmailApp.getThreadById(someoneElsesThreadId).getMessages()[0].getId();

/* someoneElsesThreadId and firstMessageId are the same */

I'm not sure why it works like this. Maybe it's to quickly figure out if the thread was started by you or someone else since the GmailThread class doesn't seem to have a method to do so.

Either way the solution that you found would cover any scenario since it will return the parent thread even if you use the ID of any of the messages in the chain.

  •  Tags:  
  • Related