0

Trying to simplify multifle if-else conditions . Below code is throwing error " Error: Compile Error: Expression cannot be a statement. " . Can you please let me know how to resolve the issue ?

 if (hasFailedBatches) {
   Boolean inDMode = run.Run_Mode__c == 'inDMode' ? true:false;
   inDMode ? input.put('Status', 'Failed'):input.put('Status', 'Retrival fialed');
 }

1 Answer 1

1

While:

inDMode ? input.put('Status', 'Failed'):input.put('Status', 'Retrival fialed');

is legal in JavaScript and other languages, you can't do this in Apex. Instead, you can use the ternary operator as a parameter:

input.put(
  'Status', 
  inDMode? 'Failed', 'Retrieval Failed'
);

Other Notes

You can also use a ternary operator with a compatible set of data types, such as:

(isInput? inputMap: outputMap).put('key','value');

And it's also legal for functions as long as they return the same type:

someVar = someBoolean? functionA(): functionB();

Note that a Boolean operator already returns a Boolean:

Boolean inDMode = run.Run_Mode__c == 'inDMode';
1
  • Thank you so much . You are one of the best salesforce expert .
    – Hemant-2
    Commented Aug 31, 2022 at 6:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .