You need to sign in to do that
Don't have an account?

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();
}
}
}
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();
}
}
}
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
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.
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.
Now i am trying to convert this to as invokeable, Futurecallout Class so that i can utilize this in Process Builder.
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 ?