0

I'm trying to export record on into a tab separated text file. In getting Maximum stack depth reached: 1001 in the inner class where i'm doing the piece of code and I have pasted full code in the bottom.

public void addValue(Date value){
    if(value!=null){
        this.addValue(DateTime.newInstance(value.year(), value.month(), value.day()).format('dd/MM/yyyy'));    
    } else{
        this.addValue(value);
    }

}

Following is full code

public class CarerCardExportController {
    public static List<Card_Request__c> cards {get; set;}
    public static String header {get; set;}
    public static final String filename = DateTime.now().format('yyyyMMdd') +  'Carer Card Export.txt';
    public static String content {get; private set;}

    public CarerCardExportController(ApexPages.StandardController controller){

    }

    public CarerCardExportController(){

    }

    public void export(){
        header = 'CarerCardNumber\tCardExpiryDate\tFirstname\tSurname\tAddress1\tAddress2\tSuburb\tState\tPostcode\tPostalAddress1\t'
                  +'PostalAddress2\tPostalSuburb\tPostalSate\tPostalPostcode\tCentrelinkCRN\tDOB\tEmail\tPackType';
        //this is all we need for the page
        cards = [SELECT Id, Carers_Card_Number__c, SC_Contact__r.FirstName, SC_Contact__r.Lastname, SC_Contact__r.MailingStreet,
                SC_Contact__r.MailingCity, SC_Contact__r.MailingPostalCode, SC_Contact__r.MailingState, SC_Contact__r.MailingCountry, 
                SC_Contact__r.OtherStreet, SC_Contact__r.OtherCity, SC_Contact__r.OtherPostalCode, SC_Contact__r.OtherState, 
                SC_Contact__r.OtherCountry, CC_Expiry_Date__c, SC_Contact__r.CRN_Number__c, SC_Contact__r.Birthdate, SC_Contact__r.Email, 
                Carer_Card_Print_Type__c FROM Card_Request__c WHERE Card_Status__c = 'Issued' AND Carer_Sub_Status__c = 'Print Request Pending' AND Card_Type__c = 'Carer Card' FOR UPDATE];

        //now process the cards
        postProcessCards();   
    }



    public class CarerRow{

        public String row;
        public Boolean firstCall;

        public CarerRow(){
            row = '';
            firstCall = false;
        }

        public CarerRow(String value){
            this();
            row = (value == null) ? '' : value;
            firstCall = true;
        }

        public void addValue(String value){
            if(!firstCall && String.isBlank(row)){
                row =(value == null) ? '' : value;
                firstCall = true;
            }
            else row += '\t' + ((value == null) ? '' : value);
        }

        public void addValue(Date value){
            if(value!=null){
                this.addValue(DateTime.newInstance(value.year(), value.month(), value.day()).format('dd/MM/yyyy'));    
            } else{
                this.addValue(value);
            }

        }
    }

    public class CarerDocument{

        private String value;

        public CarerDocument(){
            value = '';
        }

        public CarerDocument(CarerRow row){
            this();
            value = row.row;
        }

        public void addRow(CarerRow row){
            value += '\r\n' + row.row;
        }

        public Blob getBlob(){
            return Blob.valueOf(value);
        }

        public String getValue(){
            return value;
        }

    }

    public static void postProcessCards(){
        List<Card_Request__c> cardsToUpdate = new List<Card_Request__c>();
        Set<Id> updatedCardIds = new Set<Id>();

        CarerRow headers = new CarerRow(header);
        CarerDocument carerExport = new CarerDocument(headers);

        for(Card_Request__c card : cards){

            //we don't want to mutate what's displayed on the page so handling the update for cards in a separate list
            cardsToUpdate.add(new Card_Request__c(
                Id = card.Id,
                Carer_Sub_Status__c = 'Waiting - Print Receipt',
                Date_Exported__c = Date.today()
            ));

            //also we are going to link the file generated to all the cards
            updatedCardIds.add(card.Id);

            String mailingKey = card.SC_Contact__r.MailingStreet + card.SC_Contact__r.MailingCity + card.SC_Contact__r.MailingState + card.SC_Contact__r.MailingPostalCode;
            String otherKey = card.SC_Contact__r.OtherStreet + card.SC_Contact__r.OtherCity + card.SC_Contact__r.OtherState + card.SC_Contact__r.OtherPostalCode;

            CarerRow newRow = new CarerRow();
            newRow.addValue(card.Carers_Card_Number__c);
            newRow.addValue(card.CC_Expiry_Date__c);
            newRow.addValue(card.SC_Contact__r.FirstName);
            newRow.addValue(card.SC_Contact__r.Lastname);
            //newRow.addValue(card.SC_Contact__r.Name);
            newRow.addValue(card.SC_Contact__r.MailingStreet);
            newRow.addValue(card.SC_Contact__r.MailingCity);
            newRow.addValue(card.SC_Contact__r.MailingState);
            newRow.addValue(card.SC_Contact__r.MailingPostalCode);
            if(otherKey == mailingKey){
                newRow.addValue('');
                newRow.addValue('');
                newRow.addValue('');
                newRow.addValue('');
            }
            else{
                newRow.addValue(card.SC_Contact__r.OtherStreet);
                newRow.addValue(card.SC_Contact__r.OtherCity);
                newRow.addValue(card.SC_Contact__r.OtherState);
                newRow.addValue(card.SC_Contact__r.OtherPostalCode);                
            }
            newRow.addValue(card.SC_Contact__r.CRN_Number__c);
            newRow.addValue(card.SC_Contact__r.Birthdate);
            newRow.addValue(card.SC_Contact__r.Email);
            newRow.addValue(card.Carer_Card_Print_Type__c);
            carerExport.addRow(newRow);
        }


        //create the file
        content = carerExport.getValue();
        Blob exportBlob = carerExport.getBlob(); //convert the page generated to a Blob
        ContentVersion conVer = new ContentVersion();
        conVer.ContentLocation = 'S'; // S specify this document is in SF
        conVer.PathOnClient = filename; 
        conVer.Title = filename; 
        conVer.VersionData = exportBlob; 
        insert conVer;

        //need to fetch the content document Id
        Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;

        //link the file to all the card records
        List<ContentDocumentLink> cdlsToInsert = new List<ContentDocumentLink>();
        for(Id cardId : updatedCardIds){
            ContentDocumentLink cDe = new ContentDocumentLink();
            cDe.ContentDocumentId = conDoc;
            cDe.LinkedEntityId = cardId;
            cDe.ShareType = 'I'; // Inferred permission, checkout description of ContentDocumentLink object for more details
            //cDe.Visibility = 'SharedUsers';
            cdlsToInsert.add(cDe);
        }
        insert cdlsToInsert;

        update cardsToUpdate;
    }

    public String getFilename(){
        return filename;
    }
}
1

When you call CarerRow#addValue(Date), if value is null, then it tries to call CarerRow#addValue(Date) again, in which value will be null, which tries to call CarerRow#addValue(Date), ad nauseam, until the stack limit is reached. You probably meant to write this instead:

    public void addValue(Date value){
        if(value!=null){
            this.addValue(DateTime.newInstance(value.year(), value.month(), value.day()).format('dd/MM/yyyy'));    
        } else{
            this.addValue((String)null);
        }
    }

This will call your CarerRow#addValue(String) instead, which should fix your problem.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.