0

I am getting this error when my FOR loop goes to the next row (I am writing a CSV parser). All executes fine for the first row, then it get's to this line and crashes:

ftoHeader = headers[colIndex].trim();

Thanks in ADVANCE for any help!

Full Class:

public static list<sObject> csvTosObject(List<List<String>> parsedCSV, string objectType){

    System.debug ('================================================================================== FTOEmailHandler Starting csvTosObject ');

    Schema.sObjectType objectDef = Schema.getGlobalDescribe().get(objectType).getDescribe().getSObjectType();
    system.debug('================================================================================== You want me to make a '+objectDef);

    list<sObject> objectsToAdd = new list<sObject>();
    list<sObject> objectsToUpdate = new list<sObject>();
    list<list<sObject>> objectsLists = new list<list<sObject>>();
    list<FTO__c> ftoEntries = new list<FTO__c>();
    list<string> headers = new list<string>();

    list<Contact> importContacts = new List<Contact>();
    list<Lead> importLeads = new List<Lead>();

    sObject thisObj = objectDef.newSobject();
    FTO__c thisFTOEntry = new FTO__c();

    integer rowNumber = 0;
    integer rowCount = 0;
    integer colIndex = 0;
    integer ccount = 0;
    integer lcount = 0;

    string headerName = '';
    string headerFTOName = '';
    string ftoHeader = '';

    string entryEmail = '';
    string entryFirstName = '';
    string entryLastName = '';
    string entryMailingStreet = '';
    string entryMailingCity = '';
    string entryMailingState = '';
    string entryMailingPostalCode = '';
    string entryTitle = '';
    string entryCompanyName = '';
    string entryNote = '';
    string entryPhone = ''; 
    string entryFax = '';
    string entryJob_Function = '';
    string entryIndustry = '';


    for(list<string> row : parsedCSV)
    {
        for(string col : row)
        {
            headers.add(col);
        }
        system.debug('================================================================================== File Column Headers');
        system.debug('=================================================================================='+headers);
        break;
    }// End For to get cols

    for(list<string> row : parsedCSV)
    {
        if(rowNumber == 0)
        {
            rowNumber++;
            continue;
        }
        else
        {
            system.debug('================================================================================== Row Index: ' + rowNumber);
            for(string col : row)
            {                   
                headerName = 'NO';
                headerFTOName = 'NO';
                ftoHeader = headers[colIndex].trim();   

                system.debug('================================================================================== Column Name ' + ftoHeader);                

                if(ftoHeader.length() > 0){

                    if(ftoHeader == 'ReferenceNumber'){
                        headerFTOName = 'FTO_Tracking_Number__c';

                    }else if(ftoHeader == 'SubmitDate'){
                        headerFTOName = 'FTO_Date_Received__c';

                    }else if(ftoHeader == 'FirstName'){
                        headerName = 'FirstName';

                    }else if(ftoHeader == 'LastName'){
                        headerName = 'LastName';

                    }else if(ftoHeader == 'JobFunction'){
                        headerName = 'Job_Function__c';

                    }else if(ftoHeader == 'JobTitle'){
                        headerName = 'Title';

                    }else if(ftoHeader == 'CompanyName'){
                        headerName = 'Company';

                    }else if(ftoHeader == 'Email'){
                        headerName = 'Email';

                    }else if(ftoHeader == 'Phone'){
                        headerName = 'Phone';

                    }else if(ftoHeader == 'Fax'){
                        headerName = 'Fax';

                    }else if(ftoHeader == 'Address'){
                        headerName = 'MailingStreet';

                    }else if(ftoHeader == 'City'){
                        headerName = 'MailingCity';

                    }else if(ftoHeader == 'State'){
                        headerName = 'MailingState';

                    }else if(ftoHeader == 'Country'){
                        headerName = 'MailingCountry';

                    }else if(ftoHeader == 'ZipCode'){
                        headerName = 'MailingPostalCode';

                    }else if(ftoHeader == 'RequestType'){
                        headerFTOName = 'FTO_Inquiry_Type__c';

                    }else if(ftoHeader == 'ProductDescription'){
                        headerFTOName = 'FTO_Product__c';

                    }else if(ftoHeader == 'RequestSpecifics'){
                        headerFTOName = 'FTO_Specifics__c';

                    }else if(ftoHeader == 'PreferredContactMethod'){
                        headerFTOName = 'Preferred_Contact_Method__c';

                    }else if(ftoHeader == 'Industry'){
                        headerName = 'Industry';

                    }else if(ftoHeader == 'ProductDescription'){
                        headerFTOName = 'FTO_Content__c';

                    }else if(ftoHeader == 'NoteToSupplier'){
                        headerFTOName = 'FTO_Note__c';

                    }else{
                        system.debug('================================================================================== Skipping column '+ftoHeader);
                    }                   

                    if( headerName != 'NO'){
                        //Add Field to object for Lead/Contact
                        system.debug('================================================================================== Adding to Object for insert/update on Lead/Contact');
                        try {                       
                            thisObj.put(headerName,col.trim());
                            system.debug('=================================================================================='+ftoHeader+' column mapped to ' + headerName);
                        }
                        catch(exception e){
                            system.debug('================================================================================== No corresponding SFDC field specified in header');                           
                        }
                    }

                    if( headerFTOName != 'NO' ){
                        //Add Field to object for FTO Entry
                        system.debug('================================================================================== Adding to FTO Object for insert');
                        try {                       
                            thisFTOEntry.put(headerFTOName,col.trim());
                            system.debug('=================================================================================='+ftoHeader+' column mapped to ' + headerFTOName);
                        }
                        catch(exception e){
                            system.debug('================================================================================== No corresponding SFDC field specified in header');                           
                        }
                    }

                    colIndex++;
                }
            } 

            //*********************************************************************
            // Set some Vars for later use
            //*********************************************************************
            entryEmail              = String.valueOf( thisObj.get('Email') );
            entryCompanyName        = String.valueOf( thisObj.get('Company') );
            entryNote               = String.valueOf( thisObj.get('FTO_Note__c') );
            entryFirstName          = String.valueOf( thisObj.get('FirstName') );
            entryLastName           = String.valueOf( thisObj.get('LastName') );
            entryMailingStreet      = String.valueOf( thisObj.get('Street') );
            entryMailingCity        = String.valueOf( thisObj.get('City') );
            entryMailingState       = String.valueOf( thisObj.get('State') );
            entryMailingPostalCode  = String.valueOf( thisObj.get('PostalCode') );
            entryTitle              = String.valueOf( thisObj.get('Title') );
            entryPhone              = String.valueOf( thisObj.get('Phone') );
            entryFax                = String.valueOf( thisObj.get('Fax') );
            entryIndustry           = String.valueOf( thisObj.get('Industry') );
            entryJob_Function       = String.valueOf( thisObj.get('Job_Function__c') );

            // check for a matching Lead in the DB based on lastname + email
            try{

                try{
                importContacts = [SELECT Id 
                                 FROM Contact 
                                 WHERE email = :entryEmail
                                 LIMIT 1];
                ccount = importContacts.size();

                }catch (QueryException e){
                    ccount = 0;                     
                }

                try{
                importLeads = [SELECT Id 
                              FROM Lead 
                              WHERE email = :entryEmail
                              LIMIT 1];
                lcount = importLeads.size();
                }catch (QueryException e){
                    lcount = 0; 
                }

                if(ccount > 0){
                    system.debug('================================================================================== Found Exisitng Contact in SFDC');
                    for(Contact c : importContacts){                        

                        //Grab Swyft Object Info
                        Contact cObj = [SELECT Id 
                        FROM Contact 
                        WHERE id = :c.id];

                        Account aObj = [SELECT Id 
                        FROM Account 
                        WHERE id = :c.account.id];

                        if(entryCompanyName != aObj.Name){
                        system.debug('================================================================================== No Exisitng Account in SFDC');
                            Account aToAdd = new Account();

                            aToAdd.Name                 = entryCompanyName;
                            aToAdd.BillingStreet        = entryMailingStreet;
                            aToAdd.BillingCity          = entryMailingCity;
                            aToAdd.BillingState         = entryMailingState;
                            aToAdd.BillingPostalCode    = entryMailingPostalCode;

                            insert aToAdd;

                            Account adddedAcc = [SELECT Id
                                                FROM Account
                                                WHERE name = :entryCompanyName
                                                LIMIT 1];

                            cObj.Account.id     = adddedAcc.id;

                        }else{
                            system.debug('================================================================================== Found Exisitng Account in SFDC');
                            cObj.Account.id     = aObj.id;

                        }                           

                        //Create Contact to Add
                        cObj.id                 = c.id;
                        //cObj.Email            = entryEmail;
                        cObj.FirstName          = entryFirstName;
                        cObj.LastName           = entryLastName;
                        cObj.MailingStreet      = entryMailingStreet;
                        cObj.MailingCity        = entryMailingCity;
                        cObj.MailingState       = entryMailingState;
                        cObj.MailingPostalCode  = entryMailingPostalCode;
                        cObj.Title              = entryTitle;
                        cObj.Phone              = entryPhone;
                        cObj.Fax                = entryFax;
                        cObj.Industry__c        = entryIndustry;
                        cObj.Job_Function__c    = entryJob_Function;

                        objectsToUpdate.add(cObj);
                        system.debug('================================================================================== Contact Added to Collection for Updating');
                    }
                }else if(lcount > 0){
                    system.debug('================================================================================== Found Exisitng Lead in SFDC');
                    for(Lead l : importLeads){

                        thisObj.put('id', l.id); 
                        thisObj.put('company', entryCompanyName); 
                        thisObj.put('FTO_Note__c', entryNote);

                        objectsToUpdate.add(thisObj);
                        system.debug('==================================================================================Lead Added to Collection for Updating');
                    }
                }else{
                    //make lead 
                    system.debug('================================================================================== Added to Collection for new Object Creation');
                    objectsToAdd.add(thisObj);
                }


            } catch (QueryException qe){
                system.debug('================================================================================== Query EXECPTION no Exisiting');
                if (importContacts.size() == 0 || importLeads.size() == 0 ){
                    system.debug('================================================================================== Added to Collection for new Object Creation');
                    objectsToAdd.add(thisObj);
                }
            }
            system.debug('================================================================================== Row '+rowNumber+' Done!');
            system.debug('================================================================================== Next Row');
            rowNumber++;
            rowCount++;
        }       
    }// End of FOR Parsed CSV rows

    try{
        insert objectsToAdd;
        system.debug('=================================================================================='+rowCount+' Records Inserted');
    }
    catch(exception e){
        System.debug('================================================================================== Insert FAILED');
    }

    try{
        update objectsToUpdate;
        system.debug('=================================================================================='+rowCount+' Records Updated');
    }
    catch(exception e){
        System.debug('================================================================================== Updated FAILED');
    }

    return null;
}

1 Answer 1

2

I think you need to reset colIndex back to 0 each time you process a new row. e.g.

for(list<string> row : parsedCSV)
{
    colIndex = 0;
    if(rowNumber == 0)
    {
1
  • 1
    Richard! BOOOOOOOOOOOOM! I must have moved it out of the FOR loop when doing something else, I put it back, and all is good. Thanks man! I owe you a beer ;)
    – Red2678
    Commented Mar 20, 2013 at 11:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .