{"action":"created","issue":{"url":"https://api.github.com/repos/financialforcedev/apex-mdapi/issues/147","repository_url":"https://api.github.com/repos/financialforcedev/apex-mdapi","labels_url":"https://api.github.com/repos/financialforcedev/apex-mdapi/issues/147/labels{/name}","comments_url":"https://api.github.com/repos/financialforcedev/apex-mdapi/issues/147/comments","events_url":"https://api.github.com/repos/financialforcedev/apex-mdapi/issues/147/events","html_url":"https://github.com/financialforcedev/apex-mdapi/issues/147","id":183035774,"number":147,"title":"How to create reports ?","user":{"login":"KGFTW","id":6064738,"avatar_url":"https://avatars.githubusercontent.com/u/6064738?v=3","gravatar_id":"","url":"https://api.github.com/users/KGFTW","html_url":"https://github.com/KGFTW","followers_url":"https://api.github.com/users/KGFTW/followers","following_url":"https://api.github.com/users/KGFTW/following{/other_user}","gists_url":"https://api.github.com/users/KGFTW/gists{/gist_id}","starred_url":"https://api.github.com/users/KGFTW/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KGFTW/subscriptions","organizations_url":"https://api.github.com/users/KGFTW/orgs","repos_url":"https://api.github.com/users/KGFTW/repos","events_url":"https://api.github.com/users/KGFTW/events{/privacy}","received_events_url":"https://api.github.com/users/KGFTW/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-10-14T12:18:10Z","updated_at":"2016-11-22T10:54:04Z","closed_at":null,"body":"Hi,\nI am trying to create reports programmatically with the apex metadata api, but am facing the following issue:\n![image](https://cloud.githubusercontent.com/assets/6064738/19387073/395abfba-9218-11e6-994b-549e33afd2f1.png)\n\nHere's the code used (in the execute anonymous window) :\n\n```\npublic class MetadataServiceException extends Exception { }\npublic static void handleSaveResults(MetadataService.SaveResult saveResult) {\n    if(saveResult==null || saveResult.success)\n        return;\n    if(saveResult.errors!=null) {\n        List<String> messages = new List<String>();\n        messages.add(\n            (saveResult.errors.size()==1 ? 'Error ' : 'Errors ') +\n            'occured processing component ' + saveResult.fullName + '.');\n        for(MetadataService.Error error : saveResult.errors)\n            messages.add(\n                error.message + ' (' + error.statusCode + ').' +\n                ( error.fields!=null && error.fields.size()>0 ?\n                 ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );\n        if(messages.size()>0)\n            throw new MetadataServiceException (String.join(messages, ' '));\n    }\n    if(!saveResult.success)\n        throw new MetadataServiceException ('Request failed with no specified error.');\n}\n\n// Here's the part that I am concerned with\nMetadataService.MetadataPort service = new MetadataService.MetadataPort();\nservice.SessionHeader = new MetadataService.SessionHeader_element();\nservice.SessionHeader.sessionId = UserInfo.getSessionId();\n\n// I get the report to clone\nMetadataService.Report reportToClone = (MetadataService.Report) service.readMetadata('Report', new String[] { 'report_folder_api_name/report_api_name' }).getRecords()[0];\n\n// Instanciate a new one to attribute the same metadata from the report to clone\nMetadataService.Report apexReport = new MetadataService.Report();\napexReport.name = 'Apex_Clone_Report';\napexReport.fullName = 'Apex Clone Report';\napexReport.reportType = reportToClone.reportType;\napexReport.format = reportToClone.format;\napexReport.filter = reportToClone.filter;\napexReport.folderName = 'report_folder_api_name';\napexReport.columns = reportToClone.columns;\n\n// Create the report\nList<MetadataService.SaveResult> results = service.createMetadata(new MetadataService.Metadata[] { apexReport });\n\n// Handle the results\nhandleSaveResults(results[0]);\n```\n\nCould you provide me with what I'm missing there ?\n\nThanks.\n\nPS: This is **almost** a duplicate of [this issue](https://github.com/financialforcedev/apex-mdapi/issues/123).\n"},"comment":{"url":"https://api.github.com/repos/financialforcedev/apex-mdapi/issues/comments/262209627","html_url":"https://github.com/financialforcedev/apex-mdapi/issues/147#issuecomment-262209627","issue_url":"https://api.github.com/repos/financialforcedev/apex-mdapi/issues/147","id":262209627,"user":{"login":"KGFTW","id":6064738,"avatar_url":"https://avatars.githubusercontent.com/u/6064738?v=3","gravatar_id":"","url":"https://api.github.com/users/KGFTW","html_url":"https://github.com/KGFTW","followers_url":"https://api.github.com/users/KGFTW/followers","following_url":"https://api.github.com/users/KGFTW/following{/other_user}","gists_url":"https://api.github.com/users/KGFTW/gists{/gist_id}","starred_url":"https://api.github.com/users/KGFTW/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KGFTW/subscriptions","organizations_url":"https://api.github.com/users/KGFTW/orgs","repos_url":"https://api.github.com/users/KGFTW/repos","events_url":"https://api.github.com/users/KGFTW/events{/privacy}","received_events_url":"https://api.github.com/users/KGFTW/received_events","type":"User","site_admin":false},"created_at":"2016-11-22T10:54:04Z","updated_at":"2016-11-22T10:54:04Z","body":"Without any answer about this issue, I tried over again and managed to entirely clone a report from one folder to another.\r\n\r\nThe initial issue was that the \"fullName\" field of the new report wasn't well constructed as it actually needs to be populated with both the report folder api name and the report api name.\r\n\r\nAs this might be of use for anyone, here's the code sample I'm using:\r\n```\r\npublic class MetadataServiceException extends Exception { }\r\npublic static void handleSaveResults(MetadataService.SaveResult saveResult) {\r\n    if(saveResult==null || saveResult.success)\r\n        return;\r\n    if(saveResult.errors!=null) {\r\n        List<String> messages = new List<String>();\r\n        messages.add(\r\n            (saveResult.errors.size()==1 ? 'Error ' : 'Errors ') +\r\n            'occured processing component ' + saveResult.fullName + '.');\r\n        for(MetadataService.Error error : saveResult.errors)\r\n            messages.add(\r\n                error.message + ' (' + error.statusCode + ').' +\r\n                ( error.fields!=null && error.fields.size()>0 ?\r\n                 ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );\r\n        if(messages.size()>0)\r\n            throw new MetadataServiceException (String.join(messages, ' '));\r\n    }\r\n    if(!saveResult.success)\r\n        throw new MetadataServiceException ('Request failed with no specified error.');\r\n}\r\n\r\n/**\r\n * Method cloneReport(String sFolderApiName, String sReportApiName, String tFolderApiName, String tReportApiName)\r\n * @param sFolderApiName: api name of the (source) folder of the report to clone\r\n * @param sReportApiName: api name of the (source) report to clone\r\n * @param tFolderApiName: api name of the (target) folder to create the cloned report in\r\n * @param tReportApiName: api name of the (target) cloned report \r\n */\r\nPublic static void cloneReport(String sFolderApiName, String sReportApiName, String tFolderApiName, String tReportApiName) {\r\n\tMetadataService.MetadataPort service = new MetadataService.MetadataPort();\r\n\tservice.SessionHeader = new MetadataService.SessionHeader_element();\r\n\tservice.SessionHeader.sessionId = UserInfo.getSessionId();\r\n\r\n\t// Get the report to clone\r\n\tMetadataService.Report reportToClone = (MetadataService.Report) service.readMetadata('Report', new String[] { sFolderApiName+'/'+sReportApiName }).getRecords()[0];\r\n\r\n\t// Instanciate a new one to attribute the same metadata from the report to clone\r\n\tMetadataService.Report apexReport = new MetadataService.Report();\r\n\t// Set the cloned report properties from parameters and the source report\r\n\tapexReport.name = reportToClone.name + ' Clone';\r\n\tapexReport.fullName = tFolderApiName + '/' + tReportApiName;\r\n\tapexReport.reportType = reportToClone.reportType;\r\n\tapexReport.description = reportToClone.description;\r\n\tapexReport.format = reportToClone.format;\r\n\tapexReport.filter = reportToClone.filter;\r\n        apexReport.showDetails = reportToClone.showDetails;\r\n        apexReport.sortColumn = reportToClone.sortColumn;\r\n        apexReport.sortOrder = reportToClone.sortOrder;\r\n        apexReport.groupingsAcross = reportToClone.groupingsAcross;\r\n        apexReport.groupingsDown = reportToClone.groupingsDown;\r\n        apexReport.chart = reportToClone.chart;\r\n\tapexReport.timeFrameFilter = reportToClone.timeFrameFilter;\r\n\tapexReport.columns = reportToClone.columns;\r\n\r\n\t// Create the report clone\r\n\tList<MetadataService.SaveResult> results = service.createMetadata(new MetadataService.Metadata[] { apexReport });\r\n\r\n\t// Handle results\r\n\thandleSaveResults(results[0]);\r\n}\r\n\r\ncloneReport('Source_Folder_Api_Name','Source_Report_Api_Name','Target_Folder_Api_Name','Target_Report_Api_Name');\r\n```\r\n\r\nThis actually needs to be customized depending on one's need over reports metadata, but it's a great sample to begin with mdapi report cloning.\r\n"}}
{"actor":{"display_login":"KGFTW"}}