Using C/AL to extract page information

Hi Guys,

Just a quick question, I wish to use an action (controlled by the press of a button) to extract information from a page then run a file.

I wish to extract the contents of the <No.> field on a page and write this information to an external text file. Is this possible? Does anyone have an example chunk of C/AL code that might do the job? It doesn’t have to be pretty

- OnAction()


CODE TO EXTRACT THE CONTENTS OF THE<NO.> FIELD

CODE TO RUN AN EXTERNAL EXECUTABLE


Many thanks as always

Well, if you’re on the page you just use the field name. So if you wanted to export the No. field of a posted sales invoice you would just use “No.” as the text that you want to export.

If you’re looking into writing to files, start with looking up File and OutStream in the NAV Help Menu. Because it is a three tier environment, you will also need to use DOWNLOAD, or write to a shared drive somewhere. Depending on your requirements you could also use an XMLport.

Hi Matt,

Just to clarify, it’s contents of the No. field I wish to export, let’s assume that is 1006

I’ve created a CODEUNIT which calls a web browser and opens it to the location I wish under the click of a button. At the moment this is a fixed value to demonstrate the principal.

What I’d like to do to make it functional is lookup part of the URL address as the No. value, so an example would be:

http://webserver/scripts/myviewer.exe?DOCSEARCH=DOCNUMBER=1006

Which displays an image previously scanned into our system.

You can hopefully see what we are doing. A user scans in an image of a financial document giving it meaningful data to our webserver. This can be viewed using search criteria as above example.

A button is created within the NAV interface to link to myviewer.exe and is passed the No.

So in conclusion, when the button is pressed it calls the code unit.

This contains the relevant C/AL code which simply writes the contents of the <No.> field of the Page the RTC is currently on, (let’s say 1006) to a file in c:\navfile.txt, that’s it.

Do you have a simple C/AL example code that might achieve this.

I’m looking at the In / OutStream functions to do this, would that be the right approach?

F1.OPEN('c:\Test.txt');
F1.CREATEINSTREAM(InS);
F2.CREATE(CONTENTSOFNO.FIELD);
F2.CREATEOUTSTREAM(OutS);
COPYSTREAM(OutS,InS);
F1.CLOSE();
F2.CLOSE();

Which would produce a file

c:\Test.txt 

which contains the text

1006

If so I'm not sure of the syntax of the CONTENTSOFNO.FIELD, I'm trying just **No.** and it doesn't like it. :(
Apologies in advance, I'l still learning the C/AL side of things!

MyFile.CREATE(‘c:\tmp\test.txt’);
MyFile.CREATEOUTSTREAM(OutStream);
OutStream.WRITETEXT(“No.”);
MyFile.CLOSE;

This will create the file on the machine where the Service Tier runs.

In the C/SIDE online help you often find very small examples for tasks like this. Have a look.

Hi Thomas,

I’m using the following:

CLEAR(MyFile);
MyFile.TEXTMODE(TRUE);
MyFile.WRITEMODE(TRUE);
MyFile.CREATE(‘c:\temp\nav.txt’);
MyFile.WRITE(‘No.’);
MyFile.CLOSE();

Which works, but as mentioned I don’t want No. in the text file, I want the contents of the No.l field on the Page, in this case 1006 which is the No. of the Woodmart supply Purchase Invoice. Is this possible using this method?

If you want your the contents of the field you need to use double quotes, not single quotes. Single quotes means the exact string. You should really check out the NAV documentation on coding. If you are a customer and paying your maintenance it is all available for free.

‘No.’ is not equal to “No.”

With single quotes it is a string with double quotes the No. field.

From the online help:

You can include one or more special characters in a variable name in C/AL. If you include special characters, then the variable name must be enclosed in quotation marks. In this case, the name can contain any mix of letters, digits, and special characters.

I realise the quotes are significant, if I double them up it tells me I have specified an unknown variable No. I have zero experience in C/AL but know many other languages so I under stand the principles, it’s just knowing where to go within the classic client, it doesn’t seem as intuitive as anything I’ve used before and there’s only so many examples you can read when non of them I’ve found are appropriate to what I’m doing!

Again …

CLEAR(MyFile);
MyFile.TEXTMODE(TRUE);
MyFile.WRITEMODE(TRUE);
MyFile.CREATE(‘c:\temp\nav.txt’);
MyFile.WRITE(‘testing 123’);
MyFile.CLOSE();

This CODEUNIT is underneath a button on my Purchase Invoice Page which works fine, this example writes the text file which contains testing 123.

Instead of testing 123, what would be the correct syntax using the above example to extract THIS field to the text file so it contains 1006 /or whatever number is in that field. I need to use this CODEUNIT throughout which must always output the number in the No. field.

Life saver whoever can tell me, I’m stumped!

You can use the field menu, F5, while you’re coding to see all of the variables you have available to you. Have you passed your record variable to the function? The record is just like a class (well, sort of) in OOP, so you use dot syntax to access its field. Record.Field.

Again, I know it’s frustrating, being used to other languages, but read the documentation. There are examples for how to do all of these tasks. Maybe not in one place for exactly what you want, but they are there. There is chapter on using record variables, examples of how to write to files, examples of how to create functions and pass parameters, etc.

It is nice to see someone trying it out on their own first, though, I must say. [:D]

I thought the code would be in the OnAction trigger of the page.

Hi Matt,

I find it far more satisfying :slight_smile:

No, I don’t know what form that takes for this example, I Press F5 > Is it a PAGE? A FORM? What form does the record variable take?

I know it’s frustrating for you answering a beginner … all I’m asking is a a quick guide to achieve it and I’ll leave you alone lol

With me, for better or worse, I hardly ever tell anyone the exact solution. I find it more satisfying to help them learn.

The Field Menu, or C/AL Symbol Menu is used when viewing the code. I’m not sure exactly how you have your codeunit structured, whether you are relying on the OnRun trigger or a function. For the former you need to look at the TableNo property (Shit + F4 when viewing the code). For the latter you need to define an input parameter to the function. Similar to how you defined your variables for the streams and files, but they are done under the locals instead of globals.

You should also know that the Rec variable on a page always refers to the current record, so that is what you will be passing to your codeunit.

Go with that and let us know what you figure out.

You know, I’m going to take back what I said above. You should do it more like this. Instead of having the function in a Codeunit, write the function in the table that you want to export. Then just call that function directly from the page action.

Cracked it … The problem was I was trying to do the work in the CODEUNIT not having access to the globals, if I’d use the action directly I can refer to the Rec.No

Now when I hit my button, it writes to the txt file [H] FYI Here is the code under the Action for the button:

CLEAR(MyFile);

.

.

MyFile.WRITE(Rec.“No.”);

Thanks again, great job. :slight_smile: