0

How to get the phone, email,name,company fileds the below Xml response to insert in salesforce lead object

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" xmlns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd">
    <businessCard imageRotation="noRotation">
        <field type="Phone">
            <value>248.123 4567</value>
        </field>
        <field type="Phone">
            <value>248.123.4568</value>
        </field>
        <field type="Email">
            <value>apalncksgtfisSofO.com</value>
        </field>
        <field type="Address">
            <value>1234 Grand R.vor Drive • Farmmgton. Ml • 48337</value>
        </field>
        <field type="Name">
            <value>Alex Patrick</value>
        </field>
        <field type="Company">
            <value>tfisSofO</value>
        </field>
        <field type="Text">
            <value>A
TRI-SHORE
IfUSURANCC
Alex Patrick
oHico: 248.123 4567
lax: 248.123.4568
email apalncksgtfisSofO.com
1234 Grand R.vor Drive • Farmmgton. Ml • 48337</value>
        </field>
    </businessCard>
</document>

can anyone please help? i want to insert those data into lead object in the form name, phone, email etc from the above response?

Here's what I tried so far and where I'm stuck:

Dom.Document d = new Dom.Document();
d.load(respp.getbody()); //respp.body() hold the above xml response.
String field = d.getRootElement().getChildElement('field', null).getAttribute('Email', null);
String email=field.getText();
  • One of the ideas my be to write an apex code, that parse XML and make a lead. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… and developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… can give you enough information to make first approach. Alternatively, you can convert that to CSV somehow and use DataLoader to insert to salesforce. Or use Pentaho Spoon to make your own transformation that loads data to salesforce. – kurunve Jun 9 '17 at 11:40
  • I got the above response from one of the http callout , now i need to parse the above xml response and insert the data into lead object (like name from the xml response = lead name etc) – Manikon Jun 9 '17 at 12:02
  • Dom.Document d = new Dom.Document(); d.load(respp.getbody()); //respp.body() hold the above xml response. String field = d.getRootElement().getChildElement('field', null).getAttribute('Email', null); String email=field.getText(); i struck here, i am not sure how to proceed next – Manikon Jun 9 '17 at 12:19
1

You may need to loop through the businessCard node child element and the the field type attribute and check against it and set your field values. You can also use an SObject map the type to field based on conditions. This will get you started I think. I used the xml you provided, and I needed to use the namespace parameter to get some of the nodes.

String xmlData = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>' +    
'<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" xmlns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd">' +
    '<businessCard imageRotation="noRotation">' +
        '<field type="Phone">' +
            '<value>248.123 4567</value>' +
        '</field>' +
        '<field type="Phone">' +
            '<value>248.123.4568</value>' +
        '</field>' +
       '<field type="Email">' +
            '<value>apalncksgtfisSofO.com</value>' +
        '</field>' +
        '<field type="Address">' +
            '<value>1234 Grand R.vor Drive • Farmmgton. Ml • 48337</value>' +
        '</field>' +
        '<field type="Name">' +
            '<value>Alex Patrick</value>' +
        '</field>' +
        '<field type="Company">' +
            '<value>tfisSofO</value>' +
        '</field>' +
        '<field type="Text">' +
            '<value>A TRI-SHORE INSURANCE' +
                   'Alex Patrick' +
                   'oHico: 248.123 4567' +
                   'fax: 248.123.4568' +
                   'email apalncksgtfisSofO.com' +
                   '1234 Grand R.vor Drive • Farmmgton. Ml • 48337' +
           '</value>' +
        '</field>' +
   '</businessCard>' +
'</document>';
Dom.Document doc = new Dom.Document();
doc.load(xmlData);
Dom.XMLNode root = doc.getRootElement();
Dom.XMLNode businessCard = root.getChildElement('businessCard', 'http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd');
Lead l = new Lead();
for (Dom.XmlNode node : businessCard.getChildren()) {
    if (node.getAttribute('type', null) != null) {
        if (node.getAttribute('type', null) == 'Phone') {
            // set the lead phone
            if (node.getChildElement('value', 'http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd') != null) {
                l.Phone = node.getChildElement('value', 'http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd').getText();
            }
            // add other type condition checks
        }       
    }
}
0

I tried to modify the Andrew L's answer. I see that your Email is not in correct format. So for testing I added @gmail.com like this (apalncksgtfisSofO@gmail.com).

String ROOT_TAG = 'http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd';
String xmlData = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>' +    
'<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" xmlns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd">' +
    '<businessCard imageRotation="noRotation">' +
        '<field type="Phone">' +
            '<value>248.123 4567</value>' +
        '</field>' +
        '<field type="Phone">' +
            '<value>248.123.4568</value>' +
        '</field>' +
       '<field type="Email">' +
            '<value>apalncksgtfisSofO@gmail.com</value>' +
        '</field>' +
        '<field type="Address">' +
            '<value>1234 Grand R.vor Drive • Farmmgton. Ml • 48337</value>' +
        '</field>' +
        '<field type="Name">' +
            '<value>Alex Patrick</value>' +
        '</field>' +
        '<field type="Company">' +
            '<value>tfisSofO</value>' +
        '</field>' +
        '<field type="Text">' +
            '<value>A TRI-SHORE INSURANCE' +
                   'Alex Patrick' +
                   'oHico: 248.123 4567' +
                   'fax: 248.123.4568' +
                   'email apalncksgtfisSofO.com' +
                   '1234 Grand R.vor Drive • Farmmgton. Ml • 48337' +
           '</value>' +
        '</field>' +
   '</businessCard>' +
'</document>';
Dom.Document doc = new Dom.Document();
doc.load(xmlData);
Dom.XMLNode root = doc.getRootElement();
Dom.XMLNode businessCard = root.getChildElement('businessCard', 'http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd');
Lead l = new Lead();

for (Dom.XmlNode node : businessCard.getChildren()) {
    if (node.getAttribute('type', null) != null) {
        if (node.getAttribute('type', null) == 'Phone') {
            if (node.getChildElement('value', ROOT_TAG) != null) {
                l.Phone = node.getChildElement('value', ROOT_TAG).getText();
            }
        }
        if (node.getAttribute('type', null) == 'Email') {
            if (node.getChildElement('value', ROOT_TAG) != null) {
                l.Email = node.getChildElement('value', ROOT_TAG).getText();
            }
        }
        if (node.getAttribute('type', null) == 'Company') {
            if (node.getChildElement('value', ROOT_TAG) != null) {
                l.Company = node.getChildElement('value', ROOT_TAG).getText();
            }
        }
        if (node.getAttribute('type', null) == 'Name') {
            if (node.getChildElement('value', ROOT_TAG) != null) {
                String name = node.getChildElement('value', ROOT_TAG).getText();
                String[] splited = name.split('\\s+');
                if(splited[0] != null || splited[0] != '')
                    l.FirstName = splited[0];

                if(splited[1] != null || splited[1] != '') 
                    l.LastName = splited[1];
            }
        }

        if (node.getAttribute('type', null) == 'Address') {
            if (node.getChildElement('value', ROOT_TAG) != null) {
                String address = node.getChildElement('value', ROOT_TAG).getText();
                String[] updatedAdd = address.split('•');
                if(updatedAdd != null && (updatedAdd[0] != null || updatedAdd[0] != ''))
                    l.street = updatedAdd[0];

                if(updatedAdd != null && (updatedAdd[1] != null || updatedAdd[1] != '')) 
                    l.city = updatedAdd[1];

                if(updatedAdd != null && (updatedAdd[2] != null || updatedAdd[2] != '')) 
                    l.postalCode = updatedAdd[2];
            }
        }
    }
}
insert l;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.