Code Page Formula Button
I have a formula button whose intended purpose is to check a box on the record (to start a pipeline) then redirect to a code page that is coded to pause, then check if a status field has changed, if not, pause again. If the field is changed it returns the user to the form originally launched from.
I'm trying to cobble together different posts/search results as I haven't been able to find any direct examples of my need.
This formula works to open the code page and passes the correct parameters for checking the status, but it doesn't check the box and thus the pipeline never runs and the page never closes.
var text codePageID = "6"; // Replace with your actual code page ID
var text recordID = ToText([Record ID#]);
var text currentStatus = URLEncode([Charges Pipeline Status]); // Replace with your field's name
var text url = URLRoot() & "db/" & AppID() & "?a=dbpage&pageid=" & $codePageID & "&rid=" & $recordID & "&initialStatus=" & $currentStatus;
"<a style= \" text-decoration:none; background: #b4b55d; border-radius: 15px; color: #fff; display: inline-block; padding: 10px 10px 10px 10px; width: 100%; font-size: 14px; text-align: center;\" href='" & $url & "'>Create New Charges</a>"
This formula correctly edits the checkbox but doesn't pass the status parameters (I do have the correct app token inserted in the field - replaced with [apptoken] here for reference:
var text urlToExecute = URLRoot() & "db/" & Dbid() & "?a=API_EditRecord&apptoken=[apptoken]"
& "&rid=" & [Record ID#] & "&_fid_12=true";
"<a style= \" text-decoration:none; background: #b4b55d; border-radius: 15px; color: #fff; display: inline-block; padding: 10px 10px 10px 10px; width: 100%; font-size: 14px; text-align: center;\" href='" & AppID() & "?a=dbpage&pageid=3" & "&url=" & URLEncode($urlToExecute) & "'>Create New Charges</a>"
This is my attempt at trying to combine the 2 formulas above:
var text codePageID = "6"; // Replace with your actual code page ID
var text recordID = ToText([Record ID#]);
var text currentStatus = URLEncode([Charges Pipeline Status]); // Replace with your field's name
var text url = URLRoot() & "db/" & Dbid() & "?a=API_EditRecord&apptoken=pamm2bcsqv939bufwq8hwzvwx4"
& "&rid=" & [Record ID#] & "&_fid_12=1" & AppID() & "?a=dbpage&pageid=" & $codePageID & "&rid=" & $recordID & "&initialStatus=" & $currentStatus;
"<a style= \" text-decoration:none; background: #b4b55d; border-radius: 15px; color: #fff; display: inline-block; padding: 10px 10px 10px 10px; width: 100%; font-size: 14px; text-align: center;\" href='" & $url & "'>Create New Charges</a>"
When I use the third formula, I get an error that says the XML file does not appear to have any style information associated with it and shows the following tree:
<qdbapi>
<action>API_EditRecord</action>
<errcode>0</errcode>
<errtext>No error</errtext>
<rid>7</rid>
<num_fields_changed>0</num_fields_changed>
<update_id>1757601111721</update_id>
</qdbapi>
Here is the script for the code page:
<script>
// Configuration
const recordId = new URLSearchParams(window.location.search).get('rid');
const initialStatus = new URLSearchParams(window.location.search).get('initialStatus');
const dbid = 'bvcwn2ues'; // <<< REPLACE WITH YOUR TABLE'S DBID
const appToken = 'pamm2bcsqv939bufwq8hwzvwx4'; // <<< REPLACE WITH YOUR APP TOKEN IF NEEDED
const statusFieldId = '40'; // <<< REPLACE WITH YOUR STATUS FIELD'S FID
const checkInterval = 120000; // 120 seconds in milliseconds
function checkStatus() {
$.ajax({
url: window.location.origin + '/db/' + dbid + '?a=API_DoQuery&qid=1&options=csv&apptoken=' + appToken + '&query={' + recordId + '.EX.' + recordId + '}',
type: 'GET',
success: function(response) {
const lines = response.trim().split('\n');
if (lines.length > 1) {
const data = lines[1].split(',');
const currentStatus = data[statusFieldId];
if (currentStatus !== initialStatus) {
// Status has changed! Redirect to the updated record.
window.location.href = window.location.origin + '/db/' + dbid + '?a=dr&rid=' + recordId;
} else {
// Status is the same. Pause and check again.
console.log('Status has not changed. Checking again in ' + checkInterval / 120000 + ' seconds.');
setTimeout(checkStatus, checkInterval);
}
} else {
console.log('Record not found or API error. Retrying...');
setTimeout(checkStatus, checkInterval);
}
},
error: function(xhr, status, error) {
console.error('API Error: ' + status, error);
setTimeout(checkStatus, checkInterval);
}
});
}
// Start the check after a brief initial pause
setTimeout(checkStatus, checkInterval);
</script>
I'm thinking I might need to add another const under the Configuration, but I just don't know enough to determine that. Or, I'm thinking that the structure of the button formula itself is where adjustments need to be made.
I appreciate all the assistance, but I have decided to go a different way. This option was to have simplified the mass creation of new records, but the pipeline process could take up to 10 minutes to fully complete, whereas, exporting to excel, making minor adjustments, then reimporting them takes only a minute or two.