You need to sign in to do that
Don't have an account?
Wahib_Idris
554 System.SObjectException: Invalid field FirstName for Account
Error: 554 System.SObjectException: Invalid field FirstName for Account Class.myHandler.handleInboundEmail
I am parsing CSV in email service. My CSV has four columns
FirstName LastName Type AnnualRevenue Test AccountWaa GPO 1000
Below is my code snippet
global class myHandler implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) { Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); System.Debug('Debug :: Mail Checking' + email); Messaging.InboundEmail.TextAttachment[] tAttachments = email.textAttachments; list<Account> lstAccount = new List<Account>(); // list of accounts. String csvbody=''; List<List<String>> allFields = new List<List<String>>(); // list of rows in csv List<String> lines = new List<String>(); // Rows of csv list<String> headers = new List<String>(); // field names List<String> fields = new List<String>(); // list of fields if(tAttachments !=null){ for(Messaging.InboundEmail.TextAttachment btt :tAttachments){ if(btt.filename.endsWith('.csv')){ csvbody = btt.body; // process csv body here System.Debug('Debug ::: CSV as a string' + csvbody); try { lines = csvbody.split('\n'); System.Debug('Debug ::: Split csv ' + lines); System.Debug('Debug ::: Split csv size' + lines.size()); } catch (System.ListException e) { System.debug('Limits exceeded?' + e.getMessage()); } } } integer rowNumber = 0; for(String line : lines) { // check for blank CSV lines (only commas) if (line.replaceAll(',','').trim().length() == 0) break; fields = line.split(',', -1); System.Debug('Debug ::: line after replace :: ' + line); System.Debug('Debug ::: fields csv ' + fields ); System.Debug('Debug ::: fields csv size' + fields.size() ); allFields.add(fields); if(rowNumber == 0) { // for getting the field names. for(list<String> rows : allFields){ for(String header : rows){ headers.add(header); } break; } rowNumber++; continue; } } System.Debug('Debug ::: before removing all fields ' + allFields); System.Debug('Debug ::: after removing all fields ' + allFields); System.Debug('Debug ::: all fields size' + allFields.size()); System.Debug('Debug :: Check account' + headers); System.Debug('Debug :: Check account' + lstAccount); for(Integer i = 1 ; i < lines.size() ; i++){ Account a = new Account(); System.Debug('Debug :: first header value ' + headers.get(0)); System.debug('Debug :: Field Name ::' + headers[0]); System.debug('Debug :: Field Value ::' + allFields[i][0]); //Schema.Account.Name a.put(headers[0] , allFields[i][0]); a.put(headers[1] , allFields[i][1]); a.put(headers[2] , allFields[i][2]); a.put(headers[3] , allFields[i][3]); lstAccount.add(a); } System.Debug('Debug :: Check account' + lstAccount); insert lstAccount; } return result; } }
a.put(headers[0] , allFields[i][0]); // I am getting invalid field 'FirstName' error on this line of code although debug log is showing correct value.
a.put('FirstName', all Fields[i][0]); // this is working perfect
I suspect a blank at the end of the field name. Try this:
All Answers
I suspect a blank at the end of the field name. Try this:
Thanks for the response Ram. This solution worked !
Another quick question :
headers[3] is AnnualRevene field and its value is 1000 in CSV.
a.put(headers[3], String.valueof(allFields[i][3]));
I am getting this error "554 System.SObjectException: Illegal assignment from String to Decimal Class.myHandler.handleInboundEmail: line 125, column 1"
Please help me on this also !
Thanks in advance
Wahib,
Try
a.put(headers[3], Decimal.valueof(allFields[i][3]));
Ram