Sample Logic app using Recurring integration in D365 fo

 Often we need to create data in D365 environment from a 3rd party system. Here this is basic logic app that can help to creates the data by taking the files from SFTP folder and and Creates data in d365.


1. In my case Im taking the trigger point as Recurrence as it should process the files at time on each end of the day. you can also take when file is added.












2. Take the next step and add the scope. our operations will be go on inside the scope as we can determine the logic app run process weather it is failed succeed aborted.








3. Now take the List files in the folder step from SFTP-SSH, this action will retrieve all the files from the specified folder.









4. Now take a for_each loop which will loop the each file which we will get the list in the above step. Pass body of the above step as the input for the for each loop. Add the action Get files content to read the content of the file.













You can take the file content below.



4. Now pass the file content to D365 by using the recurring integration API.  To know more about the RI please visit y blog here.
5. To connect to the D365 we would required a Tenet ID, Client Id, Client secret for the authentication.
for this step I'm creating one more global logic app with HTTP trigger which is used to get trigger the file to D365. because we can not take the request action inside a for_each loop.
6. For the logic app am taking the inputs as below.
{
    "properties": {
        "Audience": {
            "type": "string"
        },
        "CSVInputRequest": {
            "type": "string"
        },
        "InputRequestType": {
            "type": "string"
        },
        "JsonInputRequest": {
            "type": "object"
        },
        "Method": {
            "type": "string"
        },
        "URI": {
            "type": "string"
        }
    },
    "required": [
        "URI"
    ],
    "type": "object"
}
7. with The above inputs we need to create a below step for the web request.




8. Now from the above step we will get the failure and success response as below. 

9. Now go to original app and call the above created logic app and pass the inputs as below.

10. From the above you will get the respose with a message ID. To get the processing status of message queue I have added the below step. take the delay of 3 mins.

11. Now take Until step to get any one of  status added in the condition.

@or(equals(body('GetMessageStatus')?['value'], 'Processed'),equals(body('GetMessageStatus')?['value'], 'ProcessedWithErrors'),equals(body('GetMessageStatus')?['value'], 'PostProcessingError'),equals(body('GetMessageStatus')?['value'], 'PreProcessingError'))









12. Now trigger the getMessagestatus API by using the above created gobal logic app.

13. if it is processed we need to move this to Archive file.


14. if the status other than this we need to move this to error folder.



Follow the process step by step for any host-to-host integration using Logic Apps. Feel free to share your suggestions.

Thanks !!



Upload / read files from the FTP server using X++

In the code below, we can see how files are transferred to an FTP server using X++.

Prerequisites :

FTP Address:  The address used to connect to the FTP server.  
Username:       The authorized username required for logging into the FTP server.  
Password:        The password needed to access the FTP.  
Folder paths:   The locations of folders where files can be placed or read.


    public void sendFileToFTP(System.IO.Stream  _stream)
    {
        System.Text.Encoding 		            getUTF8;
        System.Byte[] 			                bytes;
        System.Object 			                request,response,credential;
        System.Net.FtpWebRequest 	            ftpRequest;
        System.IO.Stream 		                requestStream;
        System.Net.FtpWebResponse 	            ftpResponse;
        System.IO.StreamReader 		            reader;
        System.Char[] 			                chars;
        Str1260 			                    ftpFileName =  "ftp://<Ipaddress>//INPUT/UNPROCESSED/ "+ filename ;  // folder paths
        
        try
        {
        _stream.Position = 0;
        // Encode
        reader 	= new System.IO.StreamReader(_stream);
        getUTF8 = System.Text.Encoding::get_UTF8();
        bytes 	= getUTF8.GetBytes(reader.ReadToEnd());
 
        // Creating request
        request 	= System.Net.WebRequest::Create(new System.Uri(ftpFileName));
        ftpRequest 	= request;
 
        // Creating credentials
        credential = new System.Net.NetworkCredential('UserName', 'Password');
 
        // assign parameters to reuest
        ftpRequest.set_Credentials(credential);
        ftpRequest.set_ContentLength(bytes.get_Length());
        ftpRequest.set_Method("STOR");
     
 
        // Write file to stream
        requestStream = ftpRequest.GetRequestStream();
        requestStream.Write(bytes,0,bytes.get_Length());
        requestStream.Close();
 
        // Get respose
        response = ftpRequest.GetResponse();
        ftpResponse = response;
        info('Uploded.');
        }
        catch
        {
            error("failed");
        }
    }
public static str readFileFromSFTP(FileName _fileName)
    {
        System.Object                   ftpo;
        System.Net.FtpWebRequest        request;
        System.IO.StreamReader          reader;
        System.Net.NetworkCredential    credential;
        System.Net.FtpWebResponse       response;
        Str1260                         text;
        UserId                          usernameFTP;
        Password                        password;
        Str1260                         readingPath;
 
        try
        {
            ttsbegin;
            password     = cryptoblob2str(WinAPIServer::cryptUnProtectData('password'));
            ftpo            = System.Net.WebRequest::Create(@'FolderpathtoReadFie' + @"/" + _fileName);
            request         = ftpo;
 
            credential      = new System.Net.NetworkCredential('UserName', password);
            request.set_Credentials(credential);

            response        = request.GetResponse();
            reader          = new System.IO.StreamReader(response.GetResponseStream());
            text            = reader.ReadToEnd();
            ttscommit;
        }
        catch
        {
            error("Error reading files");
        }
        return text;
   }

Keep Daxing !!