Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. public class payment {
  2. @InvocableVariable(label='RecordID')
  3. public String recordID;
  4. @InvocableVariable(label='Status')
  5. public String status;
  6. }
  7.  
  8. @InvocableMethod(label='Generate PDF')
  9. public static void generatePDF(List<payment> payments)
  10. {
  11. payment pr = payments[0];
  12.  
  13. String QuoteID;
  14.  
  15. Opportunity opp = [select id,Pricebook2Id,owner.email,name,accountid,account.personcontactid,account.billingstreet,account.PersonEmail,account.billingcity,account.billingstate,account.billingpostalcode from opportunity where id =: pr.recordID];
  16.  
  17. List<Opportunitylineitem> lstOpplineItems = [SELECT Id,ListPrice,Name,OpportunityId,PricebookEntryId,Product2Id,ProductCode,Product_Family__c,Product_Name__c,Quantity,TotalPrice,UnitPrice FROM OpportunityLineItem where OpportunityId =: opp.id];
  18.  
  19. List<OpportunityContactRole> lstOppContactRoles = [SELECT ContactId,contact.email,Contact.mailingstreet,Contact.mailingcity,Contact.mailingstate,Contact.mailingpostalcode,Id,IsPrimary,OpportunityId,Role FROM OpportunityContactRole where OpportunityId =: opp.id and IsPrimary = True];
  20.  
  21.  
  22. system.debug('Status' + pr.status);
  23.  
  24. //Create the quote once the payment request is sent to the customer
  25. if(pr.status == 'Created' || pr.status == 'Manually Sent' || pr.status == 'Auto Sent')
  26. {
  27. String MailingStreet = '';
  28. String MailingCity = '';
  29. String MailingState = '';
  30. String MailingPostalCode = '';
  31.  
  32.  
  33. //List<contact> listContacts = [select id,email,name,MailingCity,MailingCountry,MailingPostalCode,MailingState,MailingStreet,accountid from contact where id =: lstOppContactRoles[0].contactid];
  34.  
  35. //Take the address from the contact linked with the opp
  36. if(lstOppContactRoles.size() == 0)
  37. {
  38. MailingStreet = opp.account.billingstreet;
  39. MailingCity = opp.account.billingcity;
  40. MailingState = opp.account.billingstate;
  41. MailingPostalCode = opp.account.billingpostalcode;
  42.  
  43. }
  44. else
  45. {
  46. MailingStreet = lstOppContactRoles[0].Contact.mailingstreet;
  47. MailingCity = lstOppContactRoles[0].Contact.mailingcity;
  48. MailingState = lstOppContactRoles[0].Contact.mailingstate;
  49. MailingPostalCode = lstOppContactRoles[0].Contact.mailingpostalcode;
  50. }
  51.  
  52. //Create the quote here
  53. Quote q = new Quote();
  54. q.OpportunityId = opp.id;
  55. q.name = 'Quote : Online Payment - ' + Opp.Name;
  56. q.Notes_about_your_order__c = 'N/A';
  57.  
  58.  
  59. q.Status = 'Draft';
  60. q.BillingStreet =MailingStreet;
  61. q.BillingCity =MailingCity;
  62. q.BillingState =MailingState;
  63. q.BillingPostalCode =MailingPostalCode;
  64.  
  65. q.ShippingStreet =MailingStreet;
  66. q.ShippingCity =MailingCity;
  67. q.ShippingState =MailingState;
  68. q.ShippingPostalCode =MailingPostalCode;
  69. q.Pricebook2Id = opp.Pricebook2Id;
  70.  
  71. insert q;
  72.  
  73. //Create quote line items based on opp line items
  74.  
  75. List<QuoteLineItem> listQuoteLineItem = new List<QuoteLineItem>();
  76. for(Opportunitylineitem OppLineItem : lstOpplineItems)
  77. {
  78. QuoteLineItem qlt = new QuoteLineItem();
  79.  
  80.  
  81. qlt.PricebookEntryId = OppLineItem.PricebookEntryId ;
  82. qlt.Product2Id = OppLineItem.Product2Id ;
  83. qlt.Quantity = OppLineItem.Quantity ;
  84. qlt.UnitPrice = OppLineItem.UnitPrice ;
  85. qlt.QuoteId = q.id ;
  86.  
  87. listQuoteLineItem.add(qlt);
  88. }
  89.  
  90. insert listQuoteLineItem;
  91. }
  92. else //The payment request is now paid, hence a quote PDF will be created and sent to the customer
  93. {
  94. List<Quote> quotes = [select id,contactid,email from quote where opportunityid =: opp.id limit 1];
  95. List<Quote> quotestoUpdate = new List<Quote>();
  96. if(quotes.size()>0)
  97. {
  98. QuoteID = quotes[0].id;
  99. String quotetemplateID = '0EH24000000yKo7';
  100. String quoteUrl = '/quote/quoteTemplateDataViewer.apexp?id=';
  101. quoteUrl +=QuoteID;
  102. quoteUrl +='&headerHeight=190&footerHeight=188&summlid=';
  103. quoteUrl +=quotetemplateID ;
  104. quoteUrl +='#toolbar=1&navpanes=0&zoom=90';
  105.  
  106. //Create pdf content
  107. PageReference pg = new PageReference(quoteUrl) ;
  108.  
  109. //Document object of quote which hold the quote pdf
  110. QuoteDocument quotedoc = new QuoteDocument();
  111.  
  112. Blob b;
  113. if (Test.IsRunningTest())
  114. {
  115. b = Blob.valueOf('UNIT.TEST');
  116. }
  117. else
  118. {
  119. b = pg.getContentAsPDF() ;
  120. }
  121.  
  122.  
  123. //content assign to document
  124. quotedoc.Document = b;
  125.  
  126. //assign quote id where pdf should attach
  127. quotedoc.QuoteId = QuoteID ;
  128.  
  129. //insert the quotdoc
  130. insert quotedoc;
  131.  
  132.  
  133. EmailTemplate templateId = [Select id from EmailTemplate where name = 'Customer VAT Receipt'];
  134.  
  135. /// Send the email to the customer
  136. Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
  137.  
  138. // Create the email attachment with the quote PDF created above
  139. email.setTemplateID(templateId.Id);
  140.  
  141. if(lstOppContactRoles.size() == 0)
  142. email.setTargetObjectId(opp.account.personcontactid);
  143. else
  144. email.setTargetObjectId(lstOppContactRoles[0].contactid);
  145.  
  146. email.setSaveAsActivity(false);
  147. Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
  148. efa.setFileName('attachment.pdf');
  149. efa.setBody(b);
  150.  
  151. String[] toAddresses;
  152. if(lstOppContactRoles.size() == 0)
  153. toAddresses = new String[] {opp.account.PersonEmail};
  154. else
  155. toAddresses = new String[] {lstOppContactRoles[0].contact.email};
  156.  
  157. //String[] toAddresses = new String[] {opp.account.PersonEmail};
  158. String[] ccAddresses = new String[] {opp.owner.email};
  159.  
  160. email.setToAddresses( toAddresses );
  161. email.setCcAddresses( ccAddresses );
  162. //email.setPlainTextBody( 'This is a simple email body' );
  163. email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
  164.  
  165. Messaging.SendEmailResult [] r =
  166. Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
  167.  
  168.  
  169. //Once the email is sent to the customer with Quote and PDF attached, the quote status will be changed to accepted.
  170. quotes[0].status = 'Accepted';
  171. quotestoUpdate.add(quotes[0]);
  172.  
  173. update quotestoUpdate;
  174.  
  175. //Finally the Opp stage will be changed to Invoiced.
  176. opp.StageName = 'Invoiced';
  177.  
  178. update opp;
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement