0

This is my code snippet ,

string.isBlank(distributorId) ? gen.writeNullField(DISTRIBUTORID):gen.writeStringField(DISTRIBUTORID,distributorId);

why it is throwing me error Expression cannot be a statement

Thanks & Regards, Ravi

3
  • What is the return type of writeNullField() and writeStringField()? – TSmith Apr 3 '20 at 9:15
  • WriteNullField() function writes null value to json and writeStringField() writes the string to json if the distributor id is not null thats why i am using ternary operator to check it is null or not – Ravi Teja Apr 3 '20 at 9:16
  • 2
    this can help salesforce.stackexchange.com/questions/155008/… – User6670 Apr 3 '20 at 9:30
1

This is an expression:

string.isBlank(distributorId) ? gen.writeNullField(DISTRIBUTORID):gen.writeStringField(DISTRIBUTORID,distributorId)

whereas this is a statement:

Boolean b = string.isBlank(distributorId) ? gen.writeNullField(DISTRIBUTORID):gen.writeStringField(DISTRIBUTORID,distributorId);

If you don't need the result of string.isBlank(distributorId) in later code, write this as a normal if statement:

if (string.isBlank(distributorId)) gen.writeNullField(DISTRIBUTORID);
else gen.writeStringField(DISTRIBUTORID,distributorId);

or if you prefer:

if (string.isBlank(distributorId)) {
    gen.writeNullField(DISTRIBUTORID);
} else {
    gen.writeStringField(DISTRIBUTORID,distributorId);
}
1
  • One should always prefer the latter, IMO. The former gets very tricky to read when you've got a bunch of them in a row. – sfdcfox Apr 3 '20 at 12:17
0

The below code is an expression

string.isBlank(distributorId) ? gen.writeNullField(DISTRIBUTORID):gen.writeStringField(DISTRIBUTORID,distributorId);

to assign gen implementation can be

if(string.isBlank(distributorId)) gen.writeNullField(DISTRIBUTORID)
else gen.writeStringField(DISTRIBUTORID,distributorId)

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.