web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

WEB Api Update Issue Using Java

(0) ShareShare
ReportReport
Posted on by

Hi All,

I am trying to update CRM Quote record in Java using WEB API, but its giving me " aused by: java.io.IOException: Server returned HTTP response code: 500 for URL" Error.

Can you help me on urgent basis.

    private static String UpdateQuote(String token, String accountId) throws MalformedURLException, IOException, URISyntaxException {
        JSONObject quote = new JSONObject();
        quote.put("new_oraclequotenumber", "456");
        quote.put("name","test223");
        HttpURLConnection connection = null;
        //The URL will change in 2016 to include the API version - /api/data/v8.0/accounts
        URL url = new URL(RESOURCE + "/api/data/v9.1/quotes(D0BC8A5D-70FD-E811-A961-000D3A378CA2)");
    //    URL url = new URL(RESOURCE + "/api/data/v9.1/quotes(quotenumber='QUO-00619-B2F9D7')");
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
        connection.setRequestMethod("POST");
        connection.setRequestProperty("OData-MaxVersion", "4.0");
        connection.setRequestProperty("OData-Version", "4.0");
        connection.setRequestProperty("Accept", "application/json");
        //connection.setRequestProperty("If-Match", "*");
        connection.addRequestProperty("Authorization", "Bearer " + token);
        connection.setUseCaches(false);
        connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
       // connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("Prefer", "return=representation");        
        connection.setDoOutput(true);
        connection.connect();

        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write(quote.toJSONString());
        out.flush();
        out.close();

        int responseCode = connection.getResponseCode();
        System.out.println("responseCode==>"+responseCode);
    //    System.out.println("responseCode msg==>"+connection.getErrorStream());
        System.out.println("responseCode msg==>"+connection.getContent());
        String headerId = connection.getHeaderField("OData-EntityId");
        System.out.println("headerId==>"+headerId);
        accountId = headerId.split("[\\(\\)]")[1];
        System.out.println("accountId==>"+accountId);
        return accountId;
    }

*This post is locked for comments

I have the same question (0)
  • Ben Thompson Profile Picture
    6,350 on at

    I would first try to use something like fiddler to see what the message being sent to Dynamics 365 looks like and then debug it using Postman until you've got a working message.

    However on first glance you seen to be saying that you have an attribute called oraclequotenumber in the quote entity which I suspect is not the case and it's likely to be either new_oraclequotenumber or whatever the solution publisher's prefix you are using is - so go back to the Dynamics 365 and check the actual logical name for the oraclequotenumber attribute...

  • Community Member Profile Picture
    on at

    yes, its  new_oraclequotenumber, i just edited here. But its not working though.

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Check if your able to update this record directly in CRM? 500 generally means the call is executed but not processed successfully due to internal error. That sad, it is possible that the problem is within that record or in CRM.\

    Hope this helps.

  • Community Member Profile Picture
    on at

    I am also facing the same problem . No solution found in the net.

    No supporting document for simple curd operation using java from microsoft

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Check following post - docs.microsoft.com/.../update-delete-entities-using-web-api

    I believe you should doublecheck that all your request headers are correct. Especially I will recommend to check following pieces:

    connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");//this is not mentioned in the post at all

    connection.setRequestMethod("POST");//For Update it should be PATCH, not POST

    connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");//try to use "application/json" instead

    As Ben already mentioned - test your requests using PostMan before start coding. Good luck.

  • Community Member Profile Picture
    on at

    The given sample code is not in java

    java doesn't support connection.setRequestMethod("PATCH");

    but we used connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");

    please check the above code

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Thanks for explanation but I know that this is not Java. This is general structure of request to be sent to update the record.

    Based on my fast search engine check it's not a problem of the WebApi but problem of Java that it does't allow to use Patch. I'm afraid you will have to use some workaround. As one possible way you can try using batch requests - docs.microsoft.com/.../execute-batch-operations-using-web-api I hope Java supports POST requests.

  • Community Member Profile Picture
    on at

    Call the below method "addHttpPatch"before update , It will  update the http method in java

    now you can call the patch as http method and remove "X-HTTP-Method-Override"

    private static void addHttpPatch() {
    
           try {
    
               Field methodsField = HttpURLConnection.class.getDeclaredField("methods");
    
               Field modifiersField = Field.class.getDeclaredField("modifiers");
    
               modifiersField.setAccessible(true);
    
               modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);
    
               methodsField.setAccessible(true);
    
               String[] oldMethods = (String[]) methodsField.get(null);
    
               Set<String> methodsSet = new LinkedHashSet<>(Arrays.asList(oldMethods));
    
               methodsSet.addAll(Arrays.asList("PATCH"));
    
               String[] newMethods = methodsSet.toArray(new String[0]);
    
               methodsField.set(null/*static field*/, newMethods);
    
           } catch (NoSuchFieldException | IllegalAccessException e) {
    
               throw new IllegalStateException(e);
    
           }
    
       }
    
        private static String UpdateQuote(String token, String accountId) throws MalformedURLException, IOException, URISyntaxException {
            JSONObject quote = new JSONObject();
            quote.put("new_oraclequotenumber", "456");
            quote.put("name","test223");
    addHttpPatch(); HttpURLConnection connection = null; //The URL will change in 2016 to include the API version - /api/data/v8.0/accounts URL url = new URL(RESOURCE + "/api/data/v9.1/quotes(D0BC8A5D-70FD-E811-A961-000D3A378CA2)"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("PATCH"); connection.setRequestProperty("OData-MaxVersion", "4.0"); connection.setRequestProperty("OData-Version", "4.0"); connection.setRequestProperty("Accept", "application/json"); connection.addRequestProperty("Authorization", "Bearer " + token); connection.setUseCaches(false); connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); connection.setRequestProperty("Prefer", "return=representation"); connection.setDoOutput(true); connection.connect(); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(quote.toJSONString()); out.flush(); out.close(); int responseCode = connection.getResponseCode(); System.out.println("responseCode==>"+responseCode); System.out.println("responseCode msg==>"+connection.getContent()); String headerId = connection.getHeaderField("OData-EntityId"); System.out.println("headerId==>"+headerId); accountId = headerId.split("[\\(\\)]")[1]; System.out.println("accountId==>"+accountId); return accountId; }
  • ananeto Profile Picture
    36 on at

    As an alternative to using WebAPI, you can get an integration tool between you (and your Java code) and the API. This is a clean solution and involves far less maintenance www.connecting-software.com/.../

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans