• Prabhat Kumar 28
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
Hi,
I am having a custom object and have 'Files' or 'Notes and Attachments' related list. There is a field called 'Status' on custom object. When the status is 'Approved' i would like to restrict the user from uploading the file. How can i achieve this?
Is it correct to write a trigger on 'ContentVersion' (after insert) and throw error message from it?
  • April 17, 2018
  • Like
  • 0

I want to create one Custom button and on click of that button I want to open PDF.

So i created Vf page for this which is renderAS=PDF it works fine

but now When i clich on that button I want to opne the PDF and at the same time want to save the same PDF as a attachment in Attachment object in saleforce.

here is my Vf page code
<apex:page standardController="WorkOrder" showHeader="false" sidebar="false"  extensions="CustomPDFController" renderAs="pdf"  action="{!SaveAtt}" >
    <apex:form >
        
        <apex:pageBlock tabStyle="Account" title="Work Order">
            &nbsp;&nbsp;<apex:outputLabel value="Name : "> &nbsp;&nbsp;&nbsp;&nbsp;
            <apex:outputText value="{!wkorderlist[0].WorkOrderNumber}"></apex:outputText></apex:outputLabel>
            
            &nbsp;&nbsp; <apex:outputLabel value="Status : "> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <apex:outputText value="{!wkorderlist[0].status}"></apex:outputText></apex:outputLabel>
            
            &nbsp;&nbsp; <apex:outputLabel value="Priority : "> &nbsp;&nbsp;&nbsp;&nbsp;
            <apex:outputText value="{!wkorderlist[0].Priority}"></apex:outputText></apex:outputLabel>
        </apex:pageBlock>
        
        <apex:repeat value="{!MapList}" var="key">
            <apex:pageBlock title="Customer Asset Name" >
            </apex:pageBlock>
            <apex:outputText value="{!key}"/> &nbsp;&nbsp;&nbsp;
            <apex:pageBlock title="Work Order Line Items">
                <apex:pageBlockTable value="{!MapList[key]}" var="lists">
                    <apex:column value="{!lists.LineItemNumber}"/>
                    <apex:column value="{!lists.Status}"/>
                    <apex:column value="{!lists.Discount}"/>
                    <apex:column value="{!lists.DurationType}"/>
                    <apex:column value="{!lists.Priority}"/>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:repeat>
    </apex:form>
</apex:page>

this is my class code

public class CustomPDFController{
   
    public String currentRecordId {get;set;}
    public Map<String,List<WorkOrderLineItem>> MapList {get;set;}
    public list<Customer_Asset__c> customerAssertlist {get;set;}
    public list<workorder> wkorderlist {get;set;}
    public CustomPDFController(ApexPages.StandardController stdController){
    
        MapList = new map<String,list<WorkOrderLineItem>>();
        currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        system.debug('currentRecordId >>>>>>>> ' +currentRecordId);
        
        customerAssertlist = [select Name, Account__c,Division__c,Work_Order__c,Parent_Customer_Asset__c,(select Id,LineItemNumber,Status,Discount,DurationType,Priority from Work_Order_Line_Items__r where WorkOrderId =:currentRecordId) from Customer_Asset__c  ];
        
        wkorderlist = [select Id,WorkOrderNumber,status,Priority from workorder where Id =:currentRecordId];
        for(Customer_Asset__c cusAsObj : customerAssertlist){
            if(cusAsObj.Work_Order_Line_Items__r.size() > 0){
                MapList.put(cusAsObj.Name, cusAsObj.Work_Order_Line_Items__r);
            }
        }
        /*system.debug('MapList >>>>>>>>>>>>> ' +MapList);
         PageReference pdf = new PageReference('/apex/CustomPDFControllerSec?id='+currentRecordId);
        PageReference pdf = Page.CustomPDF;
        //prep attachment   
        //pdf.getParameters().put('id', currentRecordId);
        
        Attachment attach = new Attachment();
        Blob b = pdf.getContent();
        attach.Body = b;
        attach.Name = 'invoice.pdf';
        attach.IsPrivate = false;
        attach.ParentId = currentRecordId;
        //insert attach ;*/
        
    }
    
    public void SaveAtt(){
        //PageReference pdf = new PageReference('/apex/CustomPDFv2?id='+currentRecordId);
        //prep attachment  
        PageReference pdf = Page.CustomPDF; 
        system.debug('DDEREREREGGGG '+pdf) ;
        pdf.getParameters().put('id', currentRecordId);
        
        Attachment attach = new Attachment();
        Blob b;
        try{
            b = pdf.getContentAsPDF();
        } catch (VisualforceException e) {
            b = Blob.valueOf('Some Text');
        }
        system.debug('FDFDFDF '+b) ;
        attach.Body = b;
        attach.Name = 'invoice.pdf';
        attach.IsPrivate = false;
        attach.ParentId = currentRecordId;
        attach.ContentType = 'application/pdf';
        insert attach ;
        
    }
    
    @AuraEnabled
    public static void emailInvoice(String RecordId){
        
        PageReference pdf = Page.CustomPDF; 
        pdf.getParameters().put('id', RecordId);
        //pdf.setRedirect(true); //does not seem to be required
        
        list<attachment> attlst =new list<attachment>();
        Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
       
        Blob b = pdf.getContentAsPDF();
        system.debug('dddffdfdfd '+b) ;
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName('invoice.pdf');
        efa.setBody(b);
        string[] toAddr = new string[] {'rohit.dhiman@360degreeapps.com'};
        mail.setToAddresses(toAddr);
        
        mail.setSubject('Your Invoice from ' + UserInfo.getOrganizationName());
        
        mail.setHtmlBody('Thanks for attending:<b> ' + UserInfo.getOrganizationName() +' </b><p>'+
                         ' Your Invoice is attached.');
        
        mail.setFileAttachments(new Messaging.Emailfileattachment[] {efa}); 
        
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});  
        system.debug('Mail Sent >>>>>>>>>> ' +r);
       

    }
}

So while opening the VF page from the custom button I am getting error as 
Too many nested getContent calls. 

So can anyone please help me out in this 

 

Thank you in advance

Hi everyone,

I have two objects Account and Sales.
Account has fields - name, created date, location, payment made.
Sales has fields - date, location, revenue.

I want to compare sum of all payment made by all accounts group by created date and location with revenue from sales object on particular date for same location.
 
for eg: 5 accounts from location A on Mar 01 has sum of 1000 as payment made.
Also on sales object location A on mar 01 should have revenue greater than 1000 or more.

I have tried reports, but I am not successful in getting it solved.

please suggest me possible ideas which I can use to implement this. I am comfortable using apex if needed.