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
Kalle KalifKalle Kalif 

New record from e-mail - null handling

I need to know how to handle nulls when creating a new record from an incoming e-mail. The email is in plain text, values seperated by newline, like this example:

 

--emailbody below--

3

v2.3

2011-01-01

1500

0

 

 

56

--

 

now i capture each line in one string list, so that mylist[0]='3', mylist[2]='2011-01-01' and so on.

I then make a new record and upsert, like this:

 

MyObject__c r = new MyObject__c(Name = RptName, V_Name__c = Car[0].ID, Email_from__c = email.fromAddress,

//input from string list

Object_ID__c = integer.valueof(mylist[0],
version__c = mylist[1],
Report_Date__c = date.valueof(mylist[2]),
field4__c = integer.valueof(mylist[3]),
field5__c = decimal.valueof(mylist[4]),
field6__c = decimal.valueof(mylist[5]),
field7__c = date.valueof(mylist[6]),

//etc

);

upsert r Name;

return result;

 

my problem is that, in this example, i will get an exception for field6__c and field7__c (554 System.TypeException: Invalid decimal (or invalid date): ) because it cannot take the decimal value of null, which is exactly what lines 5 and 6 in the mail contains.

 

how can i do this in a better way? I want to create the record even if some lines are empty. Almost none of the custom fields in my object are required. So I do not simply want an exception stopping the entire process just because some lines are null.

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma



 

I would suggest you to check null value before converting it to decimal or date

 

you can write like this, If null then assign null otherwise convert to decimal or date as per case.

 

field6__c = (mylist[5] == null)? null : decimal.valueof(mylist[5]);
field7__c = (mylist[6] == null)? null : date.valueof(mylist[6]); 

 

 If you want to give any default value instead of null you can give that also. Just put that value where i put null .

 

field6__c = (mylist[5] == null)? 0.0 : decimal.valueof(mylist[5]);
field7__c = (mylist[6] == null)? Date.today() : date.valueof(mylist[6]); 

 

 

All Answers

Shashikant SharmaShashikant Sharma



 

I would suggest you to check null value before converting it to decimal or date

 

you can write like this, If null then assign null otherwise convert to decimal or date as per case.

 

field6__c = (mylist[5] == null)? null : decimal.valueof(mylist[5]);
field7__c = (mylist[6] == null)? null : date.valueof(mylist[6]); 

 

 If you want to give any default value instead of null you can give that also. Just put that value where i put null .

 

field6__c = (mylist[5] == null)? 0.0 : decimal.valueof(mylist[5]);
field7__c = (mylist[6] == null)? Date.today() : date.valueof(mylist[6]); 

 

 

This was selected as the best answer
Kalle KalifKalle Kalif
Thanks, that works to a large degree. Unfortunately the null is not always recognized, that is, if a line is empty in the email the check for null turns out false. I have tried to trim, for instance (mylist[6].trim() == null)? null : etc but it does not work. Could there be some hidden characters in the email (plain text email that is)? Is there another way i can check for empty lines?
Shashikant SharmaShashikant Sharma

Ty this if null not working then check like this

 

field6__c = (mylist[5] == null ||  String.valueOf(mylist[5]).trim().length() == 0)? 0.0 : decimal.valueof(mylist[5]);

 

 

You have to add String.valueOf(mylist[5]).trim().length() == 0) this as OR condition , It will resolve your issue.

Kalle KalifKalle Kalif
with a slight modification, it has to be Integer.valueof( //length of field) == 0 It works! Thank you very much, you have saved my day sir.