function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pjaenick.ax736pjaenick.ax736 

Argument 1 cannot be null: External entry point

Ok,

Looking for some shorthand on setting a value to zero if null in an assignment statement....

I have this :

    for(Account A: Accts) {
        MyObject__c MO = new MyObject__c(
           ...,
           MO.MyField__c = A.FieldA__c + A.FieldB__c //Errors when either FieldA or B is Null
        );
        MOsToInsert.add(MO);
    }

MyField, and Account fields FieldA and FieldB are all integers.  The above, of course, fails if either FieldA or FieldB is Null.  My sloppy work-around is to define variables for FieldA and FieldB, swapping in a zero if either is null, (requires about 10 additional lines of code).  I know there must be an easier way - perhaps an inline edit to this line to handle the null condition?  ::

(I thought this would work, but this fails too... perhaps my syntax is off?)

           MO.MyField__c = A.FieldA__c == null?0:A.FieldA__c + A.FieldB__c == null? 0:A.FieldB__c

Thanks for any assistance!

Pete

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

You could write a method to handle this but to do it inline:

 

MO.MyField__c = ((A.FieldA__c ==Null) ? 0 :A.FieldA__c) + ((A.FieldB__c == Null) ? 0 : A.FieldB__c);

All Answers

Starz26Starz26

You could write a method to handle this but to do it inline:

 

MO.MyField__c = ((A.FieldA__c ==Null) ? 0 :A.FieldA__c) + ((A.FieldB__c == Null) ? 0 : A.FieldB__c);

This was selected as the best answer
pjaenick.ax736pjaenick.ax736

Thanks Starz26!  Did the trick!

 

Is this notation documented anyplace?

 

Thanks,

Pete

mauricio.ramos@corpitalmauricio.ramos@corpital

Hi,

 

Don't know if you have found your answer but I will detail the syntax below:

 

basically the field is adding together the result of two inline IF conditionals:

 

MO.MyField__c = ((A.FieldA__c ==Null) ? 0 :A.FieldA__c) + ((A.FieldB__c == Null) ? 0 : A.FieldB__c);

 

This is the first IF:      (A.FieldA__c ==Null) ? 0 :A.FieldA__c

The same happens with the other IF and then they are added together and assigned to MO.MyField__c

 

to write inline ifs you do as follows:

 

[Variable to be assigned a value] = [conditional statement] ? [value if true] : [value if false];

 

 

 

 

 

pjaenick.ax736pjaenick.ax736

Thanks !  Much appreciated!

 

Pete