0

I want to insert a detail column in report in salesforce. I have get the data from report but unable to set the column in salesforce. Anyone have solution for that scenario?

The code and error:

  public PageReference SetData (){
    List <Report> reportList = [SELECT Id,DeveloperName FROM Report WHERE ID = '00O90000002umtc'];
    for(Report rpt:reportList){
        reportId = (String)rpt.Id;
        Reports.ReportDescribeResult describe = Reports.ReportManager.describeReport(reportId);
         // Get the report metadata
        Reports.ReportMetadata rm = describe.getReportMetadata();
        List<String> lstString = rm.getDetailColumns();
        system.debug(lstString);
        List<String> lstString1 = new List<String>();
        //For(string st : lstString){
            string st = 'Hello_dev__Price__c';
            system.debug('============================================='+st);
            If(st != null){
                lstString1.add(st);
                system.debug('=========================================='+lstString1);
               rm.setDetailColumns(lstString1);
               Reports.ReportResults results = Reports.ReportManager.runReport(reportId, rm, true);

            //}

          }

        }
    return null;
}

reports.InvalidFilterException: [The column hello_dev__merchandise__c.hello_dev__price__c is not a valid column.] Error is in expression '{!SetData}' in component in page hello_dev:metadatalist: Class.reports.ReportManager.runReport: line 24, column 1 Class.Hello_dev.MetaDatalist.SetData: line 122, column 1 An unexpected error has occurred. Your solution provider has been notified. (reports)

6
  • Given that the method is available, setDetailColumns, what exactly is your problem?
    – Keith C
    May 17 '16 at 8:11
  • need a set column in exist report I am using following code and it is throwing exception. May 17 '16 at 8:28
  • string st = 'hello_dev__merchandise__c.hello_dev__price__c'; If(st != null){ lstString1.add(st); rm.setDetailColumns(lstString1); Reports.ReportResults results = Reports.ReportManager.runReport(reportId, rm, true); } May 17 '16 at 8:30
  • reports.InvalidFilterException: [The column hello_dev__merchandise__c.hello_dev__price__c is not a valid column.] Error is in expression '{!SetData}' in component <apex:commandButton> in page hello_dev:metadatalist: Class.reports.ReportManager.runReport: line 24, column 1 Class.Hello_dev.MetaDatalist.SetData: line 122, column 1 An unexpected error has occurred. Your solution provider has been notified. (reports) May 17 '16 at 8:30
  • I've edited that information into the question. (I'm assuming you don't presently have editing rights.)
    – Keith C
    May 17 '16 at 8:43
0

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;
} 

}

2
  • Pass the report id in Url as a para meter May 23 '16 at 11:40
  • Any one need vf page we can post here May 23 '16 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.