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. 

3 comments:

  1. Hi
    Mention used in Transfer Function

    ReplyDelete
  2. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. סדנאות מכירות

    ReplyDelete
  3. This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. learn more

    ReplyDelete