You need to sign in to do that
Don't have an account?
Arpitha M
how to get json data in visualforce page. In Apex class I have parsed the json data , In visualforce page I need to display it.
Apex Class:
public class createCorporate {
public Account acc {get;set;}
public List<Contact> conList {get;set;}
public AccountWrapper.cls_billing cbilling {get;set;}
public AccountWrapper.cls_personal personal {get;set;}
public Id Id{get;set;}
public String message{get;set;}
public string updatedmessage{get;set;}
CorporateResponse core = new CorporateResponse();
public createCorporate(ApexPages.StandardController controller){
acc = (Account) controller.getRecord();
Id = acc.Id;
}
public void createCorporatedata(){
HMACGenearatorHandler hg = new HMACGenearatorHandler();
fromJSON obj = new fromJSON();
HMACGenearatorHandler.Wrapper wr;
wr = hg.createJson();
if(wr.response != null){
obj = obj.parse(wr.response);
string accesssecret = obj.hotelogix.response.accesssecret;
string accesskey = obj.hotelogix.response.accesskey;
acc= new Account();
Contact con = new Contact();
AccountWrapper aw = new AccountWrapper();
AccountWrapper.cls_personal personal = new AccountWrapper.cls_personal();
AccountWrapper.cls_billing cbilling = new AccountWrapper.cls_billing();
acc= [Select id,Name,taxCode__c,Account_Address__c,Account_State__c,Account_City__c,Account_Pincode__c,
BillingStreet,BillingCity,BillingCountry,BillingState,BillingPostalCode,(Select id,Title,name,Type__c,Contact_Designation__c,
Birthdate,Email,Gender__c,Fax,FirstName,LastName,MobilePhone,Phone
from contacts where Type__c != 'Others' ORDER BY Type__c ASC LIMIT 2) from Account where id =:Id];
conList= acc.contacts;
system.debug('con+++'+conList);
for(Contact c : conList){
if(c.Type__c =='Personal'){
personal.salutation= c.Title;
personal.designation =c.Contact_Designation__c;
personal.dob =string.valueOf(c.Birthdate);
personal.email =c.Email;
personal.faxNo = c.Fax;
personal.fName = c.FirstName;
personal.gender =c.Gender__c;
personal.lName = c.LastName;
personal.mobileNo =c.MobilePhone;
personal.phoneExtension='';
personal.phoneNo =c.Phone;
personal.website ='';
}
if(c.Type__c =='Billing'){
cbilling.salutation= c.Title;
cbilling.designation =c.Contact_Designation__c;
cbilling.dob =string.valueOf(c.Birthdate);
cbilling.email =c.Email;
cbilling.faxNo = c.Fax;
cbilling.fName = c.FirstName;
cbilling.gender =c.Gender__c;
cbilling.lName = c.LastName;
cbilling.mobileNo =c.MobilePhone;
cbilling.phoneExtension='';
cbilling.phoneNo =c.Phone;
cbilling.website ='';
}
}
AccountWrapper.cls_office office = new AccountWrapper.cls_office();
office.address = acc.Account_Address__c;
office.city = acc.Account_City__c;
office.country ='India';
office.state=acc.Account_State__c;
office.zip = String.valueOf(acc.Account_Pincode__c);
AccountWrapper.cls_billingAddress billingad = new AccountWrapper.cls_billingAddress();
billingad.companyName = acc.Name;
billingad.address = acc.BillingStreet;
billingad.city = acc.BillingCity;
billingad.country = acc.BillingCountry;
billingad.state=acc.BillingState;
billingad.zip = acc.BillingPostalCode;
AccountWrapper.cls_addresses addresses = new AccountWrapper.cls_addresses();
addresses.office = office;
addresses.billingaddress = billingad;
AccountWrapper.cls_contacts contacts = new AccountWrapper.cls_contacts();
contacts.personal = personal;
contacts.billing = cbilling;
AccountWrapper.cls_creditCardDetail carddetails = new AccountWrapper.cls_creditCardDetail();
carddetails.nameOnCard ='Puja';
carddetails.cardType ='Visa';
carddetails.cardNo ='4111111111111111';
carddetails.cvc ='256';
carddetails.city='Delhi';
carddetails.expiryMonth ='05';
carddetails.expiryYear ='2022';
carddetails.zip ='256';
carddetails.address ='noida';
carddetails.state ='delhi';
carddetails.country ='India';
List<AccountWrapper.cls_hotels> hotels = new List<AccountWrapper.cls_hotels>();
AccountWrapper.cls_hotels hotel = new AccountWrapper.cls_hotels();
hotel.id ='51237';
hotels.add(hotel);
AccountWrapper.cls_data data = new AccountWrapper.cls_data();
data.groupCode='';
data.businessName=acc.Name;
data.taxCode =acc.taxCode__c;
data.addresses= addresses;
data.contacts = contacts;
data.creditCardDetail = carddetails;
data.creditLimit='100';
data.paymentTerms='5';
data.discount='10';
data.discountable=true;
data.hotels = hotels;
AccountWrapper.cls_request req = new AccountWrapper.cls_request();
req.key =accesskey;
req.method='createcorporate';
req.data = data;
AccountWrapper.cls_hotelogix hlogix = new AccountWrapper.cls_hotelogix();
hlogix.version='1.0';
hlogix.datetime_x =wr.myDate;
hlogix.request = req;
aw.hotelogix = hlogix;
String corporateJSON = JSON.serialize(aw);
corporateJSON = corporateJSON.replace('datetime_x', 'datetime');
system.debug('corporateJSON: '+ corporateJSON);
String mac2=hg.generateHmacSHA1Signature(corporateJSON,accesssecret);
sendcreateCorpRequest(corporateJSON,mac2);
}
}
public static void sendcreateCorpRequest(String corporatejson,String mac2){
System.debug('mac2+++ '+ mac2);
HttpRequest req = new HttpRequest();
req.setEndpoint(System.Label.createCorporate);
req.setMethod('POST');
req.setHeader('X-HAPI-Signature',mac2);
req.setHeader('Content-Type', System.label.Content_Type);
req.setBody(corporatejson);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res);
System.debug('createcorp++++ '+res.getBody());
System.debug('GET STATUS +++ '+res.getStatus());
String response = res.getBody();
CorporateResponse core = new CorporateResponse();
core = core.parse(response);
System.debug('core++'+core);
String message = core.hotelogix.response.status.message;
System.debug(message);
String updatedmessage = core.hotelogix.response.hotels[0].updateStatus.message;
System.debug(updatedmessage);
}
}
Visualforce Page:
<apex:page standardController="Account" extensions="createCorporate" action="{!createCorporatedata}">
{!message}
{!updatedmessage}
</apex:page>
public class createCorporate {
public Account acc {get;set;}
public List<Contact> conList {get;set;}
public AccountWrapper.cls_billing cbilling {get;set;}
public AccountWrapper.cls_personal personal {get;set;}
public Id Id{get;set;}
public String message{get;set;}
public string updatedmessage{get;set;}
CorporateResponse core = new CorporateResponse();
public createCorporate(ApexPages.StandardController controller){
acc = (Account) controller.getRecord();
Id = acc.Id;
}
public void createCorporatedata(){
HMACGenearatorHandler hg = new HMACGenearatorHandler();
fromJSON obj = new fromJSON();
HMACGenearatorHandler.Wrapper wr;
wr = hg.createJson();
if(wr.response != null){
obj = obj.parse(wr.response);
string accesssecret = obj.hotelogix.response.accesssecret;
string accesskey = obj.hotelogix.response.accesskey;
acc= new Account();
Contact con = new Contact();
AccountWrapper aw = new AccountWrapper();
AccountWrapper.cls_personal personal = new AccountWrapper.cls_personal();
AccountWrapper.cls_billing cbilling = new AccountWrapper.cls_billing();
acc= [Select id,Name,taxCode__c,Account_Address__c,Account_State__c,Account_City__c,Account_Pincode__c,
BillingStreet,BillingCity,BillingCountry,BillingState,BillingPostalCode,(Select id,Title,name,Type__c,Contact_Designation__c,
Birthdate,Email,Gender__c,Fax,FirstName,LastName,MobilePhone,Phone
from contacts where Type__c != 'Others' ORDER BY Type__c ASC LIMIT 2) from Account where id =:Id];
conList= acc.contacts;
system.debug('con+++'+conList);
for(Contact c : conList){
if(c.Type__c =='Personal'){
personal.salutation= c.Title;
personal.designation =c.Contact_Designation__c;
personal.dob =string.valueOf(c.Birthdate);
personal.email =c.Email;
personal.faxNo = c.Fax;
personal.fName = c.FirstName;
personal.gender =c.Gender__c;
personal.lName = c.LastName;
personal.mobileNo =c.MobilePhone;
personal.phoneExtension='';
personal.phoneNo =c.Phone;
personal.website ='';
}
if(c.Type__c =='Billing'){
cbilling.salutation= c.Title;
cbilling.designation =c.Contact_Designation__c;
cbilling.dob =string.valueOf(c.Birthdate);
cbilling.email =c.Email;
cbilling.faxNo = c.Fax;
cbilling.fName = c.FirstName;
cbilling.gender =c.Gender__c;
cbilling.lName = c.LastName;
cbilling.mobileNo =c.MobilePhone;
cbilling.phoneExtension='';
cbilling.phoneNo =c.Phone;
cbilling.website ='';
}
}
AccountWrapper.cls_office office = new AccountWrapper.cls_office();
office.address = acc.Account_Address__c;
office.city = acc.Account_City__c;
office.country ='India';
office.state=acc.Account_State__c;
office.zip = String.valueOf(acc.Account_Pincode__c);
AccountWrapper.cls_billingAddress billingad = new AccountWrapper.cls_billingAddress();
billingad.companyName = acc.Name;
billingad.address = acc.BillingStreet;
billingad.city = acc.BillingCity;
billingad.country = acc.BillingCountry;
billingad.state=acc.BillingState;
billingad.zip = acc.BillingPostalCode;
AccountWrapper.cls_addresses addresses = new AccountWrapper.cls_addresses();
addresses.office = office;
addresses.billingaddress = billingad;
AccountWrapper.cls_contacts contacts = new AccountWrapper.cls_contacts();
contacts.personal = personal;
contacts.billing = cbilling;
AccountWrapper.cls_creditCardDetail carddetails = new AccountWrapper.cls_creditCardDetail();
carddetails.nameOnCard ='Puja';
carddetails.cardType ='Visa';
carddetails.cardNo ='4111111111111111';
carddetails.cvc ='256';
carddetails.city='Delhi';
carddetails.expiryMonth ='05';
carddetails.expiryYear ='2022';
carddetails.zip ='256';
carddetails.address ='noida';
carddetails.state ='delhi';
carddetails.country ='India';
List<AccountWrapper.cls_hotels> hotels = new List<AccountWrapper.cls_hotels>();
AccountWrapper.cls_hotels hotel = new AccountWrapper.cls_hotels();
hotel.id ='51237';
hotels.add(hotel);
AccountWrapper.cls_data data = new AccountWrapper.cls_data();
data.groupCode='';
data.businessName=acc.Name;
data.taxCode =acc.taxCode__c;
data.addresses= addresses;
data.contacts = contacts;
data.creditCardDetail = carddetails;
data.creditLimit='100';
data.paymentTerms='5';
data.discount='10';
data.discountable=true;
data.hotels = hotels;
AccountWrapper.cls_request req = new AccountWrapper.cls_request();
req.key =accesskey;
req.method='createcorporate';
req.data = data;
AccountWrapper.cls_hotelogix hlogix = new AccountWrapper.cls_hotelogix();
hlogix.version='1.0';
hlogix.datetime_x =wr.myDate;
hlogix.request = req;
aw.hotelogix = hlogix;
String corporateJSON = JSON.serialize(aw);
corporateJSON = corporateJSON.replace('datetime_x', 'datetime');
system.debug('corporateJSON: '+ corporateJSON);
String mac2=hg.generateHmacSHA1Signature(corporateJSON,accesssecret);
sendcreateCorpRequest(corporateJSON,mac2);
}
}
public static void sendcreateCorpRequest(String corporatejson,String mac2){
System.debug('mac2+++ '+ mac2);
HttpRequest req = new HttpRequest();
req.setEndpoint(System.Label.createCorporate);
req.setMethod('POST');
req.setHeader('X-HAPI-Signature',mac2);
req.setHeader('Content-Type', System.label.Content_Type);
req.setBody(corporatejson);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res);
System.debug('createcorp++++ '+res.getBody());
System.debug('GET STATUS +++ '+res.getStatus());
String response = res.getBody();
CorporateResponse core = new CorporateResponse();
core = core.parse(response);
System.debug('core++'+core);
String message = core.hotelogix.response.status.message;
System.debug(message);
String updatedmessage = core.hotelogix.response.hotels[0].updateStatus.message;
System.debug(updatedmessage);
}
}
Visualforce Page:
<apex:page standardController="Account" extensions="createCorporate" action="{!createCorporatedata}">
{!message}
{!updatedmessage}
</apex:page>
Best Answer chosen by Arpitha M
Arpitha M
The static should be removed from sendcreateCorpRequest() method.