Bum
Bum

Reputation: 1

How Can I Refresh Document Using SAP Rest APIS?

I am using SAP BO rest services and getting document parameters with below code;

        public string GetDocumentParameters(string documentId)
        {
            return _client.GetStringAsync($"{ServiceParameters.BaseUrl}/raylight/v1/documents/{documentId}/parameters").GetAwaiter().GetResult();    
        }

Then I am parsing result and display parameters in the asp.net webform aspx page. For example, above code return string name, datetime birthday etc. then I am generating textbox or datetimepicker etc. When the user fills in these parameters on the screen, I assign the data entered by the user to a class called RootObject then I want to create reports according to the parameters it fills in. So this purpose I am using below code;

        public DocumentResult GetDocument(string documentId, RootObject rootObject)
        {
            UpdateDocumentParameters(documentId, rootObject);         
            var documentResult = FetchDocumentContent(documentId);

            return documentResult;
        }
        
        private void UpdateDocumentParameters(string documentId, RootObject rootObject)
        {
            var json = JsonConvert.SerializeObject(rootObject, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Formatting = Newtonsoft.Json.Formatting.Indented
            });

            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var requestUri = $"{ServiceParameters.BaseUrl}/raylight/v1/documents/{documentId}/parameters";

            var response = _client.PutAsync(requestUri, content).GetAwaiter().GetResult();
            response.EnsureSuccessStatusCode();
        }       
        
        private DocumentResult FetchDocumentContent(string documentId)
        {
            var requestUri = $"{ServiceParameters.BaseUrl}/raylight/v1/documents/{documentId}/reports";
            var documentReports = GetDocumentReports(requestUri);
            var reports = ParseReports(documentReports);
            var reportContents = new Dictionary<int, string>();
            foreach (var reportId in reports.Select(r => r.Id))
            {
                var reportUrl = $"{requestUri}/{reportId}";
                var reportContentDoc = GetReport(reportUrl);
                reportContents[reportId] = reportContentDoc;
            }

            return new DocumentResult
            {
                ReportContents = reportContents,
                Reports = reports
            };
        }
        
        private XDocument GetDocumentReports(string url)
        {
            var response = _client.GetStringAsync(url).GetAwaiter().GetResult();
            return XDocument.Parse(response);
        }
        

        private List<ReportDetail> ParseReports(XDocument document)
        {
            return document.Descendants("report")
                           .Select(ParseReportDetail)
                           .ToList();
        }

        private ReportDetail ParseReportDetail(XElement reportElement)
        {
            return new ReportDetail
            {
                Id = int.Parse(reportElement.Element("id").Value),
                Name = reportElement.Element("name").Value,
                Content = reportElement.Element("reference").Value
            };
        }

        private string GetReport(string url)
        {
            return _client.GetStringAsync(url).GetAwaiter().GetResult();
        }   

When I am look at the DocumentResult reports not affacted this parameters. There are 3 reports for the sample document. What I expect is for the relevant reports to be filled according to the parameters I send, but only one of the 3 reports is filled and the other two are empty.

How Can I fix this problem?

Upvotes: 0

Views: 20

Answers (0)

Related Questions