Getting Started > Use Lua Scripts > Method Reference > Example Script

Example Script
For each document, this Lua script adds a COUNT field, a total sections count to the title, and replaces the content of each section with the section number.
 
NOTE The COUNT is 1 for the first document and increases as long as the job is running.
doc_count = 0
function handler(document)
doc_count = doc_count + 1
document:addField("COUNT",doc_count);
 
local section_count = 0
local section = document
 
while section do
section_count = section_count + 1
section:setFieldValue("CONTENT", "Section "..section_count);
section = section:getNextSection()
end
local field = section:findField("DRECONTENT")
if field then
    section:fieldSetValue(field, "Section "..section_count);
end
section = section:getNextSection()
end
 
document:setFieldValue("TITLE",
document:getFieldValue("TITLE").." Total Sections "
..section_count)
return true;
local field = document:findField("DRETITLE")
if field then
document:fieldSetValue(field, document:fieldGetValue(field).." Total Sections "..section_count)
end
end