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
Phuc Nguyen 18Phuc Nguyen 18 

Generate PDF from newly created record

Hello All,
Hopeone someone can steer me in the right direction here.  I am saving a record from a user entered VF page. What I need to do is generate a PDF of the newly created record and attach it to the record.  I have a new VF page using (renderAs= PDF) and an Apex Class to grab the data to populate the VF but I need to make sure that the data has been saved first  before generating the PDF.  Also, how can I save it to the new record?  Is there a better way to do do this or am I on the right track?
Thanks,
P
Best Answer chosen by Phuc Nguyen 18
Foram Rana RForam Rana R
Hi phuc,
Hope you are doing well....!!!

You can use below code.
//*** Method to Create Contact
public void createContact() 
{
		List<Contact> checkingContacts = new List<Contact>();
        for(Contact objCon : lstcon) {
            if(objCon.Email!=NULL && objCon!=NULL )
            {
                
                checkingContacts = [SELECT id, Name, Email FROM Contact WHERE Email =:objCon.email LIMIT 1];
            }
            //system.debug('checkingContacts:::'+checkingContacts);
            //IF CONTACT IS NOT PRESENT IN SALESFORCE then insert.
            if((checkingContacts==NULL || checkingContacts.size()==0 || checkingContacts.isEmpty()) && CampaignId!=NULL)
        {
            try
            {
                
                insert objCon;
                
               
                //INSERTING FILES
                                   
                    ContentVersion conVer = new ContentVersion();
                    conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
                    conVer.PathOnClient = fname; // The files name, extension is very important here which will help the file in preview.
                    conVer.Title = fname; // Display name of the files
                    conVer.VersionData = EncodingUtil.base64Decode(fileBody.substringAfter(',')); // converting your binary string to Blog
                    insert conVer;  
                    
                    // First get the content document Id from ContentVersion
                    Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
                    
                    //Create ContentDocumentLink
                    ContentDocumentLink cDe = new ContentDocumentLink();
                    cDe.ContentDocumentId = conDoc;
                    cDe.LinkedEntityId = objCon.Id; // you can use objectId,GroupId etc
                    cDe.ShareType = 'V'; // Inferred permission, checkout description of ContentDocumentLink object for more details
                    cDe.Visibility = 'AllUsers';
                    insert cDe;
                    
                    //displayPopup = false;
                    displayOuterPannel = true;
                    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Contact Created Successfully.Thank you!'));
                
            }
            catch(DMLException e)
            {
                 apexpages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter valid data to create contact.'));
            }
            
        }
		
	}
}
Hope this solution helps you...!!

Regards,
Foram Rana
 

All Answers

Foram Rana RForam Rana R
Hi phuc,
Hope you are doing well....!!!

You can use below code.
//*** Method to Create Contact
public void createContact() 
{
		List<Contact> checkingContacts = new List<Contact>();
        for(Contact objCon : lstcon) {
            if(objCon.Email!=NULL && objCon!=NULL )
            {
                
                checkingContacts = [SELECT id, Name, Email FROM Contact WHERE Email =:objCon.email LIMIT 1];
            }
            //system.debug('checkingContacts:::'+checkingContacts);
            //IF CONTACT IS NOT PRESENT IN SALESFORCE then insert.
            if((checkingContacts==NULL || checkingContacts.size()==0 || checkingContacts.isEmpty()) && CampaignId!=NULL)
        {
            try
            {
                
                insert objCon;
                
               
                //INSERTING FILES
                                   
                    ContentVersion conVer = new ContentVersion();
                    conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
                    conVer.PathOnClient = fname; // The files name, extension is very important here which will help the file in preview.
                    conVer.Title = fname; // Display name of the files
                    conVer.VersionData = EncodingUtil.base64Decode(fileBody.substringAfter(',')); // converting your binary string to Blog
                    insert conVer;  
                    
                    // First get the content document Id from ContentVersion
                    Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
                    
                    //Create ContentDocumentLink
                    ContentDocumentLink cDe = new ContentDocumentLink();
                    cDe.ContentDocumentId = conDoc;
                    cDe.LinkedEntityId = objCon.Id; // you can use objectId,GroupId etc
                    cDe.ShareType = 'V'; // Inferred permission, checkout description of ContentDocumentLink object for more details
                    cDe.Visibility = 'AllUsers';
                    insert cDe;
                    
                    //displayPopup = false;
                    displayOuterPannel = true;
                    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Contact Created Successfully.Thank you!'));
                
            }
            catch(DMLException e)
            {
                 apexpages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter valid data to create contact.'));
            }
            
        }
		
	}
}
Hope this solution helps you...!!

Regards,
Foram Rana
 
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Hello Foram,
Thank you for the reply.  How is the 'objCon.email' value obtained?  If I need to get teh id of teh record from the record that was just created how can I do that?  Am I appending the code below '  //INSERTING FILES' into my existing code?  So if you recall I have a VF page that a user enters data and they save the record.  I created another VF(PDF) and Class but was not sure how to tie it together.  Do I need a seperate VF page to generate the PDF?
Thanks for your help and time.
Cheers,
P