Showing posts with label API. Show all posts
Showing posts with label API. Show all posts

Thursday, July 12, 2018

How to use POSTMAN for MS DYNAMICS NAV 2018 API /MS BUSINESS CENTRAL?

Hi ,

If you have not read my earlier post than please go to below link before moving further in this blog

post.

API Creation in NAV and Setup

So I am going to give you an example of NAV API where I can insert , update , modify and delete

data using postman in database.

What is POSTMAN ?

As a NAV developer , I am new to this term but world is so advance and we need to be with them.

POSTMAN is basically a testing tool for and API.

As I understood one thing API is nothing but a URL which will give me data and do other things 

which will help me out in transaction. So I will never be interacting with original database.

POSTMAN has some attributes methods which interact with API.

What are attributes we have in POSTMAN?

POSTMAN can be downloaded from this LINK

There are 4 main transactions we do in any database.

1. Insert for this POSTMAN has POST

2. Delete for this POSTMAN has DELETE

3. Modify for this POSTMAN has PATCH

4. Select for this POSTMAN has GET

GET all API from NAV :

GET http://NAV2017:7048/DynamicsNAV110/api/beta/$metadata


GET http://nav2017:7048/DynamicsNAV110/api/beta/companies


How to get company information using API ID FIELD?

GET http://NAV2017:7048/DynamicsNAV110/api/beta/companies(68a76662-a198-477b-8097-d9df2116514a)/companyInformation


How to create a Customer or any entity in NAV?

POST http://desktop-jsuo248:7048/DynamicsNAV110/api/beta/companies(cbc66cd3-c138-4bfb-81f8-c5142f303a15)/customers

ENTER THE RAW FORMAT AS SHOWN IN SCREEN :


How to modify a Customer  or any entity in NAV?

PATCH http://desktop-jsuo248:7048/DynamicsNAV110/api/beta/companies(cbc66cd3-c138-4bfb-81f8-c5142f303a15)/customers(d39e6dfa-dfcf-4524-b02a-93d8c8967562)



How to delete a Customer or any enitity in NAV ?

DELETE http://desktop-jsuo248:7048/DynamicsNAV110/api/beta/companies(cbc66cd3-c138-4bfb-81f8-c5142f303a15)/customers(d39e6dfa-dfcf-4524-b02a-93d8c8967562)



How to use FIlters in API URL?

GET http://desktop-jsuo248:7048/DynamicsNAV110/api/beta/companies(cbc66cd3-c138-4bfb-81f8-c5142f303a15)/items?$filter=unitPrice%20gt%20100

How to get details about any Sales Invoice or any document from NAV?

GET http://desktop-jsuo248:7048/DynamicsNAV110/api/beta/companies(cbc66cd3-c138-4bfb-81f8-c5142f303a15)/salesInvoices(9dec16d6-122b-4ed3-ba0f-ac62dd562409)?$expand=salesInvoiceLines




Last 2 methods for creating Sales Invoices and posting it can be checked at








Tuesday, July 10, 2018

How to send SMS from MS Dynamics NAV or Business Central?

Hi ,

Below is the method to send SMS from NAV. It is a powerful tool we can achieve via using API.

Now all data centers are moving towards API.

So Dynamics NAV is also capable of using these APIs at a extreme level. Codeunits are now

available to developers for example CU 1290 ,1297 ,1299.

So below method I had used to send SMS and I am sure this method we can use for any API

integration in NAV.

Prerequisite :

API Credentials UserName and Password.

In my solution I had created 2 functions

Function 1 : CallRestWebService

Parameters :

Var Name DataType Subtype Length
No BaseURL Text

No Method Text

No RestMethod Text

Yes HttpContent DotNet System.Net.Http.HttpContent.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'


Yes HttpResponseMessage DotNet System.Net.Http.HttpResponseMessage.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'



Variables in this function :

Name         DataType Subtype     Length
HttpClient DotNet         System.Net.Http.HttpClient.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'


Uri DotNet System.Uri.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'


Code of This Function :

LOCAL CallRestWebService(BaseURL : Text;Method : Text;RestMethod : Text;VAR HttpContent : DotNet "System.Net.Http.HttpContent";VAR HttpResponseMessage : DotNet "System.Net.Http.HttpResponseMessage")

HttpClient := HttpClient.HttpClient();
HttpClient.BaseAddress := Uri.Uri(BaseURL);
CASE RestMethod OF
  'GET':    
    HttpResponseMessage := HttpClient.GetAsync(Method).Result;
  'POST':   
    HttpResponseMessage := HttpClient.PostAsync(Method,HttpContent).Result;
  'PUT':    
    HttpResponseMessage := HttpClient.PutAsync(Method,HttpContent).Result;
  'DELETE': 
    HttpResponseMessage := HttpClient.DeleteAsync(Method).Result;
END;
HttpResponseMessage.EnsureSuccessStatusCode(); // Throws an error when no success


Function 2 : SendSMS

VARIABLES

Name DataType Subtype Length
_StudentCollege Record Student - COLLEGE

Window DotNet Microsoft.VisualBasic.Interaction.'Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

data Text

httpUtility DotNet System.Web.HttpUtility.'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

encoding DotNet System.Text.Encoding.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

stringContent DotNet System.Net.Http.StringContent.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

HttpResponseMessage DotNet System.Net.Http.HttpResponseMessage.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'


Code of Function :

MessageText := Window.InputBox('Enter your message to Send :','INPUT','',100,100);

_StudentCollege.SETRANGE("Global Dimension 1 Code",InstituteCode);

IF _StudentCollege.FINDSET THEN  

  REPEAT

    data := 'User=' + httpUtility.UrlEncode('USERNAMECREDENTIAL',encoding.GetEncoding('ISO-8859-1'));

    data += '&passwd=' + httpUtility.UrlEncode('PASSWORDCRENDENTIAL',encoding.GetEncoding('ISO-8859-1'));

    data += '&mobilenumber=' + _StudentCollege."Mobile Number";

    data += '&message=' + httpUtility.UrlEncode(MessageText,encoding.GetEncoding('ISO-8859-1'));

    data += '&sid=SMSCntry&mtype=N&DR=Y';

    stringContent := stringContent.StringContent(data,encoding.UTF8,'application/x-www-form-urlencoded');

    CallRestWebService('http://api.smscountry.com/SMSCwebservice_bulk.aspx?',
                                                       '',
                                                       'POST',
                                                       stringContent,
                                                       HttpResponseMessage);
  UNTIL _StudentCollege.NEXT = 0;

MESSAGE('Done');


How this code is working I will discuss in my next blog. 

Thursday, May 31, 2018

API communication in MS Dynamics NAV 2018 Now Business Central.

Hi All ,

Once again I am getting stuck with calling of API in MS Dynamics NAV in my Germany office so I

thought why not get a dive into the sea of API. And lets check what it is in reality.

What is an API ?

Google says API is an Application Programming Interface. A way which provides us a facility to get 

data from another server.

But I cant understand now also. So lets get some diagrammatic view of an API.

I have a requirement from customer whenever a payment is done by a customer we need to check 

whether the customer has a credit balance to buy the article if no then send the notification to 

customer please contact the owner to increase your credit balance.

So here we were suggested to come with a solution of API we need to create one API which will be 

used by Paypal developers to checks whether this transaction is allowed or not.


I hope you got the point that what is an API.

API is generally a program written to say outer world that we will give you your data just send me 

the url for this requirement. In our example , my developed API will take the parameter of customer 

No. and will check in our customer table below points :

1. Customer Exist or Not.

2. If Exist , check the credit balance and proceed.

I hope concept is clear and requirement is also clear.

So what NAV developers will do now , we will create codeunit and will check the conditions and 

expose it to web service. But now we want to interpret with Paypal and they suggested API.

How to use API in NAV 2018?

First we will see how we can use then we will create API also.

Requirement from NAV Side :

1. Enable your NAV 2018 Admin for Soap and Odata Url.


If you want to go in more deep about API follow this video.




2. Search for API Setup page. Open it and click on Integrate API function it will populate all API present in NAV in Table API Web Service.  This is new system table.

3. We can check the data in Page but we need to create one page for this , as I had created this page on table API Web Service.
Please check how to create page on this table if you dont know in NAV from MSDN.


Lets Run this page.

You can create a web service for this page and check the data for this on browser.

The result of the data will be in JSON Format.

These are the following APIs provided by NAV 2018.


How to call API of NAV?

This is the URL provided by MSDN:

 http://<<Server Name>>:<<OData Port>>/<<Service Name>>/api/beta

For me its :

http://localhost:7048/DynamicsNAV110/api/beta

Hint : If you are not familiar with the url just open web service page and click on URL fields. There are 3 fields

ODATA V4 URL
ODATA URL
SOAP URL

With this you will get the desired URL for your system I followed the same.

After some time the result is as in Screen.


Now if its an API URL it should be present in POSTMAN. Lets check there also the result of our API  data is it available to other world.

For this I will write a new POST.

But I get 1 point now Microsoft is going to Business Central to a next Level of Data Library.