Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to create sales orders in SAP C4C

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to use Java to create sales orders in SAP C4C". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The details of the sales orders to be created are specified by hard coding:

For example, the sales order is described as Jerry Test 2019-1-23 16:05PM

After execution, you see Status Code 201, which indicates that the creation is successful:

You can see the successfully created sales order on UI:

Implementation code:

Package odata;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.URI;import org.apache.http.HttpEntity;import org.apache.http.HttpHost;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity Import org.apache.http.impl.client.HttpClientBuilder;public class SimpleOrderCreator {private final String SERVICEURL = "https://my5000jerry.c4c.saphybriscloud.cn/sap/c4c/odata/cust/v1/zjerrysalesorder/CustomerQuoteCollection"; HttpClient masked httpClient; private HttpClient getHttpClient () {if (this.m_httpClient = = null) {this.m_httpClient = HttpClientBuilder.create (). Build ();} return this.m_httpClient } private String getCSRFToken () {final HttpGet get = new HttpGet (SERVICEURL); get.setHeader ("Authorization", "Basic 0FOR0pFUlJZMTpTYXB0ZXN0MQQ ="); get.setHeader ("x-csrf-token", "fetch"); HttpHost proxy = new HttpHost ("proxy.sha.sap.corp", 8080, "http"); RequestConfig config = RequestConfig.custom (). SetProxy (proxy). Build (); get.setConfig (config) HttpResponse response; String token = null; try {response = getHttpClient (). Execute (get); / * Header [] header = response.getAllHeaders (); for (int I = 0; I)

< header.length; i++){ System.out.println("Header: " + header[i].getValue()); }*/ token = response.getFirstHeader("x-csrf-token").getValue(); System.out.println("token: " + token); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException | UnsupportedOperationException e) { e.printStackTrace(); } return token; } private void createSO(String token,String body) { final HttpPost post = new HttpPost( URI.create(SERVICEURL)); post.setHeader("Authorization", "Basic 0FOR0pFUlJZMTpTYXB0ZXN0MQ=="); post.setHeader("Content-Type", "application/json"); post.setHeader("X-CSRF-Token", token); HttpEntity entity = null; try { entity = new StringEntity(body); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } post.setEntity(entity); HttpResponse response = null; try { response = getHttpClient().execute(post); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Response statusCode for Batch =>

"+ response.getStatusLine () .getStatusCode ();} public static void main (String [] args) {SimpleOrderCreator tool = new SimpleOrderCreator (); String token = tool.getCSRFToken () String body = "{" + "\" Name\ ":\" Jerry Test 2019-1-23 16:05PM\ "," + "\" TypeCode\ ":\" 2059\ "," + "\" BuyerID\ ":\" ee\ "," + "\" PartyID\ ":\" 60102\ "," + "\" CustomerQuoteText\ ": [{" + "\" Text\ ":\" test comment by Jerry Wang\ " "+"\ "TypeCode\":\ "10024\"+"}], "+"\ "CustomerQuoteItem\": [{"+"\ "ProductID\":\ "1042416\"+"}], "+"\ "CustomerQuoteItemProposal\": [{"+"\ "ProductUUID\":\ "00163E72-09C6-1EE8-BBDC-AC5F0CB0D795\", "+"\ "Quantity\":\ "1\" "+"\ "unitCode\":\ "EA\"+"}] "+"} " Tool.createSO (token, body);}} "how to use Java to create sales orders in SAP C4C" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report