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();
}

  • Suggested answer
    Martin Dráb Profile Picture
    233,017 Most Valuable Professional on at
    RE: WinAPI::fileExists(tmpFile))//FILE not found error in batch

    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()).

  • Suggested answer
    id365font Profile Picture
    398 on at
    RE: WinAPI::fileExists(tmpFile))//FILE not found error in batch

    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.

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,261 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,017 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans