Email notification based on advanced condition

leslie8
Mega Expert

I need to make a condition that does NOT send an email notification if the comments contains a certain string of text.

When the comments field contains the text "email client message sent" I do not want the notification to be sent. I have similar rules working for other advanced conditions.

I have this advanced condition but for some reason it is still sending the notification even when the comments contains the phrase "email client message sent".

var comments = current.comments;

  if (comments.indexOf('email client message sent') !== -1) {

  answer = false;

  }

  else {

  answer = true;

  }

1 ACCEPTED SOLUTION

Actually, you are correct.   I should have tested out the "changes" condition (I know it did not work previously - I just tested in Fuji, Patch 4).   So, a notification with the following condition and script should work, assuming we understand the requirements:


____Jim_C_____.png



The script would be:


answer = (current.comments.getJournalEntry(1).toLowerCase().indexOf("email client message sent") == -1);



So like you said, the Condition field will first make sure a comment was added and then the script checks to see if the string is NOT found in the latest comment, setting the answer to "true" which would then trigger the email.   I always like to force a compare on lowercased strings just to be safe.


View solution in original post

13 REPLIES 13

Try:



if (comments.indexOf('email client message sent') > -1{


answer = false;


}


else{


answer = true;


}


Actually, I was doing some testing in my instance. I believe you may need to alter:



var comments = current.comments;




to read:




var comments = current.comments.getJournalEntry(-1);




I will do some testing and edit this post if it works.




EDIT: I believe adding the getJournalEntry should resolve your problem. Additionally, I tested your initial if statement (!== -1), and it works fine as well.


Jim Coyne
Kilo Patron

The problem with journal fields is that the current.comments will always return an empty string in the condition script.   And using getJournalEntry() should not be used because you do not know when the last entry was added.



So that really only leaves you with one choice - you will have to use the "Send when - Event is fired" method and create a Business Rule to evaluate whether the notification should go out or not.   Is so, then raise an event that the notification will react to.


In my example I used getJournalEntry(-1) which would return all comments (current and previous).



If Leslie were to use getJournalEntry(1), it would retrieve only the most recent comment which would then allow for the indexOf statement.



Is there a reason this method would not work, Jim?


As you said, getJournalEntry(1) would retrieve the most recent comment - however, that does not mean the comment on the last update.   It will retrieve the last comment that was added regardless of when it was.



So if you add "hello world" as a Comment, save the record, change the Short description and save again, getJournalEntry(1) would return "hello world" because that was the last Comment made, even though it was not on the last update.




That is why getJournalEntry(1) would not work in this case.   You could be comparing older comments on newer record updates.