function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
OssieOssie 

Please Help!! - Auto Increment File Name

Hi All,

 

Need help please.  Im not a developer and so struggling with the followng issue: 

 

I have created a custom object which behaves in a similer manner as the "Quote" standard object and so i am able to produce Quotes in PDF by clicking on a custom button called "Create PDF".  However, when I produce the PDF file i would like to incremant the file name by '1' each time.  For example, when the "Create PDF" button is clicked the file name should be Quote_V1.pdf should then become Quote_V2.PDF,  then Quote_V3.PDF etc etc

 

Not sure how to do this.  Please see a part of my apex code below which is where I think i need to make my modification :

     Public PageReference SaveQuote()
    {
        try
        {
            String QId = ApexPages.currentPage().getParameters().get('id');
            dealerQuotation = [SELECT Id,Name FROM Dealer_Quotation__c WHERE Id = :QId]; 
            PageReference pdf = Page.Dealer_Quotation;
            pdf.getParameters().put('id',Qid);
            
            Attachment attach = new Attachment();
            Blob body;
            try
            {
                body = pdf.getContentAsPdf();
            }
            catch(Exception ex)
            {
                body = Blob.ValueOf('Error occured in page');
            }
            string name;
            if(dealerQuotation != null)
            {
                name = dealerQuotation.Name + '.pdf';
            }
            else
            {
                name = 'Quotation.pdf';
            }
            attach.body = body;
            attach.Name = name;
            attach.IsPrivate = false;
            attach.ParentId = QId;
            insert attach; 
            PageReference redirectPage = New PageReference('/' + QId);
            redirectPage.setRedirect(true);
            return redirectPage;    
        }
        catch(Exception ex)
        {
            ApexPages.Message exMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            ApexPages.AddMessage(exMsg);
            return null;
        }    
       
    }

 

Any help would be greatly appreciated!

 

Best Answer chosen by Admin (Salesforce Developers) 
k_bentsenk_bentsen
Yes, you can do an SOQL query for attachments using WHERE ParentID = :Dealer_Quotation__c.id and then get the size of the returned list.

All Answers

k_bentsenk_bentsen

You could create a custom number field on your Dealer Quotation object that tracks the number of created pdfs such that each time the button is clicked, the value is incremented by 1, and you can use this value in the quote names. Just set the default value to 1 for the custom field, and increment after attachment is inserted.

OssieOssie

Thanks for the response.

The pds's are saved to related list "Notes and Attachments" and so any idea how i track the number of pds created? 

k_bentsenk_bentsen
Yes, you can do an SOQL query for attachments using WHERE ParentID = :Dealer_Quotation__c.id and then get the size of the returned list.
This was selected as the best answer
OssieOssie

That worked perfectly!  Thank you!