public class SetMetaData {
public String selectedObject {set; get;}
public String Selectfields {set; get;}
Map mapObjects = Schema.getGlobalDescribe();
string sID {get;set;}
public SetMetaData (){
sID = ApexPages.currentPage().getParameters().get('Id');
System.debug(sID);
}
public List<SelectOption> getObjects(){
List<Schema.SObjectType> gd = mapObjects.Values();
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('','--None--'));
for(Schema.SObjectType f: gd){
options.add(new SelectOption(f.getDescribe().getName(),f.getDescribe().getName()));
}
return options;
}
public List<SelectOption> getFields(){
//selectedObject = 'Account';
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('','--None--'));
if(mapObjects.containskey(selectedObject)){
system.debug('===================='+selectedObject);
Schema.SObjectType s12 = mapObjects.get(selectedObject);
List<Schema.SObjectType> objects = new List<Schema.SObjectType>{s12};
for(Schema.SObjectType objType: objects){
for(Schema.SObjectField fld: objType.getDescribe().fields.getMap().values()){
options.add(new SelectOption(fld.getDescribe().getName(),fld.getDescribe().getName()));
System.debug('API Field Name = '+fld.getDescribe().getName());
}
}
}
return options;
}
public PageReference SetData (){
//Get the Report data in json format
string st = selectedObject+'.'+Selectfields;
Http hr = new Http();
HttpRequest req1 = new HttpRequest();
req1.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v34.0/analytics/reports/'+sID);
req1.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req1.setHeader('Content-Type', 'application/json');
req1.setMethod('GET');
HttpResponse res1 = hr.send(req1);
String body = res1.getBody();
system.debug('++++++++++++++++++++++'+res1);
system.debug('++++++++++++++++++++++'+body);
//map Json for Common data request
Map<String, Object> root = (Map<String, Object>)JSON.deserializeUntyped(body);
Map<String, Object> AllResultoObj = (Map<String, Object>)root.get('reportMetadata');
system.debug('++++++++++++++++++++++'+AllResultoObj);
List<Object> rowList = (List<Object>)AllResultoObj.get('detailColumns');
System.debug('++++++++++++++++++++++'+rowList);
//Create the body for send the data in report
list<object> lststr = rowList;
System.debug('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'+lststr);
List<Object> str = new List<Object>();
string myListStirng;
for(object objstr : lststr){
str.add('"'+objstr+'"');
myListStirng = string.join(str,',');
system.debug('********************'+myListStirng);
}
for(object ob : rowList){
System.debug('++++++++++++++++++++++'+ob);
if(ob == st){
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.WARNING, 'This Column already exist in report'));
}
else {
string BodyParse = '{"reportMetadata" : {"detailColumns" : ['+myListStirng+',"'+st+'"]}}';
System.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+BodyParse);
//Post the data and save the reports
system.debug('============'+st);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v34.0/analytics/reports/'+sID+'?_HttpMethod=PATCH');
req.setBody(BodyParse);
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setMethod('POST');
HttpResponse res = h.send(req);
string bodyres = res.getBody();
if(bodyres.contains('errorCode'))
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, bodyres));
else
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM, 'This Column has been inserted in report'));
system.debug('++++++++++++++++++++++'+res);
system.debug('++++++++++++++++++++++'+bodyres);
}
}
return null;
}
Public PageReference delData(){
//Get the Report data in json format
string st = selectedObject+'.'+Selectfields;
Http hr = new Http();
HttpRequest req1 = new HttpRequest();
req1.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v34.0/analytics/reports/'+sID);
req1.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req1.setHeader('Content-Type', 'application/json');
req1.setMethod('GET');
HttpResponse res1 = hr.send(req1);
String body = res1.getBody();
system.debug('++++++++++++++++++++++'+res1);
system.debug('++++++++++++++++++++++'+body);
//map Json for Common data request
Map<String, Object> root = (Map<String, Object>)JSON.deserializeUntyped(body);
Map<String, Object> AllResultoObj = (Map<String, Object>)root.get('reportMetadata');
system.debug('++++++++++++++++++++++'+AllResultoObj);
List<Object> rowList = (List<Object>)AllResultoObj.get('detailColumns');
System.debug('++++++++++++++++++++++'+rowList);
//Create the body for send the data in report
list<object> lststr = rowList;
List<Object> delStr = new List<Object>();
string delString;
for(object ob : lststr){
System.debug('++++++++++++++++++++++'+ob);
if(ob != st){
delStr.add('"'+ob+'"');
delString = string.join(delStr,',');
}
else {
string BodyParse = '{"reportMetadata" : {"detailColumns" : ['+delString+']}}';
System.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+BodyParse);
//Post the data and save the reports
system.debug('============'+st);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v34.0/analytics/reports/'+sID+'?_HttpMethod=PATCH');
req.setBody(BodyParse);
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setMethod('POST');
HttpResponse res = h.send(req);
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM, 'This Column has been deleted in report'));
system.debug('++++++++++++++++++++++'+res);
}
}
return null;
}
}