+ Start a Discussion
SFDC@ErrorSFDC@Error 

Dynamic pdf name in attachment

Hi All
I have created a controller and vf page.when i am creating record file attaching in notes and attachment with static name.like test.pdf.How can i create dynamic name for pdf name for example object__c.Name.pdf.
public class AttachPDF {

private final Id recordId;

public AttachPDF_Controller(ApexPages.StandardController controller) {
recordId =     controller.getId();
system.debug('inside AttachPDF');
    }
    @future(callout = true)
     public static void save(Id AccId, String acctName){
    system.debug('inside AttachPDF save');
        PageReference pdf = new PageReference('/apex/PDFPage?id='+Accid);
         pdf.setRedirect(true);
         pdf.getParameters().put('Id',AccId);
         Blob b= pdf.getContent();
        
        Attachment formAttach = new Attachment();
           
             formAttach.Name = 'test'+'.pdf';
             formAttach.body = b;
             formAttach.ContentType='application/pdf';
             formAttach.parentId =AccId;
             insert formAttach; 
              
        
    }

}
Hemant_SoniHemant_Soni
Hi SFDC ERROR,
Please try like below example 
public class AttachPDF {

private final Id recordId;
public Account oAcc = new Account();
public AttachPDF_Controller(ApexPages.StandardController controller) {
	recordId =     controller.getId();
	for(Account oAccount : [Select Id,Name From Account Where Id =: recordId]){
		oAcc = oAccount;
	}
}
system.debug('inside AttachPDF');
    }
    @future(callout = true)
     public static void save(Id AccId, String acctName){
    system.debug('inside AttachPDF save');
        PageReference pdf = new PageReference('/apex/PDFPage?id='+Accid);
         pdf.setRedirect(true);
         pdf.getParameters().put('Id',AccId);
         Blob b= pdf.getContent();
        
        Attachment formAttach = new Attachment();
           
             formAttach.Name = oAcc.Name+'.pdf';
             formAttach.body = b;
             formAttach.ContentType='application/pdf';
             formAttach.parentId =AccId;
             insert formAttach; 
              
        
    }
}
Thanks
Hemant
SFDC@ErrorSFDC@Error
i am getting error [image: Error]Error: Compile Error: Unexpected token '@'. at line 11 column 5
Raj VakatiRaj Vakati
Use this code
 
public class AttachPDF {

private final Id recordId;
public Account oAcc = new Account();
public AttachPDF_Controller(ApexPages.StandardController controller) {
	recordId =     controller.getId();
	for(Account oAccount : [Select Id,Name From Account Where Id =: recordId]){
		oAcc = oAccount;
	}
}
system.debug('inside AttachPDF');
    }
    @future(callout = true)
     public static void save(Id AccId, String acctName){
    system.debug('inside AttachPDF save');
        PageReference pdf = new PageReference('/apex/PDFPage?id='+Accid);
         pdf.setRedirect(true);
         pdf.getParameters().put('Id',AccId);
         Blob b= pdf.getContent();
        
        Attachment formAttach = new Attachment();
           
             formAttach.Name = acctName+'.pdf';
             formAttach.body = b;
             formAttach.ContentType='application/pdf';
             formAttach.parentId =AccId;
             insert formAttach; 
              
        
    }
}

 
Hemant_SoniHemant_Soni
I thaought Below will Complete Fix. But First Try "Raj Vakati" Code and if that give you error then try Mine. Because Raj Is Too Good.
public class AttachPDF {
    
    private final Id recordId;
    public Static Account oAcc = new Account();
    
    public AttachPDF(ApexPages.StandardController controller) {
        recordId = controller.getId();
        for(Account oAccount : [Select Id,Name From Account Where Id =: recordId]){
            oAcc = oAccount;
        }
        system.debug('inside AttachPDF');
    }
    @future(callout = true)
    public static void save(Id AccId, String acctName){
        system.debug('inside AttachPDF save');
        PageReference pdf = new PageReference('/apex/PDFPage?id='+Accid);
        pdf.setRedirect(true);
        pdf.getParameters().put('Id',AccId);
        Blob b= pdf.getContent();
        
        Attachment formAttach = new Attachment();
        formAttach.Name = oAcc.Name+'.pdf';
        formAttach.body = b;
        formAttach.ContentType = 'application/pdf';
        formAttach.parentId = AccId;
        
        insert formAttach;         
    }
}

 
SFDC@ErrorSFDC@Error
Yes ,But now it is not attaching the file after record create
Raj VakatiRaj Vakati
Can you refresh the record and see .. its feature call so there may be some delay
Raj VakatiRaj Vakati
Check debug logs also
Hemant_SoniHemant_Soni
Use "getContentAsPDF()" method instead of "getContent()". I thought that will help.
SFDC@ErrorSFDC@Error
I checked ,its showing null.pdf.
Raj VakatiRaj Vakati
How do you invoking this code ??
SFDC@ErrorSFDC@Error
Using trigger after insert event
Raj VakatiRaj Vakati
Give me the trigger code
SFDC@ErrorSFDC@Error
trigger InsertAttachment on Training__c(after insert) 
{
    for(Training__c acc:Trigger.New)
    {   
        
     
     if(acc.Status__c=='Received')
        {
     
        AttachPDF.Save(acc.Id, acc.Name);
       
   
    }
}
}

 
Raj VakatiRaj Vakati
Use this code
Trigger 
 
trigger InsertAttachment on Training__c(after insert) 
{
    for(Training__c acc:Trigger.New)
    {   
     if(acc.Status__c=='Received')
        {
        AttachPDF.Save(acc.Id, acc.Name);
    }
}
}

Class
 
public class AttachPDF {
    
    private final Id recordId;
    public Static Account oAcc = new Account();
    
    public AttachPDF(ApexPages.StandardController controller) {
        recordId = controller.getId();
        for(Account oAccount : [Select Id,Name From Account Where Id =: recordId]){
            oAcc = oAccount;
        }
        system.debug('inside AttachPDF');
    }
    @future(callout = true)
    public static void save(Id AccId, String acctName){
        system.debug('inside AttachPDF save');
        PageReference pdf = new PageReference('/apex/PDFPage?id='+Accid);
        pdf.setRedirect(true);
        pdf.getParameters().put('Id',AccId);
        Blob b= pdf.getContent();
    
        Attachment formAttach = new Attachment();
        formAttach.Name = acctName+'.pdf';
        formAttach.body = b;
        formAttach.ContentType = 'application/pdf';
        formAttach.parentId = AccId;
        
        insert formAttach;         
    }
}


Let me know if it dnt work
Hemant_SoniHemant_Soni
I got the point Use below.
public class AttachPDF {
    
    private final Id recordId;
    
    public AttachPDF(ApexPages.StandardController controller) {
        recordId = controller.getId();
        system.debug('inside AttachPDF');
    }
    @future(callout = true)
    public static void save(Id AccId, String acctName){
        system.debug('inside AttachPDF save');
        PageReference pdf = new PageReference('/apex/PDFPage?id='+Accid);
        pdf.setRedirect(true);
        pdf.getParameters().put('Id',AccId);
        Blob b= pdf.getContent();
        
        Attachment formAttach = new Attachment();
        formAttach.Name = acctName+'.pdf';
        formAttach.body = b;
        formAttach.ContentType = 'application/pdf';
        formAttach.parentId = AccId;
        
        insert formAttach;         
    }
}
Raj This guy using Mine Code and that will not work at all from trigger that is i know. I was in the impression that this will run from a vf page. Now i have made same change that you have and fixed all other changes as well. I think this time it will work.

Thanks
Hemant