10

我们必须通过 REST -OAuth 调用将潜在客户转换为帐户。我们能够创建、更新(编辑)和详细线索字段,但无法转换它们。

我们发现通过 SOAP API 也可以做到这一点,但我们只遵循 REST OAuth。

4

2 回答 2

14

是的,我们通过为 REST 调用创建 Apex 类解决了这个问题。示例代码是这样的 -

@RestResource(urlMapping='/Lead/*')
global with sharing class RestLeadConvert {            

    @HttpGet
    global static String doGet() {
        String ret = 'fail';
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String leadId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);              
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(leadId);

        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);           
        Database.LeadConvertResult lcr ;
        try{
            lcr = Database.convertLead(lc);
            system.debug('*****lcr.isSuccess()'+lcr.isSuccess());            
            ret = 'ok';
        }
        catch(exception ex){
            system.debug('***NOT CONVERTED**');           
        }
        return ret;
    }   
}

你可以使用这个调用

<Your Instance URL>/services/apexrest/Lead/<LeadId>

该测试将为您提供大约 93% 的覆盖率。

@isTest
public class RestLeadConvertTest{

    static testMethod void testHttpGet() {
        Lead l = new Lead();
        l.FirstName = 'First';
        l.LastName = 'Last';
        l.Company = 'Unit Test';
        insert l;

        Test.startTest();
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        req.requestURI = '/Lead/' + l.Id;
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;
        RestLeadConvert.doGet();
        Test.stopTest();
    }

}
于 2013-11-06T17:47:59.193 回答
0

您可以构建一次性 SOAP 请求来转换潜在客户并使用您已有的用于 REST API 的 OAuth 令牌。

请求正文应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ens="urn:sobject.partner.soap.sforce.com" xmlns:fns="urn:fault.partner.soap.sforce.com" xmlns:tns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Header>
      <tns:SessionHeader>
         <sessionId>YOUR_OAUTH_TOKEN</sessionId>
      </tns:SessionHeader>
   </soap:Header>
   <soap:Body>
      <tns:convertLead>
         <tns:leadConverts>
            <tns:leadId>YOUR_LEAD_ID</tns:leadId>
            <tns:convertedStatus>Closed - Converted</tns:convertedStatus>
         </tns:leadConverts>
      </tns:convertLead>
   </soap:Body>
</soap:Envelope>
curl -H 'SOAPAction: null' -H 'Content-Type: text/xml' --data BODY_FROM_ABOVE https://your-instance-url/services/Soap/u/52.0

请注意,SOAPAction标题是必需的,即使 Salesforce 不使用它。

结果将以 XML 形式返回,类似于:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header>
      <LimitInfoHeader>
         <limitInfo>
            <current>91</current>
            <limit>15000</limit>
            <type>API REQUESTS</type>
         </limitInfo>
      </LimitInfoHeader>
   </soapenv:Header>
   <soapenv:Body>
      <convertLeadResponse>
         <result>
            <accountId>0015x00002C95kMAAR</accountId>
            <contactId>0035x00003NjdeyAAB</contactId>
            <leadId>00Q5x00001tHg1tEAC</leadId>
            <opportunityId>0065x000025fDsWAAU</opportunityId>
            <success>true</success>
         </result>
      </convertLeadResponse>
   </soapenv:Body>
</soapenv:Envelope>

如果您对 JSON 比 XML 更满意,OneGraph 提供了一个 GraphQL API 来包装 convertLead 功能。

最好创建您自己的 OneGraph 应用程序以获取自定义 app_id,但此处提供一个用于演示目的。

GraphQL 查询将是:

mutation ConvertLead {
  salesforce(
    auths: {
      salesforceOAuth: {
        instanceUrl: "YOUR_INSTANCE_URL"
        token: "YOUR_OAUTH_TOKEN"
      }
    }
  ) {
    convertLead(
      input: { leadConverts: [{ leadId: "YOUR_LEAD_ID" }] }
    ) {
      leadConverts {
        lead {
          id
          name
        }
        account {
          name
          id
        }
        contact {
          name
          id
        }
        opportunity {
          name
          id
        }
        success
        errors {
          message
          statusCode
        }
      }
    }
  }
}

然后请求将如下所示:

curl -H 'Content-Type: application/json' 'https://serve.onegraph.com/graphql?app_id=4687c59d-8f9c-494a-ab67-896fd706cee9' --data '{"query": "QUERY_FROM_ABOVE"}'

结果将以 JSON 格式返回,如下所示:

{
  "data": {
    "salesforce": {
      "convertLead": {
        "leadConverts": [
          {
            "lead": {
              "id": "00Q5x00001tHg1tEAC",
              "name": "Daniel Boone"
            },
            "account": {
              "name": "Company",
              "id": "0015x00002C95kMAAR"
            },
            "contact": {
              "name": "Daniel Boone",
              "id": "0035x00003NjdeyAAB"
            },
            "opportunity": {
              "name": "New Opportunity",
              "id": "0065x000025fDsWAAU"
            },
            "relatedPersonAccountId": null,
            "success": true,
            "errors": []
          }
        ]
      }
    }
  }
}
于 2021-08-27T22:12:42.820 回答