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 :
Finance | Project Operations, Human Resources, ...
Suggested Answer

WinAPI::fileExists(tmpFile))//FILE not found error in batch

(0) ShareShare
ReportReport
Posted on by 8,705 Moderator

I have below code and dont ask why in batch the Bindata.loadfile has a file not found error

Yes i did full compile full CILL restart aos service.

code is running client site without any issues (AX 2012R3 latest binaries)

private container saveToLog(str _jsonstring)
{
  TextIo textIo;
  BinData binData = new BinData();

  FileIOPermission permission;
  str tmpFile;
  if(isRunningOnServer())
  {
    info('server');
    tmpFile = WinAPIServer::getTempPath()+guid2str(newGuid())+ '.json';
  }
  else
  { 
    info('client');
    tmpFile = WinAPI::getTempPath()+guid2str(newGuid())+ '.json';
  }

  permission = new FileIOPermission(tmpFile, 'RW');
  permission.assert();

  textIo = new TextIo(tmpFile,"W");
  textIo.write(_jsonstring);

  textIo = null;
  if(isRunningOnServer())
  {
    if(WinAPIServer::fileExists(tmpFile))
    {
      info('found');
    }

  else
  {
    info('not found');
  }
}
else
{
   if(WinAPI::fileExists(tmpFile))
   {
      info('found');
  }
  else
  {
       info('not found');
  }
}
if( binData.loadFile(tmpFile))//FILE not found error in batch
{
  return binData.getData();
}
if(isRunningOnServer())
{
   WinAPIServer::deleteFile(tmpFile);
}
else
{
   WinAPI::deleteFile(tmpFile);
}
return conNull();
}

I have the same question (0)
  • Suggested answer
    id365font Profile Picture
    398 on at

    Hello,

    You have to take into account that xode running in batch is executed by the aos service user from the aos server.

    Please review that the file path is accessible from the aos server and the aos user has permission to do the actions required.

  • Suggested answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    First of all, let me re-post your code in the right way (using Insert > Code), to make easier to read:

    private container saveToLog(str _jsonstring)
    {
    	TextIo textIo;
    	BinData binData = new BinData();
    
    	FileIOPermission permission;
    	str tmpFile;
    	
    	if (isRunningOnServer())
    	{
    		info('server');
    		tmpFile = WinAPIServer::getTempPath() guid2str(newGuid())  '.json';
    	}
    	else
    	{ 
    		info('client');
    		tmpFile = WinAPI::getTempPath() guid2str(newGuid())  '.json';
    	}
    
    	permission = new FileIOPermission(tmpFile, 'RW');
    	permission.assert();
    
    	textIo = new TextIo(tmpFile,"W");
    	textIo.write(_jsonstring);
    
    	textIo = null;
    	
    	if(isRunningOnServer())
    	{
    		if(WinAPIServer::fileExists(tmpFile))
    		{
    			info('found');
    		}
    
    		else
    		{
    			info('not found');
    		}
    	}
    	else
    	{
    		if(WinAPI::fileExists(tmpFile))
    		{
    			info('found');
    		}
    		else
    		{
    			info('not found');
    		}
    	}
    	
    	if (binData.loadFile(tmpFile))//FILE not found error in batch
    	{
    		return binData.getData();
    	}
    	
    	if(isRunningOnServer())
    	{
    		WinAPIServer::deleteFile(tmpFile);
    	}
    	else
    	{
    		WinAPI::deleteFile(tmpFile);
    	}
    	
    	return conNull();
    }

    I see that you let execution continue even if fileExists() fails to find the file. Please throw an exception in such a case, instead of merely showing an info and trying to load the non-existing file anyway.

    private container saveToLog(str _jsonstring)
    {
    	boolean onServer = isRunningOnServer();
    	TextIo textIo;
    	BinData binData = new BinData();
    
    	FileIOPermission permission;
    	str tempDir = onServer ? WinAPIServer::getTempPath() : WinAPI::getTempPath();
    	str tmpFile = strFmt('%1%2.json', WinAPIServer::getTempPath(), newGuid());
    	boolean fileExists;
    	
    	info(onServer ? 'server' : 'client';
    	
    	permission = new FileIOPermission(tmpFile, 'RW');
    	permission.assert();
    
    	textIo = new TextIo(tmpFile,"W");
    	textIo.write(_jsonstring);
    
    	textIo = null;
    	
    	boolean fileExists = onServer ? WinAPIServer::fileExists(tmpFile) : WinAPI::fileExists(tmpFile);
    	
    	if (!fileExists)
    	{
    		throw error("Not found");
    	}
    
    	if (binData.loadFile(tmpFile))
    	{
    		return binData.getData();
    	}
    	
    	if (onServer)
    	{
    		WinAPIServer::deleteFile(tmpFile);
    	}
    	else
    	{
    		WinAPI::deleteFile(tmpFile);
    	}
    	
    	return conNull();
    }

    Recompile X , build CIL, try it again and tell us where you code fails this time.

    By the way, I see that you don't delete the temporary file if data is successfully returned. Isn't it a bug?

    Nevertheless the whole saving to a file is not necessary - you can do the whole thing in memory. It would avoid the extra step and your problems would disappear, if there is no file at all. Namely, you can put the string to a MemoryStream and then use Binary class (constructFromMemoryStream() and getContainer()).

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 611 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans