3

I am building a CSV file from an sobject list in a batch class. There are certain fields that are appearing as "NULL". When I am checking them for NULL values using IsBlank , I am getting an error

First argument of ternary must be Boolean: String at line 30 column 25

. If the field is NULL then it must be replaced by empty string '', so I have used ternary to keep it simple. Given below is the code:

string.isBlank(string.valueof(record.get('Business_Partner_Identifier_Code__c'))) ? '':string.valueof(record.get('Business_Partner_Identifier_Code__c')) 

Edit

global Database.QueryLocator start(Database.BatchableContext bc) {

    lines = new String[0];
    //string query = '';
    return Database.getQueryLocator( 'Select Id, Business_Partner_Identifier_Code__c, Last_Credential_Date__c From Account WHERE Business_Partner_Identifier_Code__c = \'FL\' ' );
}
global void execute(Database.BatchableContext bc, SObject[] scope) {
    recordCount += scope.size();
    try{
    for(Sobject record: scope) {
        String line = '';
        // header row
        if ( lines.size() == 0 ) {
            line = 'Id, Business_Partner_Identifier_Code__c, Last_Credential_Date__c';
            lines.add(line);
        }
        else {
            // build your csv line here
            line +=     record.get('Id') == null ? '' : String.valueof(record.get('Id')) +'"'
            +',' +'"' + record.get('Business_Partner_Identifier_Code__c') == null ? '' : String.valueof(record.get('Business_Partner_Identifier_Code__c')) +'"'
            +',' +'"' + record.get('Last_Credential_Date__c') == null ? '' : String.valueof(record.get('Last_Credential_Date__c')) +'"';
            lines.add(line);

            system.debug('Line No >>> ' + line); 
        }
    }
    // end of try block
    }
    catch (exception ex){
        system.debug('exception has occurred at ' + ex.getLineNumber() + ' and message ' + ex.getmessage()); 
    }
}
global void finish(Database.batchablecontext bc) {
    // proceed to send attachment by email

    // Run another batch......

    }
}
6

If the Business_Partner_Identifier_Code__c entry is of type string you can use:

String s = String.isBlank((String) record.get('Business_Partner_Identifier_Code__c'))
        ? ''
        : String.valueOf(record.get('Business_Partner_Identifier_Code__c'));

(One problem with the code you posted is that String.valueOf(null) returns the non-empty string of 'null'.)

If it is of some other type, then you can't use String.isBlank because it expects a String argument and instead would need to use:

String s = record.get('Business_Partner_Identifier_Code__c') == null
        ? ''
        : String.valueof(record.get('Business_Partner_Identifier_Code__c'));

I cannot reproduce the error that you get.

PS

If the record variable is sometimes null you can be more defensive like this:

String s = record == null || record.get('Business_Partner_Identifier_Code__c') == null
        ? ''
        : String.valueof(record.get('Business_Partner_Identifier_Code__c'));
  • Thanks Keith C, the second solution is exactly what I was looking for. Thanks. – Mahmood Nov 19 '14 at 11:43
  • Keith: I am sorry but the solution is not working completely. :( Giving me error that "Attempt to de-reference a null object" when I am executing the batch for every record, this was all working if I dont include the check of NULL existence. any ideas? – Mahmood Nov 19 '14 at 14:44
  • @MahmoodButt See the PS I added; record is the only other thing that could be null on that line. – Keith C Nov 19 '14 at 14:48
  • Record is pointing towards a row/record of scope in batch. And if you mean record is null, it means all the fields of the record have to be null, which is highly unlikely as I'm pulling the "Id" field in the query as well or is there something that I'm unaware of? – Mahmood Nov 19 '14 at 14:52
  • 1
    @MahmoodButt Glad you've figured it out; it'll be easier the next time you arr tracking down problems. – Keith C Nov 20 '14 at 8:59

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.