-2

Trigger Snippets:

 if (JIRA.currentUserIsNotJiraAgent()) { 
    if(System.isFuture() == false || System.isBatch() == false || 
System.isScheduled() == false) { //Check, If Not in any Async Apex context, only then execute the Future Method.
   JIRAConnectorWebserviceCalloutSync.synchronizeWithJIRAIssue(JIRA.baseUrl, 
   JIRA.systemId, objectType, objectId); //Future Method.
}

Error Messasge:

System.AsyncException: Future method cannot be called from a future or batch method: JIRAConnectorWebserviceCalloutSync.synchronizeWithJIRAIssue

I know where its going wrong checking OR condition causes the issue.Is there any best way to handle this conditions. (I need all these 3 condition,I need to handle it in better way)

  if(System.isFuture() == false || System.isBatch() == false || 
System.isScheduled() == false) 
0

OR (||) means "true if either condition is true." In other words, "FALSE OR FALSE" is "FALSE", and any other combination ("FALSE OR TRUE", "TRUE OR FALSE", "TRUE OR TRUE") is "TRUE".

Accordingly, by your logic, if you're in a future method, you're not in a batch method; FALSE AND TRUE is still TRUE. In fact, your current IF statement will always return a TRUE value because of this.

Instead, you need to use AND (&&), which means "true only if both conditions are true." In other words, "TRUE AND TRUE" is "TRUE", and any other combination ("FALSE AND TRUE", "TRUE AND FALSE", "FALSE AND FALSE") is "FALSE".

However, there is one way to make OR values work correctly this way, by using de Morgan's Laws. We can change "NOT A AND NOT B" to "NOT (A OR B)". The resulting Apex looks like this:

if(!(System.isFuture() || System.isBatch() || System.isScheduled()) ) {

In this manner, if any of the three flags are true, the resulting value will be false, and the branch won't be executed.

As an aside, it's usually considered good form in programming to simply use NOT instead of comparing a Boolean to false. This is because the resulting code is easier to read and requires far less typing:

if(!System.isFuture() && !System.isBatch() && !System.isScheduled() ) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.