我想生成一个 pdf 文件,将通过电子邮件发送给机会的所有相关联系人 - Salesforce

问题描述 投票:0回答:0

我想生成一个 pdf 文件,该 pdf 文件将通过电子邮件发送给机会的所有相关联系人,显示该机会关闭时的机会详细信息。 为此,我使用了 VisualForce Page 和一个合适的控制器以及一个触发器,它会在机会关闭时触发并通过电子邮件将 pdf 发送给机会的相关联系人。

--问题是,当我想在试用组织中对其进行测试时:当我想通过适当的相关联系人来传递机会阶段时,stagename = closed won 甚至 closed lost 我不能!我总是以红色触发错误,向我显示:错误!您无法更改此机会的阶段,请联系您的管理员...我试图在开发人员控制台中打开调试日志,但我什么也没找到,有人可以帮我吗?谢谢

控制器:

public class OpportunityPDFController {

    public Opportunity opportunity { get; set; }
    public list<Contact> relatedContacts {get; set;}

    public OpportunityPDFController() {
        // Get the Id parameter from the Visualforce page URL
        String opportunityId = ApexPages.currentPage().getParameters().get('id');

        // Query the opportunity record
        opportunity = [SELECT Id, Name, CloseDate, StageName, Amount, Probability, Description FROM Opportunity WHERE Id = :opportunityId];
    }
}

触发器:

    trigger ClosedOpportunityEmail on Opportunity (after update) {
    // Check if the opportunity is closed
    List<Opportunity> closedOpportunities = new List<Opportunity>();
    for (Opportunity opp : Trigger.new) {
        if (opp.StageName == 'Closed Won' || opp.StageName == 'Closed Lost') {
            closedOpportunities.add(opp);
        }
    }
    
    // Retrieve related contacts for closed opportunities
    List<Contact> relatedContacts = new List<Contact>();
    for (Opportunity opp : closedOpportunities) {
        List<OpportunityContactRole> oppRoles = [SELECT ContactId FROM OpportunityContactRole WHERE OpportunityId = :opp.Id];
        for (OpportunityContactRole role : oppRoles) {
            relatedContacts.add([SELECT Id, Name, Email FROM Contact WHERE Id = :role.ContactId]);
        }
    }
    
    // Generate PDF and send email to related contacts
    if (!relatedContacts.isEmpty()) {
        for (Contact contact : relatedContacts) {
            // Generate PDF using Visualforce
            PageReference pdfPage = Page.OpportunityPDFPage;
            pdfPage.getParameters().put('id', String.valueOf(closedOpportunities[0].Id)); // pass the Id of the closed opportunity to the Visualforce page
            Blob pdfBlob = pdfPage.getContentAsPDF();
            
            // Create and send email using Messaging.SingleEmailMessage class
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(new List<String>{contact.Email});
            email.setSubject('Opportunity Closed: ' + closedOpportunities[0].Name);
            email.setHtmlBody('Hello ' + contact.Name + ',<br/><br/>Please find attached a complete PDF with the details of the closed opportunity.<br/><br/>Best regards,');
            
            // Attach PDF to email
            Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
            attachment.setFileName(closedOpportunities[0].Name + '.pdf');
            attachment.setContentType('application/pdf');
            attachment.setBody(pdfBlob);
            email.setFileAttachments(new List<Messaging.EmailFileAttachment>{attachment});
            
            // Send email
            Messaging.SendEmailResult[] sendResults = Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{email});
        }
    }
}
salesforce apex
© www.soinside.com 2019 - 2024. All rights reserved.