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
Aditya Singh 37Aditya Singh 37 

Parse returned XML data and Update Record

Hi Guys, 

I am pretty new to Salesforce Apex development and recently I have created a @futurecallout class in Apex which gets invoked by a Lead Trigger (after Insert). In the class i am doing a POST Call (sending Lead Info) and the external system is returning an XML with a message( I am able to see that in debug log). Now I need to parse the same XML data extract a value (UID) and Update the Lead record i am sending to get updated with the value UID as received in XML. 

Please Help

public class WS FromLead {
   
   @future (callout=true)
    public static void sendNotification(id lid, String username) {
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        req.setEndpoint("Endpoint URL");
        req.setMethod('POST');
        req.setHeader('Content-Type','application/x-www-form-urlencoded');
       req.setBody(
        'username='+EncodingUtil.urlEncode(username, 'UTF-8' );  
        req.setTimeout(120000);
        try {
         res = http.send(req);
         System.debug(res.getbody());
         // Generate the HTTP response as an XML stream
         XmlStreamReader reader = res.getXmlStreamReader();
         // Read through the XML
        System.debug('Event Type:' + reader.getEventType());
        if (reader.getEventType() == XmlTag.START_ELEMENT)
           {
            System.debug(reader.getLocalName());
           }
        } 
        catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
            res.getbody();
        }
        }
        }
Best Answer chosen by Aditya Singh 37
UC InnovationUC Innovation
You can use the below sample code to get the UID:
 
String responseXML = '<Activated UId=\"abccompany-LUDL7N\">Account has been registered, you may now log into your new account.</Activated>';

DOM.Document doc = new DOM.Document();
String toParse = responseXML;
doc.load(toParse);

DOM.XMLNode root = doc.getRootElement();

String uid = root.getAttributeValue('UId', null);
System.Debug('uid: ' + uid);

Replase responseXML with your response body.

To update the lead, you can just query for the lead record with lid, update the field you need, and then do an update on the lead record.

Hope that helps.

All Answers

UC InnovationUC Innovation
What does the content of the XML look like?
Aditya Singh 37Aditya Singh 37
I can see the XML data in Debug Log as give below:
USER_DEBUG|[46]|DEBUG|<Activated UId="abccompany-LUDL7N">Account has been registered, you may now log into your new account.</Activated>  

I need to get the UID updated on the lead record. 
UC InnovationUC Innovation
You can use the below sample code to get the UID:
 
String responseXML = '<Activated UId=\"abccompany-LUDL7N\">Account has been registered, you may now log into your new account.</Activated>';

DOM.Document doc = new DOM.Document();
String toParse = responseXML;
doc.load(toParse);

DOM.XMLNode root = doc.getRootElement();

String uid = root.getAttributeValue('UId', null);
System.Debug('uid: ' + uid);

Replase responseXML with your response body.

To update the lead, you can just query for the lead record with lid, update the field you need, and then do an update on the lead record.

Hope that helps.
This was selected as the best answer
UC InnovationUC Innovation
Aditya, did this work for you?
Aditya Singh 37Aditya Singh 37
@UC Innovation Yup, it worked. Thanks so Much.
Now i am trying to convert this to as invokeable, Futurecallout Class so that i can utilize this in Process Builder. 
UC InnovationUC Innovation
No problem.  Glad to know that you were able to get things moving forward!
Aditya Singh 37Aditya Singh 37
@UC Innovation 

If i want to call this Apex class with a click of button. 

For the Custom Button this is the code
{!REQUIRESCRIPT("/soap/ajax/35.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/35.0/apex.js")}
var result = sforce.apex.execute("WS FromLead","sendNotification", {Id:'{!Lead.Id}'}, {Email:'{!Lead.Email}'}, {Firstname:'{!Lead.FirstName}'}, ); alert(result); window.location.reload();

Should this work ?
UC InnovationUC Innovation
I believe so. I've done something similar in the past.