0

I have a trigger method after update on Opportunity. I am trying to insert attachment after closing the opp.

 private void createOppAttachments(list<Opportunity> opps) {
         Set<Id> oppIds = new Set<Id>();
         List<Attachment> attList = new List<Attachment>();
         for(Opportunity op:opps){
             if (op.StageName == 'Closed Won')
                 oppIds.add(op.Id);
                 //MergePDF_Controller.Save(op.Id, op.Name);
                 //MergePDF_Controller.generatePdf(op.Id, op.Name);

         }
        StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'logi' LIMIT 1];
         //String logobody = sr.Body.toString();
         //System.debug(Logginglevel.error,'******************logobody : '+sr );
         if(oppIds != null && !oppIds.isEmpty()) {
              for(Opportunity opp : [ SELECT Id, Name,Image__c, Account.Name, 
                                     (SELECT Quantity, UnitPrice, TotalPrice, 
                                      PricebookEntry.Name, PricebookEntry.Product2.Family FROM 
                                      OpportunityLineItems) 
                                      FROM Opportunity WHERE Id IN:oppIds]){  
                   String logo='<img src="/resource/08146000000AiqO/logi" />';
                   logo.replaceall('src','');
                   String bodyStr = '';
                   System.debug(Logginglevel.error,'******************opp prod: '+opp.OpportunityLineItems);   
                   for(OpportunityLineItem ol:opp.OpportunityLineItems){
                       bodyStr += logo+'Product Name      : ' + ol.PricebookEntry.Name+ '\r\n'+'Total Price      : ' + ol.TotalPrice+ '\r\n'+'<br/>';
                       bodyStr += '\r\n';

                   }  
                   System.debug(Logginglevel.error,'******************String: '+bodyStr );               
                 bodyStr +='<img src="https://pbs.twimg.com/profile_images/748187442318974976/CDvq8db7.jpg" alt="Mountain View" style="width:304px;height:228px;">';
                 string td1='"border:1px solid green; width=200px;"';
                 string td2='"width=200px; border:1px solid green; background-color:red; color:white; font-weight:bold;"';               
                 string tdHead='"border:1px solid green; width=200px; color:white; background-color:green; font-weight:bold;"';
                 String contents = '<html><body>'+bodyStr+'</body></html>';

                 Blob pdfBlob = Blob.toPDF(bodyStr );
                 Attachment at = new Attachment(parentId = opp.id, name=opp.name + '.pdf', body = pdfBlob);

                 attList.add(at);                 
             }
              insert attList;       
         } 

    } 

Error: System.InvalidParameterValueException: An error occurred while parsing the input string.

How do I insert a logo image in the attachment body?

  • 1
    Not sure if this is causing your error, but when creating contents you aren't actually concatenating the bodyStr variable. It should be '<html><body>'+bodyStr+'</body></html>' – Jeff Bennett Apr 21 '17 at 20:25
  • I did this, "String contents = '<html><body>'+bodyStr+'</body></html>';". Still I am getting the same issue. – Ajay Apr 21 '17 at 20:28
  • Does it give you a line number? – Jeff Bennett Apr 21 '17 at 20:34
  • Additionally, this question might provide some helpful resources: salesforce.stackexchange.com/questions/7575/… – Jeff Bennett Apr 21 '17 at 20:35
  • error occurs at "Blob pdfBlob = Blob.toPDF(bodyStr );" – Ajay Apr 21 '17 at 20:36

Your Answer

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

Browse other questions tagged or ask your own question.