• Suman Deep
  • NEWBIE
  • 10 Points
  • Member since 2014
  • Salesforce Admin
  • Shipwire

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies
Hi All. 
I am trying to add a loop that checks for blank values and assign 0 to them as my import is not allowing blank cells in csv.
Any help is greatly appreciated.

Error: System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!updateForecastItems}' in component <apex:commandButton> in page forecastimportcsvpage: Class.ForecastImportFromCSVController.updateForecastItems: line 34, column 1


public class ForecastImportFromCSVController {
    public Blob csvFileBody { get; set; }
    public string csvFileName { get; set; }
    public List<forecast_items__c> fcitemslist { get; set; }
    
    public string csvAsString = null;
    
    public ForecastImportFromCSVController (){
    }
    
    public void updateForecastItems(){
        //try{
        csvAsString = csvFileBody.toString();
        
        List<List<String>> importedCSVList = parseCSV(csvAsString, false);
        Map<String, Integer> columnPositionMap = new Map<String, Integer> ();
        Integer j = 0;
        for(String col : importedCSVList.get(0)) {
            col = col.replace('\r\n', '');
            col = col.replace('\n', '');
            col = col.replace('\r', '');
            columnPositionMap.put(col.toLowerCase(), j++);
        }
        
        system.debug('columnPositionMap -- '+columnPositionMap);
        
        List<forecast_items__c> fcitemslistToUpd = new List<Forecast_items__c>(); 
        
        for(Integer i = 1; i < importedCSVList.size(); i++){
            List<String> csvRecordData = importedCSVList.get(i);
            System.debug('csvRecordData -- '+csvRecordData);
            Forecast_items__c fcitemsObj = new forecast_items__c() ;
            
Error--> String tmpOrderAcc = csvRecordData.get(columnPositionMap.get('Orders_Account__c'.toLowerCase()));
            String tmpUnitAcc = csvRecordData.get(columnPositionMap.get('Units_Account__c'.toLowerCase()));
            
            
            if(tmpOrderAcc==null){
                fcitemsObj.Orders_Account__c = 0;
            }else {
                fcitemsObj.Orders_Account__c = Integer.valueOf(tmpOrderAcc);
                
            }
            
            if(tmpUnitAcc==null){
                fcitemsObj.Units_Account__c = 0;
            }else {
                fcitemsObj.Units_Account__c = Integer.valueOf(tmpUnitAcc);
                
            }
            
            
            System.debug('csv data : '  + csvRecordData.get(i));
            //Forecast_items__c fcitemsObj = new forecast_items__c() ;
            fcitemsObj.Id = csvRecordData.get(columnPositionMap.get('ID'.toLowerCase()));
            //fcitemsObj.Account__c = Integer.valueOf(csvRecordData.get(columnPositionMap.get('Account__c')));
            //fcitemsObj.Orders_Account__c = Integer.valueOf(csvRecordData.get(columnPositionMap.get('Orders_Account__c'.toLowerCase())));
            //fcitemsObj.Orders_Finance__c = Integer.valueOf(csvRecordData.get(columnPositionMap.get('Orders_Finance__c'.toLowerCase())));
            //fcitemsObj.Units_Account__c = Integer.valueOf(csvRecordData.get(columnPositionMap.get('Units_Account__c'.toLowerCase())));   
            //fcitemsObj.Units_Finance__c = Integer.valueOf(csvRecordData.get(columnPositionMap.get('Units_Finance__c'.toLowerCase())));                                                                             
            fcitemslistToUpd.add(fcitemsObj);
        }
        update fcitemslistToUpd;
        fcitemslist =  new List<Forecast_items__c>(); 
        fcitemslist.addAll(fcitemslistToUpd);
        //} catch (Exception e) {
        //ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
        //ApexPages.addMessage(errorMessage);
        //}  
    }
    
    public static List<List<String>> parseCSV(String contents, Boolean skipHeaders) {
        List<List<String>> allFields = new List<List<String>>();
        
        // replace instances where a double quote begins a field containing a comma
        // in this case you get a double quote followed by a doubled double quote
        // do this for beginning and end of a field
        contents = contents.replaceAll(',"""',',"DBLQT').replaceall('""",','DBLQT",');
        // now replace all remaining double quotes - we do this so that we can reconstruct
        // fields with commas inside assuming they begin and end with a double quote
        contents = contents.replaceAll('""','DBLQT');
        // we are not attempting to handle fields with a newline inside of them
        // so, split on newline to get the spreadsheet rows
        List<String> lines = new List<String>();
        try {
            lines = contents.split('\n');
        } catch (System.ListException e) {
            System.debug('Limits exceeded?' + e.getMessage());
        }
        Integer num = 0;
        for(String line : lines) {
            
            system.debug('line 1-- '+line);
            line = line.replace('\r\n', '');
            line = line.replace('\n', '');
            line = line.replace('\r', '');
            system.debug('line 2-- '+line);
            
            // check for blank CSV lines (only commas)
            if (line.replaceAll(',','').trim().length() == 0) break;
            
            List<String> fields = line.split(',');  
            List<String> cleanFields = new List<String>();
            String compositeField;
            Boolean makeCompositeField = false;
            for(String field : fields) {
                if (field.startsWith('"') && field.endsWith('"')) {
                    cleanFields.add(field.replaceAll('DBLQT','"'));
                } else if (field.startsWith('"')) {
                    makeCompositeField = true;
                    compositeField = field;
                } else if (field.endsWith('"')) {
                    compositeField += ',' + field;
                    cleanFields.add(compositeField.replaceAll('DBLQT','"'));
                    makeCompositeField = false;
                } else if (makeCompositeField) {
                    compositeField +=  ',' + field;
                } else {
                    cleanFields.add(field.replaceAll('DBLQT','"'));
                }
            }
            
            allFields.add(cleanFields);
        }
        if (skipHeaders) allFields.remove(0);
        return allFields;       
    }
    
}

Thanks All
Hi All
Was trying to write a validation rule to push back the field value if field is edited to previous value, and if its blank its editable and the 1st value entered in the field stayes forever.
How can i add if its blank then let it save the value.

NOT(ISNULL(PRIORVALUE(FIElD Name)))
Any help is greatly appreciated.
Thanks
Hi All
had a quick question on writing a formula.So the use case is as follows.
We have separate timetrade links that oppty owners have that is linked to their calender for appointment fixing.So now we want to add this formula feild to email template where i can specify it the oppty owner is x then send x's timetrade link to the customer.
Any thoughts are greatly appreciated. 
Thanks Suman
Hi, 

We currently have a workflow email in place and it gets triggered where when someone changes the opportunity stage to (closed won or closed won annual) and opportunity department is (sales). However I want this workflow to get triggered only when if the opportunity owner changes/marks it as (closed won or closed won annual) and opportunity department is (sales). How can I do this? 

Thank you,
AcctMgt 
  
  • October 20, 2016
  • Like
  • 0
I created a  custom clone button for opportunity, using the following code: {!URLFOR( $Action.Opportunity.Clone , Opportunity.Id , ["opp3"=Opportunity.Name+"-Copy","opp9"="",,"00N54000000aRB3"=""...,"retURL"= URLFOR( $Action.Opportunity.View ,) ], true)}

One of the requirement is to give some fields the new values or empty them. When the button is clicked the new screen with cloned opportunity is opened and some fields got the new values except Date and Pick List (the ones that should be blank). I am not able to put a blank value to a date field or pick list. In the example, I provided opp9 is a ClosedDate. If I put any non null value, the clone will be created with a new value in this field, if I put ="', for a date or pick list, it's not working, the value from original opportunity stays on a cloned one.
Does anyone knows how to empty date and pick list on a clone? 
 
Hi All
Was trying to write a validation rule to push back the field value if field is edited to previous value, and if its blank its editable and the 1st value entered in the field stayes forever.
How can i add if its blank then let it save the value.

NOT(ISNULL(PRIORVALUE(FIElD Name)))
Any help is greatly appreciated.
Thanks