Select a file from the beginning of a flow

Post Reply
User avatar
bkromer
Member
Posts: 46
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Select a file from the beginning of a flow

Post by bkromer » Fri Jul 17, 2020 1:16 pm

Hello,

I have this flow:
Bildschirmfoto 2020-07-17 um 13.07.23.png
Bildschirmfoto 2020-07-17 um 13.07.23.png (41.24 KiB) Viewed 322 times
In the Flow I get a PDF-File from Email convert it to text search with regex in the text.

When I work with the File i always use File.read(job.getPath(), 'UTF-8'); wich gives me the incoming file wich is the txt-File.
Now I also need the PDF from the beginning, how can I select this?

I want to upload it to an API with theHTTP.setAttachedFile(job.getPath() , filename); ...

Thanks in Advance for your Help! :)
Benjamin Kromer

jan_suhr
Advanced member
Posts: 336
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Select a file from the beginning of a flow

Post by jan_suhr » Fri Jul 17, 2020 1:31 pm

You have to branch out before the text conversion so you get a copy of the PDF. Then you can upload that one.

The PDF you convert to text is sent to null after the conversion.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

User avatar
bkromer
Member
Posts: 46
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: Select a file from the beginning of a flow

Post by bkromer » Fri Jul 17, 2020 1:36 pm

okay, I have this PDF ARCHIV 48h trashcan in there, where all the incoming PDF are archived for 48 hours.
Is there a way to select the belonging PDF-file in the script, some job.get... function or similar?
Benjamin Kromer

jan_suhr
Advanced member
Posts: 336
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Select a file from the beginning of a flow

Post by jan_suhr » Fri Jul 17, 2020 2:36 pm

Branch of to one folder before the pdfToolbox element and then from that folder back to the folder before the script, then both the PDf and the TXT will be going in to the script. In the script you have to pick up both the PDF and the TXT file.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

User avatar
bkromer
Member
Posts: 46
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: Select a file from the beginning of a flow

Post by bkromer » Fri Jul 17, 2020 2:47 pm

Hmmm... :|
But I run my script for every file which means It would handle the PDF and TXT file separately. So I will end up with 2 new entries in my database.

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var pdf =  File.read( job.getPath(), "UTF-8" );// PDF Text einlesen
		function getSuppliers(){
		var theHTTP = new HTTP( HTTP.SSL );
		theHTTP.authScheme = HTTP.ProprietaryAuth;
		theHTTP.addHeader( "Authorization", "Bearer asdasdasdasd");
		theHTTP.url = "https://api.ninoxdb.de/v1/teams/asdadsasd/databases/asdasd/tables/ASDDDrecords?perPage=9999"; 
		theHTTP.get();
		while (!theHTTP.waitForFinished(3)) {
			job.log(1, 'API CALL IN PROGRESS!');
		}
	
		if (theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200) {
			job.log(1, 'getSuppliers - API CALL successfull RESPONSE: ' +theHTTP.statusCode ); // theHTTP.getServerResponse().toString('UTF-8')
			job.log(1,"FULL RESPONSE GET SUPPLIERS: "+theHTTP.getServerResponse().toString('UTF-8'))
			supplierJson = JSON.parse(theHTTP.getServerResponse().toString('UTF-8'));
			
			// job.log( 1, JSON.stringify( jsonReturn[0]['fields']['Bestellungen'] ));
			
			// job.log(1,jsonReturn[0]['fields']['Name oder Firma']);
			
		} else {
			job.log(
				3,
				'API CALL Failed:  ' +
					theHTTP.getServerResponse().toString('UTF-8') +
					'  Status Code: ' +
					theHTTP.statusCode.toString('UTF-8')
			);
		}
		return supplierJson
	}	
	function getCompany( supplierJson, fileAsText ){
		var newSupplierJson = [];
		for ( var j=0; j< supplierJson.length; j++ ){
				if ( "UST ID" in supplierJson[j]['fields'] ){
					newSupplierJson.push(supplierJson[j]);
				}
		}
		//File.write("C:\\Users\\Administrator.BECHTELDRUCK\\Nextcloud\\Documents\\coding\\SwitchScripts\\RECHNUNGSEINGANG\\RECHNUNGEN AUSLESEN TEST\\TXT LOWER ARCHIV\\"+job.getName(), pdfLower, 'UTF-8'); // AUCH ALS LOWER TEXT ABSPEICHERN!
		// job.log(1,"länge json: "+supplierJson.length);
		supplierId = 0;
		 for (var i=0; i< newSupplierJson.length; i++){
			var supplierUSTID = newSupplierJson[i]['fields']['UST ID'];
			var regex = "("+supplierUSTID+")";
			var pattern = new RegExp( regex );
			pattern.global = true; 
			pattern.ignoreCase = true;
			//job.log(1,i);
			var matched = pdf.match(pattern);
				if (matched){
					job.log(1,"Pattern found: "+supplierUSTID+" Name: "+ newSupplierJson[i]['fields']['Name oder Firma'] );
					job.setPrivateData("pdSupplierUSTID", supplierUSTID );	
					job.log(1,"matches: "+matched );
					job.log(1, JSON.stringify( newSupplierJson[i] ) );
					supplierId = newSupplierJson[i]['id'];
				}
			}
			job.sendToSingle( job.getPath() );
		return supplierId	
};
	function upload2Ninox(supplierId){
	name = job.getName();
	prefix = job.getUniqueNamePrefix();
	filename = name.replace(prefix,"");
	var pdf =  File.read( job.getPath(), "UTF-8" );// PDF Text einlesen
	var encodedText = HTTP.encodeURI( pdf );
	var nummer = "asd";
	var date="2020-07-16";
	job.log(1,date);
	var doktyp = "Rechnung";
	
	var theHTTP = new HTTP (HTTP.SSL);
	theHTTP.authScheme = HTTP.ProprietaryAuth;
	theHTTP.authorization = "Bearer asdasd";
	theHTTP.url = "https://api.ninoxdb.de/v1/teams/asdasd/databases/gasasd/tables/ASD/records";
	var json = new ByteArray(
		'{"fields":{"Lieferanten":"' +
		supplierId+
		'","Nummer":"' +
		nummer +
		'","Datum":"' +
		date +
		'","Dokumenttyp":"' +
		doktyp +
		'","pdf als text":"' +
		 encodedText +
		'"}}',
	'UTF-8'
	);
	theHTTP.setPostData(json, 'application/json');
	theHTTP.post();
	while( !theHTTP.waitForFinished( 3 ) )
	  {
	       job.log( 1, "API CALL IN PROGRESS!" );
	  } 
		
	if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
	  {
		job.log(1," upload2Ninox - API CALL successfull RESPONSE: "+theHTTP.getServerResponse().toString( "UTF-8" ));
		// UPLOAD VIA API TO NINOX DATABASE
		try{
		var response = JSON.parse(theHTTP.getServerResponse().toString( "UTF-8" ));
		var ninoxId =response.id;
		job.setPrivateData("pdNinoxId", ninoxId );	
		var theHTTP = new HTTP( HTTP.SSL );
		theHTTP.authScheme = HTTP.ProprietaryAuth;
		theHTTP.authorization = "Bearer asdasd";
		theHTTP.setAttachedFile(     job.getPath() , filename );     
		theHTTP.url = "https://api.ninoxdb.de/v1/teams/uasdasdQ/databases/gasdadv/tables/ASD/records/"+id+"/files"; 
		theHTTP.enableMime = true;
		theHTTP.post();
		while( !theHTTP.waitForFinished( 3 ) )
		  {
		       job.log( 1, "API CALL IN PROGRESS!" );
		  } 
			
		if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
		  {
				
		       
				job.log(1,"API CALL successfull RESPONSE: "+theHTTP.getServerResponse().toString( "UTF-8" ));
		  }
		else
		{
			job.log(3,"API CALL Failed:  "+theHTTP.getServerResponse().toString( "UTF-8" )+ "  Status Code: "+theHTTP.statusCode.toString( "UTF-8" ));
		}
		}
		catch(theError) {
			job.log(3,'json parse error: ' +theError);
		}
		
		  }
		else
		{
			job.log(3,"API CALL Failed:  "+theHTTP.getServerResponse().toString( "UTF-8" )+ "  Status Code: "+theHTTP.statusCode.toString( "UTF-8" ));
		}
	}
	// Ausführung
	getSuppliers();	
	getCompany(supplierJson,pdf);
	upload2Ninox(supplierId);
}
Benjamin Kromer

jan_suhr
Advanced member
Posts: 336
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Select a file from the beginning of a flow

Post by jan_suhr » Fri Jul 17, 2020 3:05 pm

Well can't you let the first file create the record and the second update that record?
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

Post Reply