function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Cloud-LingCloud-Ling 

HELP with trigger and XML

Greetings,

 

Now I have to create a trigger on Account that would parse the information(Name__c, Phone__c, Email__c, Address__c) in an XML format. I have to parse the information upon creating new record on Account and save the data to its appropriate fields on Contact object.

 

i.e. XML file --

 

<?xml version="1.0" ?> 
<!--My first XML document --> 
   <information> 
        <name>
	  <first_name>Seed</first_name>
	  <last_name>Lins</last_name>
	</name>
        <phone>888-8888-88</phone> 
        <email>email@email.com</email>
        <address>Texas,USA</address> 
   
        <name>
	  <first_name>R</first_name>
	  <last_name>Coholick</last_name>
	</name>
        <phone>662-546-54546</phone> 
        <email>email@email.com</email>
        <address>Denver,USA</address> 
  </information>

 

 

 

What am I gonna do?? I'm really confuse. Please help. Any ideas are welcome.

 

 

Thanks,

C-L

Cory CowgillCory Cowgill

I have used XMLStreamReader class alot in past. This is what you'll want to use to parse your XML.

 

Salesforcealready  has an example for what you want right here:

 

http://blog.sforce.com/sforce/2008/11/got-xml.html

Cloud-LingCloud-Ling

Cory,

 

Thanks for the link, it was a big big help. Now I've created a class code and it didn't get errors so far, it look like this :

 

 

public class SimpleXMLExample
{

   public class TempAccount
   {
      String name;
      String address;
      String phone;
      String email;
   }

   TempAccount[] parseAccounts(XMLStreamReader acc) 
   {
       TempAccount[] accts = new TempAccount[0];
       while (acc.hasNext())
          {
           if (acc.getEventType() == XmlTag.START_ELEMENT)
            {
              if(acc.getLocalName() == 'https://ap1.salesforce.com/resource/1291840986000/xml')
               {
                 accts.add(parseAccount(acc));
               }
            }
            acc.next();
          }
        return accts;
     }

      TempAccount parseAccount(XmlStreamReader acc) {
          TempAccount tmpacct = new TempAccount();
           tmpacct.name = acc.getAttributeValue('', 'name');
           while(acc.hasNext()) {
                if (acc.getEventType() == XmlTag.END_ELEMENT) {
                       break;
                } else if (acc.getEventType() == XmlTag.CHARACTERS) {
                       tmpacct.name = acc.getText();
                       tmpacct.address = acc.getText();
                       tmpacct.phone = acc.getText();
                       tmpacct.email = acc.getText();
                 }
                 acc.next();
            }
          return tmpacct;
        }

}

 

 

now, I've got to make a trigger that'll save/show the data to its appropriate fields on Contact. another question sir, how do I relate the Account to Contact?

Cory CowgillCory Cowgill

All you need to do is populate the Contact.AccountID field with the ID of the Account. That will relate a Contact to an Acccount.

 

Once you have the Contact related to the Account, you don't need to use Trigger code to populate Account data on a Contact. You can use Custom Formula Fields to pull down Account values onto the Contact. You don't need to write trigger logic to do that piece.