XML Serialization in Salesforce
From below Post you will learn how to do XML serialization in simple process.
JSON serialization is very simple in salesforce. we can do it by using JSON.serialization() method.
below simple example for getting contract JSON data.
// Start
// fetching records from database
List<contact> clist=[select id,name,email from contact limit 10];
// serialization of records in JSON
string convertedContactJsonString = JSON.serialization(clist);
system.debug(convertedContactJsonString +'JSON Data');
// end
But If you need bulk of contact records in XML format... how to do..?
we dont have XML serialization method like above JSON example to generate result with single line of code. And XMLStreamWriter is not a good idea for Bulk records process.
what you need before understanding.
1. @restresource
2. HrttpRest request.
Requirement :
Third party system needs contact information in XML via SOAP service or you need generate contacts in XML format from apex code.
here we go.....!
// this class is to return list of conatacts
@restresource(/urlmapping='/xmlSerialization/*')
global class XmlListExposer {
@httpPost
global static List<contact> contactListEextractor(){
return [select id,name , email from contact limit 100];
}
}
// class 2 for expose SOAP service
global class SoapClassToCallInternalHttpClass{
webservice static staing callRestMethodForXml(){
Httprequest req=new Httpreques();
// yopu must need mention ".xml" at the end of end point
req.setendpoint(URL.getCurrentRequestUrl().toExternalForm()+'/services/apexrest/xmlSerialization.xml');
req.setmethod('POST');
// after OAuth you need to maintain space like below code of line
req.setheader('Authorization'.'OAuth '+userinfo.getsessionid());
Http h=new Http();
httpresponse r= h.send(req);
system.debug( r.getbody()+'XMl information print');
}
}
we don't have direct simple method to generate bulk XML data that's why we are using Httprequest future.
comments and suggestions please.
you can personally email at sfdc.integration01@gmail.com
From below Post you will learn how to do XML serialization in simple process.
JSON serialization is very simple in salesforce. we can do it by using JSON.serialization() method.
below simple example for getting contract JSON data.
// Start
// fetching records from database
List<contact> clist=[select id,name,email from contact limit 10];
// serialization of records in JSON
string convertedContactJsonString = JSON.serialization(clist);
system.debug(convertedContactJsonString +'JSON Data');
// end
But If you need bulk of contact records in XML format... how to do..?
we dont have XML serialization method like above JSON example to generate result with single line of code. And XMLStreamWriter is not a good idea for Bulk records process.
what you need before understanding.
1. @restresource
2. HrttpRest request.
Requirement :
Third party system needs contact information in XML via SOAP service or you need generate contacts in XML format from apex code.
here we go.....!
// this class is to return list of conatacts
@restresource(/urlmapping='/xmlSerialization/*')
global class XmlListExposer {
@httpPost
global static List<contact> contactListEextractor(){
return [select id,name , email from contact limit 100];
}
}
// class 2 for expose SOAP service
global class SoapClassToCallInternalHttpClass{
webservice static staing callRestMethodForXml(){
Httprequest req=new Httpreques();
// yopu must need mention ".xml" at the end of end point
req.setendpoint(URL.getCurrentRequestUrl().toExternalForm()+'/services/apexrest/xmlSerialization.xml');
req.setmethod('POST');
// after OAuth you need to maintain space like below code of line
req.setheader('Authorization'.'OAuth '+userinfo.getsessionid());
Http h=new Http();
httpresponse r= h.send(req);
system.debug( r.getbody()+'XMl information print');
}
}
we don't have direct simple method to generate bulk XML data that's why we are using Httprequest future.
comments and suggestions please.
you can personally email at sfdc.integration01@gmail.com
No comments:
Post a Comment