You need to sign in to do that
Don't have an account?
Help comparing String from .csv file to record ID
Hello,
I am trying to update records using apex from a .csv that is uploaded from a VF page. I have the page working correctly, allowing the file to be selected and then passing the .csv through a reader/parser and returning it as a List<List<String>>. However, I am having trouble a getting a specific IF() statement to evaluate correctly. Here is the code:
public
classROIUploadController {
public Blob contentFile { get; set; }
public String nameFile { get; set; }
public Integer rowCount { get; set; }
public Integer colCount { get; set; }
publicvoidupdateRecordsWithROICSV(){
List<List<String>> attachedCSV = getResults(); <---- getResults() is standard CSV method that passes back the uploaded .csv as a List<List<String>>
if(attachedCSV != null){
//get and update Opportunity Data
Id oppID = (ApexPages.currentPage().getParameters().get('id'));
Opportunity Opp = [Select Id FromOpportunityWHEREId = :oppID];
List<String> oppDataRow = attachedCSV.get(2);
If(oppDataRow.get(0) == Opp.ID){
Opp.amount = decimal.ValueOf(oppDataRow.get(8));
database.update(Opp);
}
}
}
I have tested the assignment of the "amount" and update outside of the IF() and it works just fine. I have also made sure that I am referencing the correct cell and it is the cell that contains the ID of the Opp in question. However, when I test the above no update is made (meaing the IF = False). What am I doing wrong, what do I need to do to get this to evaluate to true (when the correct ID is in that cell in the .csv)
Thanks in advance for the help.
Jeremy Stender
You could read oppID to a String not an Id?....
what results do various dumps via system.debug suggest.
Try assigning the compare to a boolean and display that value.
As I understand it, you must also retrieve amount in order to change its value.
Sorry, this is the statement that I can't get to evaluate as true:
If(oppDataRow.get(0) == Opp.ID){
The Amount assignment and update is working just fine, but I need to make sure that the ID listed in the CSV is actually the ID for the Opp they are trying to update with the .csv file. Again, that is the correct cell for where the Opp ID is being stored in the .csv.
I made the adjustments below and still no luck in getting the IF() to evaluate as true:
String currentOppIDString = Opp.Id;
If(oppDataRow.get(0) == currentOppIDString){
Okay try
String id = Opp.Id;
String c0 = (String)oppDataRow.get(0); // or perhaps oppDataRow[0] ?
boolean b = ( id == c0 );
system.debug('id='+id);
system.debug('c0='+c0);
system.debug('b='+b);
and see what it looks like in the debug log ?
I ran what you suggested, and the results are very confusing:
id=006W0000002Z7dTIAS
c0= 006W0000002Z7dTIAS
b=false
that had me REALLY confused until I noticed that space at the beginning of c0 (which is coming from the attached csv.) I made sure that the .csv was not the problem (no leading space in it), so I changed the definition of the id variable to be
string id = ' ' + Opp.id
and still got a false:
id= 006W0000002Z7dTIAS
c0= 006W0000002Z7dTIAS
b=false
I might be even more confused than before actually. Is the leading space the issue? And if so why the heck is it still throwing a false when I accounted for it in the test?
Is it really a space or some other non printing character?
Try sanitising the csv data rather than fudging the "good" data to match ?
Also try
system.debug('c0=['+c0+']');
to make sure there is no stuff on the end.
If all else fails, move on to pos('006') and substring() !
You may want to try comparing Id with Id, not String with Id like you are doing now.
ex:
Finally, I was able to get this to work by passing the csv ID as an ID with the trim() method at the end of the get() method. FYI here is what worked
ID c0 = oppDataRow.get(0).trim();
Thanks for all the help!