从“自定义”按钮调用Apex方法

时间:2019-05-02 16:25:33

标签: salesforce apex

我创建了

这样的Apex类
Public class test_Controller
{

    Public test_Controller() { }

    Public void send(set<id> oppIds)
    {
        List<Oppurtunity> OppLst =[Select Name from Opportunity where id IN: oppIds];

        Private static string integration_key = 'abcd';
        Private static string account_id = 'efgh';

        Public string signer_email { get; set; }   
        Public string signer_name { get; set; }   
        Public string email_message { get; set; }  
        Public string output { get; set; }
        Public string envelope_id { get; set; }
        Public string error_code { get; set; } 
        Public string error_message { get; set; }

        Private static string ds_server = 'callout:DocuSign_Legacy_Demo/api/3.0/dsapi.asmx';

        Private static string trace_value = 'SFDC_004_SOAP_email_send'; // Used for tracing API calls
        Private static string trace_key = 'X-ray';
        Private DocuSignTK.APIServiceSoap api_sender = new DocuSignTK.APIServiceSoap();

        configure_sender();
        do_send(OppLst);

     }

    Private void configure_sender()
    {
        api_sender.endpoint_x = ds_server;
        api_sender.inputHttpHeaders_x = new Map<String, String>();
        String auth = '<DocuSignCredentials><Username>{!$Credential.Username}</Username>'
                      + '<Password>{!$Credential.Password}</Password>'
                      + '<IntegratorKey>' + integration_key + '</IntegratorKey></DocuSignCredentials>';

        api_sender.inputHttpHeaders_x.put('X-DocuSign-Authentication', auth);
    }

    Private void do_send(List<Oppurtunity> OppurLst)
    {

        if (integration_key == 'xxx-xxxx-xxxx' ||
            account_id == 'xxxx-xxxx-xxxx')
        {
            error_message = 'Please configure the Apex class DS_Recipe_Send_Env_Email_Controller with your integration key and account id.';
            error_code = 'CONFIGURATION_PROBLEM';
            return;
        }

        String file_contents = '<html><h1>Quote Document</h1>' + get_lorem(OppurLst)
            + '<p>&nbsp;</p>'
            + '<p>Signature: <span style="color:white;">signer1sig</span></p>'
            + '<p>Date: <span style="color:white;">signer1date</span></p></html>';

        DocuSignTK.Document document = new DocuSignTK.Document();
        document.ID = 1;
        document.Name = 'Quote Document';
        document.FileExtension = 'html';
        document.pdfBytes = EncodingUtil.base64Encode(Blob.valueOf(file_contents));


        DocuSignTK.Recipient recipient = new DocuSignTK.Recipient();
        recipient.Email = '';
        recipient.UserName = '';
        recipient.ID = 1;
        recipient.Type_x = 'Signer';
        recipient.RoutingOrder = 1;

        DocuSignTK.Tab signHereTab = new DocuSignTK.Tab();
        signHereTab.Type_x = 'SignHere';
        signHereTab.AnchorTabItem = new DocuSignTK.AnchorTab();
        signHereTab.AnchorTabItem.AnchorTabString = 'signer1sig'; // Anchored for doc 1
        signHereTab.AnchorTabItem.XOffset = 8;
        signHereTab.RecipientID = 1;
        signHereTab.Name = 'Please sign here';
        signHereTab.ScaleValue = 1;
        signHereTab.TabLabel = 'signer1sig';

        DocuSignTK.Tab dateSignedTab = new DocuSignTK.Tab();
        dateSignedTab.Type_x = 'DateSigned';
        dateSignedTab.AnchorTabItem = new DocuSignTK.AnchorTab();
        dateSignedTab.AnchorTabItem.AnchorTabString = 'signer1date'; // Anchored for doc 1
        dateSignedTab.AnchorTabItem.YOffset = -6;
        dateSignedTab.RecipientID = 1;
        dateSignedTab.Name = 'Date Signed';
        dateSignedTab.TabLabel = 'date_signed';


        DocuSignTK.Envelope envelope = new DocuSignTK.Envelope();
        envelope.Subject = 'Please sign the Quote Document'; 
        envelope.AccountId = account_id;
        envelope.Tabs = new DocuSignTK.ArrayOfTab();
        envelope.Tabs.Tab = new DocuSignTK.Tab[2];
        envelope.Tabs.Tab.add(signHereTab);
        envelope.Tabs.Tab.add(dateSignedTab);
        envelope.Recipients = new DocuSignTK.ArrayOfRecipient();
        envelope.Recipients.Recipient = new DocuSignTK.Recipient[1];
        envelope.Recipients.Recipient.add(recipient);
        envelope.Documents = new DocuSignTK.ArrayOfDocument();
        envelope.Documents.Document = new DocuSignTK.Document[1];
        envelope.Documents.Document.add(document);
        if (String.isNotBlank(email_message))
        {
            envelope.EmailBlurb = email_message;
        }

        try
        {
            DocuSignTK.EnvelopeStatus result = api_sender.CreateAndSendEnvelope(envelope);
            envelope_id = result.EnvelopeID;
            System.debug('Returned successfully, envelope_id = ' + envelope_id);
        }
        catch (CalloutException e)
        {
            System.debug('Exception - ' + e);
            error_code = 'Problem: ' + e;
            error_message = error_code;
        }
    }

    Private Boolean no_error()
    {
        return (String.isEmpty(error_code));
    }

    Private static String get_lorem(List<Oppurtunity> OLst)
    {
        String lorem = 'Hello World';
        return lorem;
    }
}

我正在尝试通过

的“自定义按钮”从Apex调用send方法
{!REQUIRESCRIPT("/soap/ajax/41.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/41.0/apex.js")} 

sforce.apex.execute("test_Controller","send",{oppIds:"{!Opportunity.Id}"});

当我尝试执行按钮时,出现以下错误消息:

  

此按钮或链接的OnClick JavaScript出现问题   遇到:{故障代码:''soapenv:Client',故障字符串:'无操作   可应要求   请{http://soap.sforce.com/schemas/package/test_Controller}发送   检查服务的WSDL。'}'

我是Salesforce和Apex脚本的新手。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您的方法需要一个ID列表。

 Public void send(set<id> oppIds)

所以我猜想也许您需要将其作为数组发送?

sforce.apex.execute("test_Controller","send",{oppIds:["{!Opportunity.Id}"]});

答案 1 :(得分:0)

您需要将此方法公开为webservice,并且必须为static

webservice static void send(List<Id> oppIds) {

}

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_and_ajax.htm