如何使用 TwilioForce APEX 库在 Salesforce 中创建新的潜在客户

发布于 2024-10-07 23:14:59 字数 8213 浏览 5 评论 0原文

大家早上好,

首先,Force.com IDE 和 Salesforce 对我来说是一项新技能。我正在尝试利用 TwilioForce APEX 库:https://www.twilio.com/docs/ salesforce/install

为我客户的 Twilio 帐户上的每个来电创建一个新的 Salesforce 潜在客户。我已经在 Eclipse 中创建了一个新的 Force.com 项目,将 Twilioforce 组件、类和页面复制到该项目中,但需要一些关于如何编写逻辑来创建线索的指导。

我有的问题: 1. 您能否提供参考材料的链接,演示如何以编程方式在 Salesforce 中创建新的潜在客户? 2. 如何测试 TwilioForce 组件,尤其是那些我已更改以反映客户的 Twilio 电话号码和令牌的组件?这些可以从 Eclipse 中的 Force.com 项目内部调用,还是必须从我的developer.org 帐户调用? 3. 一旦我弄清楚如何通过传入的 Twilio 调用完成上述潜在客户创建,我如何将我创建的代码库部署到我的客户?

谢谢, 席

德编辑: EyeScream,你的样本帮了很大的忙。这是 TwilioForce 代码库附带的 TwilioRestResponse 类:

public class TwilioRestResponse {

private String responseText;
private integer httpStatus;
private String url;
private String queryString;
private boolean error;


public TwilioRestResponse(String url, String text, integer status){
    Pattern p = Pattern.compile('([^?]+)\\??(.*)');
    Matcher m = p.matcher(url);
    m.matches();
    this.url = m.group(1);
    this.queryString = m.group(2);
    this.responseText = text;
    this.httpStatus=status;
    this.error = (status>=400);  
}

// getters and setters 
public String getResponseText() {
    return responseText;
}
public void setResponseText(String responseText) {
    this.responseText = responseText;
}
public integer getHttpStatus() {
    return httpStatus;
}
public void setHttpStatus(integer httpStatus) {
    this.httpStatus = httpStatus;
}
public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}
public String getQueryString() {
    return queryString;
}
public void setQueryString(String queryString) {
    this.queryString = queryString;
}
public boolean isError() {
    return error;
}
public void setError(boolean error) {
    this.error = error;
}   
}

还有一个 CallsXmlParser 类,如下所示:

public class CallsXmlParser{
//All Parsed records will be in this list
public List<Call> listRecords = new List<Call>();
//Data Model to store all response elements
public class Call{
    public string Sid{get;set;}
    public string DateCreated{get;set;}
    public string DateUpdated{get;set;}
    public string CallSegmentSid{get;set;}
    public string AccountSid{get;set;}
    public string Called{get;set;}
    public string Caller{get;set;}
    public string PhoneNumberSid{get;set;}
    public string Status{get;set;}
    public string StartTime{get;set;}
    public string EndTime{get;set;}
    public string Duration{get;set;}
    public string Price{get;set;}
    public string Flags{get;set;}
    public string Annotation{get;set;}
}
public CallsXmlParser(){

}
public CallsXmlParser(string data){
    XmlStreamReader xsr = new XmlStreamReader(data);
    listRecords = parse(xsr);
}
public Call[] parse(XmlStreamReader reader) {
    Call[] members = new Call[0];
    while(reader.hasNext()) {
        if (reader.getEventType() == XmlTag.START_ELEMENT) {
           if ('Call' == reader.getLocalName()) {                 
                Call member = parseMember(reader);
                members.add(member);
            }
         }
        reader.next();
    }
    return members;
}
//Parsing Each Call Tag and its nested tags
public Call parseMember(XmlStreamReader reader){
    Call callObject = new Call();
    while(reader.hasNext()) {

       if ('Call' == reader.getLocalName() && reader.getEventType() == XmlTag.END_ELEMENT) {
           break;
        }
        else if('Sid' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Sid = reader.getText(); 
          }
        }else if('DateCreated' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.DateCreated= reader.getText(); 
          }
        }else if('DateUpdated' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.DateUpdated= reader.getText(); 
          }
        }else if('CallSegmentSid' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.CallSegmentSid= reader.getText(); 
          }
        }else if('AccountSid' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.AccountSid= reader.getText(); 
          }
        }else if('Called' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Called= reader.getText(); 
          }
        }else if('Caller' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Caller= reader.getText(); 
          }
        }else if('PhoneNumberSid' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.PhoneNumberSid= reader.getText(); 
          }
        }else if('Status' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Status = reader.getText(); 
          }
        }else if('StartTime' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.StartTime = reader.getText(); 
          }
        }else if('EndTime' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.EndTime = reader.getText(); 
          }
        }else if('Duration' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Duration = reader.getText(); 
          }
        }else if('Price' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Price = reader.getText(); 
          }
        }else if('Flags' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Flags = reader.getText(); 
          }
        }else if('Annotation' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Annotation = reader.getText(); 
          }
        }

       reader.next();
   }
   return callObject;
} 
}

我非常基本的 insertLead 类如下:

public with sharing class insertLead {
Lead1 = new Lead(Phone='TwilioRestResponse.GetResponseText');
}

我需要从 TwilioRestResponse 或 CallXmlParser 类中读取电话号码和 CallerID 名称,然后插入到新潜在客户中的适当字段。 在我的新潜在客户中引用 CallObject.PhoneNumberSid 的适当语法是什么?或者,解析 TwilioRestResponse 是否更好?如果是这样,我如何从 GetResponseText 中仅选择电话号码和 CallerID 名称?

再次感谢, 席德

Good Morning All,

First and foremost, the Force.com IDE and Salesforce is a new skillset for me. I'm attempting to utilize the TwilioForce APEX Library: https://www.twilio.com/docs/salesforce/install

to create a new Salesforce Lead for each incoming call on my client's Twilio account. I've gotten as far as creating a new Force.com project in Eclipse, copying the Twilioforce components, classes and pages to the project, but need some guidance in how to write the logic to create the lead.

Questions I have:
1. Can you provide links to reference material demonstrating how to create a new lead in Salesforce programmtically?
2. How do I test the TwilioForce components, especially those I've changed to reflect my client's Twilio phone number and token? Are these callable from within the Force.com Project in Eclipse, or must they be called from my developer.org account?
3. Once I figure out how to accomplish the above lead creation from incoming Twilio calls, how do I deploy the codebase I've created to my client?

Thanks,
Sid

EDIT:
EyeScream, your sample was a huge help. Here's the TwilioRestResponse class that came with the TwilioForce codebase:

public class TwilioRestResponse {

private String responseText;
private integer httpStatus;
private String url;
private String queryString;
private boolean error;


public TwilioRestResponse(String url, String text, integer status){
    Pattern p = Pattern.compile('([^?]+)\\??(.*)');
    Matcher m = p.matcher(url);
    m.matches();
    this.url = m.group(1);
    this.queryString = m.group(2);
    this.responseText = text;
    this.httpStatus=status;
    this.error = (status>=400);  
}

// getters and setters 
public String getResponseText() {
    return responseText;
}
public void setResponseText(String responseText) {
    this.responseText = responseText;
}
public integer getHttpStatus() {
    return httpStatus;
}
public void setHttpStatus(integer httpStatus) {
    this.httpStatus = httpStatus;
}
public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}
public String getQueryString() {
    return queryString;
}
public void setQueryString(String queryString) {
    this.queryString = queryString;
}
public boolean isError() {
    return error;
}
public void setError(boolean error) {
    this.error = error;
}   
}

There's also a CallsXmlParser class as follows:

public class CallsXmlParser{
//All Parsed records will be in this list
public List<Call> listRecords = new List<Call>();
//Data Model to store all response elements
public class Call{
    public string Sid{get;set;}
    public string DateCreated{get;set;}
    public string DateUpdated{get;set;}
    public string CallSegmentSid{get;set;}
    public string AccountSid{get;set;}
    public string Called{get;set;}
    public string Caller{get;set;}
    public string PhoneNumberSid{get;set;}
    public string Status{get;set;}
    public string StartTime{get;set;}
    public string EndTime{get;set;}
    public string Duration{get;set;}
    public string Price{get;set;}
    public string Flags{get;set;}
    public string Annotation{get;set;}
}
public CallsXmlParser(){

}
public CallsXmlParser(string data){
    XmlStreamReader xsr = new XmlStreamReader(data);
    listRecords = parse(xsr);
}
public Call[] parse(XmlStreamReader reader) {
    Call[] members = new Call[0];
    while(reader.hasNext()) {
        if (reader.getEventType() == XmlTag.START_ELEMENT) {
           if ('Call' == reader.getLocalName()) {                 
                Call member = parseMember(reader);
                members.add(member);
            }
         }
        reader.next();
    }
    return members;
}
//Parsing Each Call Tag and its nested tags
public Call parseMember(XmlStreamReader reader){
    Call callObject = new Call();
    while(reader.hasNext()) {

       if ('Call' == reader.getLocalName() && reader.getEventType() == XmlTag.END_ELEMENT) {
           break;
        }
        else if('Sid' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Sid = reader.getText(); 
          }
        }else if('DateCreated' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.DateCreated= reader.getText(); 
          }
        }else if('DateUpdated' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.DateUpdated= reader.getText(); 
          }
        }else if('CallSegmentSid' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.CallSegmentSid= reader.getText(); 
          }
        }else if('AccountSid' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.AccountSid= reader.getText(); 
          }
        }else if('Called' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Called= reader.getText(); 
          }
        }else if('Caller' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Caller= reader.getText(); 
          }
        }else if('PhoneNumberSid' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.PhoneNumberSid= reader.getText(); 
          }
        }else if('Status' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Status = reader.getText(); 
          }
        }else if('StartTime' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.StartTime = reader.getText(); 
          }
        }else if('EndTime' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.EndTime = reader.getText(); 
          }
        }else if('Duration' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Duration = reader.getText(); 
          }
        }else if('Price' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Price = reader.getText(); 
          }
        }else if('Flags' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Flags = reader.getText(); 
          }
        }else if('Annotation' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
            callObject.Annotation = reader.getText(); 
          }
        }

       reader.next();
   }
   return callObject;
} 
}

My very basic insertLead class is as follows:

public with sharing class insertLead {
Lead1 = new Lead(Phone='TwilioRestResponse.GetResponseText');
}

I need to read in the Phone number and CallerID Name from either the TwilioRestResponse or the CallXmlParser classes and insert into the appropriate fields in a new Lead.
What is the appropriate syntax for referencing the CallObject.PhoneNumberSid in my new Lead? Alternatively, is it better to parse out the TwilioRestResponse? If so, how would I select just the phone number and CallerID Name from the GetResponseText?

Thanks again,
Sid

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随风而去 2024-10-14 23:14:59

答案已更新,请向下滚动

public class SidTest {

    //  1. How to create a new Lead programatically on Force.com (in Apex)
    /* This method is marked as test method, meaning that you can use it to run tests but in the end no data will be saved
        (transaction rollback). You'll need similar code in a class that intercepts messages from Twilio.
    */
    public static testMethod void insertLead(){
        Lead l = new Lead(FirstName='Test', LastName='Lead', Email='[email protected]', Company='test', NumberofEmployees=7);
        insert l;

        // 2. How do I test the TwilioForce components, especially those I've changed to reflect my client's Twilio phone number and token?
    /*  Not sure what do you mean, but most likely by writing test classes like this one and checking their test code coverage.
        See also http://stackoverflow.com/questions/4372202/how-to-unit-test-works-in-salesforce/4381941
        Below sample test that checks if our insert above succeeded.
        You can run it from Eclipse (preferred) or Salesforce GUI in Setup->Develop->Apex classes.
    */
        Lead[] leads = [SELECT Name, Email FROM Lead WHERE Name = 'Test Lead'];

        System.debug(leads);    // if you want to see results in detailed debug log
        System.assertEquals(1, leads.size());
        System.assertEquals('[email protected]', leads[0].Email);
    }
}

以下是在 Eclipse 中运行它时的结果:http://dl.dropbox.com/u/709568/stackoverflow/Sid.png

至于最后一个问题:当您对开发人员版本中测试的功能感到满意时,您应该要求客户授予您访问其组织的“沙盒”的权限。您可以在 Eclipse 中创建一个指向此沙箱的新项目,然后简单地在其中创建所有类,使用真实数据的子集运行测试等。最后,您或客户端的某人将执行到“生产”环境的部署,其中包含代码等内容自动化测试的覆盖率将开始变得重要。

或者,您可以使用您的代码创建一个包,然后在 AppExchange 上出售,就像 Salesforce 的插件一样,任何人都可以下载到他们的组织。当然你可以付费。但目前看来,这似乎是一个太大的跳跃......

希望这可以帮助您开始。


更新:
这是一个快速&基于 TestCallsXmlParser 的脏类包含在您正在使用的代码包中:

public class TestCallsXmlParser{
    @isTest
    public static void TestCallsXmlParserMethod1(){
        CallsXmlParser callxml = new CallsXmlParser();
        String xmlData = '<TwilioResponse> <Calls page=\"0\" numpages=\"1\" pagesize=\"50\" total=\"38\" start=\"0\" end=\"37\"> <Call> <Sid>CA42ed11f93dc08b952027ffbc406d0868</Sid> <DateCreated>Sat, 07 Feb 2009 13:15:19 -0800</DateCreated><DateUpdated>Sat, 07 Feb 2009 13:15:19 -0800</DateUpdated><CallSegmentSid/>';
        xmlData = xmlData + '<AccountSid>AC309475e5fede1b49e100272a8640f438</AccountSid><Called>4159633717</Called><Caller>4156767925</Caller><PhoneNumberSid>PN01234567890123456789012345678900</PhoneNumberSid><Status>2</Status><StartTime>Thu, 03 Apr 2008 04:36:33 -0400</StartTime><EndTime>Thu, 03 Apr 2008 04:36:47 -0400</EndTime>';
        xmlData = xmlData + '<Duration>14</Duration><Price/><Flags>1</Flags></Call>';
        xmlData = xmlData + '<Call><Sid>CA751e8fa0a0105cf26a0d7a9775fb4bfb</Sid><DateCreated>Sat, 07 Feb 2009 13:15:19 -0800</DateCreated><DateUpdated>Sat, 07 Feb 2009 13:15:19 -0800</DateUpdated><CallSegmentSid/>';
        xmlData = xmlData + '<AccountSid>AC309475e5fede1b49e100272a8640f438</AccountSid><Called>2064287985</Called><Caller>4156767925</Caller><PhoneNumberSid>PNd59c2ba27ef48264773edb90476d1674</PhoneNumberSid><Status>2</Status>';
        xmlData = xmlData + '<StartTime>Thu, 03 Apr 2008 01:37:05 -0400</StartTime><EndTime>Thu, 03 Apr 2008 01:37:40 -0400</EndTime><Duration>35</Duration><Price/> <Flags>1</Flags> </Call></Calls></TwilioResponse> ';
        CallsXmlParser callxml1 = new CallsXmlParser(xmlData);

        // eyescream's modification starts here

        // list to store our leads and bulk save them in blocks up to 100 records at 1 insert
        List<Lead> leads = new List<Lead>();

        for(CallsXmlParser.Call c : callxml1.listRecords) {
            System.debug('2 new Leads will be created from phone numbers: ' + c.Caller + ', ' + c.Called);
            leads.add(new Lead(MobilePhone = c.Caller, Company='x', LastName='x')); // Company & Last Name are mandatory fields
            leads.add(new Lead(MobilePhone = c.Called, Company='y', LastName='y')); // without them insert will fail.

            if(leads.size() == 100) { // 100 is the limit of records saved at once. Inserting in batches speeds up execution.
                insert leads;
                leads.clear();
            }
        }

        // If we have any leftovers, we'll insert them too.
        if(leads.size() > 0) {
            insert leads;
            leads.clear();
        }
    }
}

当然您可以进一步改进它,检查重复项,填写真实姓名等...但这大致就是您如何使用解析结果。我不认为自己解析 XML 是一个好的选择...您的问题表明 XML 中将出现更多字段(或者现有字段将用于存储不同的数据),但您可能能够扩展相应的解析器。

Answer updated, please scroll down

public class SidTest {

    //  1. How to create a new Lead programatically on Force.com (in Apex)
    /* This method is marked as test method, meaning that you can use it to run tests but in the end no data will be saved
        (transaction rollback). You'll need similar code in a class that intercepts messages from Twilio.
    */
    public static testMethod void insertLead(){
        Lead l = new Lead(FirstName='Test', LastName='Lead', Email='[email protected]', Company='test', NumberofEmployees=7);
        insert l;

        // 2. How do I test the TwilioForce components, especially those I've changed to reflect my client's Twilio phone number and token?
    /*  Not sure what do you mean, but most likely by writing test classes like this one and checking their test code coverage.
        See also http://stackoverflow.com/questions/4372202/how-to-unit-test-works-in-salesforce/4381941
        Below sample test that checks if our insert above succeeded.
        You can run it from Eclipse (preferred) or Salesforce GUI in Setup->Develop->Apex classes.
    */
        Lead[] leads = [SELECT Name, Email FROM Lead WHERE Name = 'Test Lead'];

        System.debug(leads);    // if you want to see results in detailed debug log
        System.assertEquals(1, leads.size());
        System.assertEquals('[email protected]', leads[0].Email);
    }
}

Here's the outcome when you'll run it in Eclipse: http://dl.dropbox.com/u/709568/stackoverflow/Sid.png

As for last question: when you'll be happy with functionality tested in your Developer Edition, you should ask client to grant you access to "Sandbox" of his organisation. You can create in Eclipse a new project pointing to this sandbox and simply create all classes in it, run tests with a subset of real data etc. Finally you or somebody from client's side will perform a deployment to "production" environment where stuff like code coverage in automated tests will start to matter.

Alternatively you could create a package with your code and sell it on AppExchange like a plugin to Salesforce that anybody can download to their organisation. You can charge for it of course. But this looks like a too big jump for now...

Hope this can get you started.


Update:
Here's a quick & dirty class based on TestCallsXmlParser that's included in code package you are using:

public class TestCallsXmlParser{
    @isTest
    public static void TestCallsXmlParserMethod1(){
        CallsXmlParser callxml = new CallsXmlParser();
        String xmlData = '<TwilioResponse> <Calls page=\"0\" numpages=\"1\" pagesize=\"50\" total=\"38\" start=\"0\" end=\"37\"> <Call> <Sid>CA42ed11f93dc08b952027ffbc406d0868</Sid> <DateCreated>Sat, 07 Feb 2009 13:15:19 -0800</DateCreated><DateUpdated>Sat, 07 Feb 2009 13:15:19 -0800</DateUpdated><CallSegmentSid/>';
        xmlData = xmlData + '<AccountSid>AC309475e5fede1b49e100272a8640f438</AccountSid><Called>4159633717</Called><Caller>4156767925</Caller><PhoneNumberSid>PN01234567890123456789012345678900</PhoneNumberSid><Status>2</Status><StartTime>Thu, 03 Apr 2008 04:36:33 -0400</StartTime><EndTime>Thu, 03 Apr 2008 04:36:47 -0400</EndTime>';
        xmlData = xmlData + '<Duration>14</Duration><Price/><Flags>1</Flags></Call>';
        xmlData = xmlData + '<Call><Sid>CA751e8fa0a0105cf26a0d7a9775fb4bfb</Sid><DateCreated>Sat, 07 Feb 2009 13:15:19 -0800</DateCreated><DateUpdated>Sat, 07 Feb 2009 13:15:19 -0800</DateUpdated><CallSegmentSid/>';
        xmlData = xmlData + '<AccountSid>AC309475e5fede1b49e100272a8640f438</AccountSid><Called>2064287985</Called><Caller>4156767925</Caller><PhoneNumberSid>PNd59c2ba27ef48264773edb90476d1674</PhoneNumberSid><Status>2</Status>';
        xmlData = xmlData + '<StartTime>Thu, 03 Apr 2008 01:37:05 -0400</StartTime><EndTime>Thu, 03 Apr 2008 01:37:40 -0400</EndTime><Duration>35</Duration><Price/> <Flags>1</Flags> </Call></Calls></TwilioResponse> ';
        CallsXmlParser callxml1 = new CallsXmlParser(xmlData);

        // eyescream's modification starts here

        // list to store our leads and bulk save them in blocks up to 100 records at 1 insert
        List<Lead> leads = new List<Lead>();

        for(CallsXmlParser.Call c : callxml1.listRecords) {
            System.debug('2 new Leads will be created from phone numbers: ' + c.Caller + ', ' + c.Called);
            leads.add(new Lead(MobilePhone = c.Caller, Company='x', LastName='x')); // Company & Last Name are mandatory fields
            leads.add(new Lead(MobilePhone = c.Called, Company='y', LastName='y')); // without them insert will fail.

            if(leads.size() == 100) { // 100 is the limit of records saved at once. Inserting in batches speeds up execution.
                insert leads;
                leads.clear();
            }
        }

        // If we have any leftovers, we'll insert them too.
        if(leads.size() > 0) {
            insert leads;
            leads.clear();
        }
    }
}

Of course you can improve it further, check for duplicates, fill with real names etc... but that's roughly how you can use parsed results. I don't think that parsing the XML yourself would be a good option... Your question suggests that more fields will come in the XML (or existing ones will be used to store different data) but probably you'll be able to expand the parser accordingly.

我不会写诗 2024-10-14 23:14:59

如果有人在部署 twilio 库时遇到问题,这里提供了很好的详细信息,可以完成大部分任务。一些如果它可以帮助http://redcurrantscloud.blogspot.in/

谢谢。

If anyone have problem in deploying twilio library here are good details provided, which do most of the tasks. Some how if it could help http://redcurrantscloud.blogspot.in/

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文