Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

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

Posted on by 8,705

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
    Martin Dráb 230,198 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
    id365font 400 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans