0

My requirement I have an XML file with a large number of address data. I need to Insert/Upload it to the Salesforce Account object in the address field.

This is the output I need to get.

enter image description here

This code I have tried but unable to insert the address data into the Account object(Address field)

Updated the code as mentioned. but unable to insert address fields in Account object

public class Xmldata {
  
 public static void parseContactAddress() {
  String s = 
'<Content '+
    'xmlns="urn:crif-CrifGate:2:1">'+
    '<CES_ServiceOutput>'+
        '<CCR_Output>'+
       '<id type="integer">7</id>'+
        '<CG_CCR_Pipe>'+
      ' <id type="integer">7</id>'+
    '<CB_NAE_ProductOutput '+
'xmlns:cb="urn:crif-creditbureau:v1"> <address>'+
      '<name>Kirk Stevens</name>'+
      '<street1>808 State St</street1>'+
      '<street2>Apt. 2</street2>'+
      '<city>Palookaville</city>'+
      '<state>PA</state>'+
      '<country>USA</country>'+
  '</address>'+ 
   '</CB_NAE_ProductOutput>'+
            '</CG_CCR_Pipe>'+
        '</CCR_Output>'+
         '</CES_ServiceOutput>'+
            
'</Content>';
 
    
List<Account> newaccounts = new List<Account> ();

Dom.Document docx = new Dom.Document();
docx.load(s);
dom.XmlNode xroot = docx.getrootelement() ;

dom.XmlNode [] xrec = xroot.getchildelements() ; //Get all Record Elements

for(Dom.XMLNode child : xrec) //Loop Through Records
{
 account a = new Account ();

    for (dom.XmlNode awr : child.getchildren() ) 
        for (Dom.XMLNode grandChild : child.getChildElements()) {
            for (Dom.XMLNode greatGrandchild : grandChild.getChildElements()) {
                if (greatGrandchild.getName() == 'CG_CCR_Pipe') {
                    for (Dom.XMLNode greatGreatGrandchild : greatGrandchild.getChildElements()) {
                        if (greatGreatGrandchild.getName() == 'CB_NAE_ProductOutput') {
                            
                            a.accountnumber = greatGreatGrandchild.getText();
                            for (Dom.XmlNode greatGreatGreatGrandchild : greatGreatGrandchild.getChildren()) {
                                if (greatGreatGreatGrandchild.getName() == 'address') {
                                    for (Dom.XmlNode addressChild : greatGreatGreatGrandchild.getChildElements()) {
                                        switch on addressChild.getName() {
                                            when 'name' {
                                                a.Name = addressChild.getText().trim();
                                            
                        String childText = addressChild.getText();
                        switch on addressChild.getName() {
                            
                            when 'name' {
                                a.Name = childText;
                            }
                            when 'street1' {
                                a.BillingStreet = childText;
                            }
                            when 'street2' {
                                a.BillingStreet = child.getText().trim();
                            }
                            when 'city' {
                                a.BillingCity = childText;
                            }
                            when 'state' {
                                a.BillingState= childText;
                            }
                            when 'country' {
                                a.BillingCountry = childText;
                            }
                        }
                    }
                }
            }
        }
    }
}
  }
  }
            }
   newaccounts.add(a);

}
system.debug(newaccounts);//you could insert here or upsert based on ID with an external Id field
 }
        
}
 }
1
  • 1
    What errors are you getting?
    – nbrown
    Mar 8, 2021 at 14:00

1 Answer 1

0

In this model you have at least 6 layers within the parent-child (or many grandchildren) relationship which will result in confusion for the developer just as much the reader. To access the address node you will need 5 for loops before ensuring the node is actually address and then a following for loop to collect the child elements of this node.

    for (Dom.XMLNode child : xroot.getChildElements()) {
        for (Dom.XMLNode grandChild : child.getChildElements()) {
            for (Dom.XMLNode greatGrandchild : grandChild.getChildElements()) {
                if (greatGrandchild.getName() == 'CG_CCR_Pipe') {
                    for (Dom.XMLNode greatGreatGrandchild : greatGrandchild.getChildElements()) {
                        if (greatGreatGrandchild.getName() == 'CB_NAE_ProductOutput') {
                            Account a = new Account ();
                            a.accountnumber = greatGreatGrandchild.getText();
                            for (Dom.XmlNode greatGreatGreatGrandchild : greatGreatGrandchild.getChildren()) {
                                if (greatGreatGreatGrandchild.getName() == 'address') {
                                    for (Dom.XmlNode addressChild : greatGreatGreatGrandchild.getChildElements()) {
                                        switch on addressChild.getName() {
                                            when 'name' {
                                                a.Name = addressChild.getText().trim();
                                            }
                                            .....

Very difficult to read and furthermore extend leading to maintenance and versioning issues down the line.

You also need to add the new Account record into your collection to be committed to the DB i.e newaccounts.add(a);. FYI, your XML in the question will never result in an Account with BillingAddress in Texas, always PA.

Apart from the XML issue, I strongly recommend you look into an xmlToJson parser. This will provide many benefits from flexibility through to management and support for extended SObjects.

0

You must log in to answer this question.