For those of you who use (or whose clients use) the USPS Click -N- Ship occasionally, but not enough to create a full blown API, here's a JavaScript function you can use with the console in Chrome Developer's Tools or Firefox's Firebug. The impetus for this was an unexpectedly successful tee shirt sale for the car club I am involved in. We had over 230 orders, so the shipping needed to be automated somewhat.
The state number function in the javascript converts the state name to the appropriate state number (I have no idea why the USPS numbers the states that way).
I hope you find this useful.
Jay
function usps(){
var fn = 'document.getElementById("deliveryAddressFirstName").value = "_First_name"';
var mi = 'document.getElementById("deliveryAddressMiddleInit").value = "_MI"';
var ln = 'document.getElementById("deliveryAddressLastName").value = "_Last_name"';
var a1 = 'document.getElementById("deliveryAddressLine1Addr").value = "_Address_1"';
var a2 = 'document.getElementById("deliveryAddressLine2Addr").value = "_Address_2"';
var c = 'document.getElementById("deliveryAddressCityName").value = "_City"';
var s = 'document.getElementById("deliveryAddressState").value = "_State"';
var z = 'document.getElementById("deliveryAddressPostalCode").value = "_Zip"';
var firstName = {Dialog.object}.getValue('FirstName');
var middleInitial = {Dialog.object}.getValue('MI');
var lastName = {Dialog.object}.getValue('LastName');
var address1 = {Dialog.object}.getValue('Address1');
var address2 = {Dialog.object}.getValue('Address2');
var city = {Dialog.object}.getValue('City');
var state = {Dialog.object}.getValue('State');
var zip = {Dialog.object}.getValue('Zip');
debugger;
zip = zip.slice(1,6);
var jsFinal = fn.replace('_First_name',firstName);
jsFinal = jsFinal + ';\n' + mi.replace('_MI',middleInitial);
jsFinal = jsFinal + ';\n' + ln.replace('_Last_name',lastName);
jsFinal = jsFinal + ';\n' + a1.replace('_Address_1',address1);
jsFinal = jsFinal + ';\n' + a2.replace('_Address_2',address2);
jsFinal = jsFinal + ';\n' + c.replace('_City',city);
jsFinal = jsFinal + ';\n' + s.replace('_State',stateNumber[state]);
jsFinal = jsFinal + ';\n' + z.replace('_Zip',zip);';';
jsFinal = jsFinal + '\ndocument.getElementById("deliveryAddressState-button").click();'
jsFinal = jsFinal + '\ndocument.getElementById("deliveryAddressState-button").click();'
{Dialog.object}.setValue('uspsJS',jsFinal);
var myControl = {Dialog.object}.getPointer('uspsJS');
myControl.select();
setTimeout(function(){
document.execCommand('copy'); // to copy the contents of the <textarea> to the clipboard.
},750)
var response = confirm('Did the label print ok?');
var id = {Dialog.object}.getValue('list::list1::ID');
if (response == true){
{dialog.object}.ajaxCallback('','','markRecordMailed','','__id='+id);
{Dialog.object}.setValue('uspsJS','');
{dialog.object}._listRefreshRow('list1');
}else{
alert('Record not marked.\n\nYou can try again.');
}
}
var stateNumber = {
'AL': '5',
'AK': '4',
'AS': '8',
'AZ': '9',
'AR': '7',
'CA': '10',
'CO': '11',
'CT': '12',
'DE': '14',
'DC': '13',
'FM': '16',
'FL': '15',
'GA': '17',
'GU': '18',
'HI': '19',
'ID': '21',
'IL': '22',
'IN': '23',
'IA': '20',
'KS': '24',
'KY': '25',
'LA': '26',
'ME': '29',
'MH': '30',
'MD': '28',
'MA': '27',
'MI': '31',
'MN': '32',
'MS': '35',
'MO': '33',
'MT': '36',
'NE': '39',
'NV': '43',
'NH': '40',
'NJ': '41',
'NM': '42',
'NY': '44',
'NC': '37',
'ND': '38',
'MP': '34',
'OH': '45',
'OK': '46',
'OR': '47',
'PW': '50',
'PA': '48',
'PR': '49',
'RI': '51',
'SC': '52',
'SD': '53',
'TN': '54',
'TX': '55',
'UT': '56',
'VT': '59',
'VA': '57',
'VI': '58',
'WA': '60',
'WV': '62',
'WI': '61',
'WY': '63'
}
If it is not obvious, there is a text area on the form called uspsJS.
The "markRecordMailed" Xbasic AJAX callback looks like this, and marks the record as processed so the USPS label does not get created twice, so:
var fn = 'document.getElementById("deliveryAddressFirstName").value = "_First_name"';
var mi = 'document.getElementById("deliveryAddressMiddleInit").value = "_MI"';
var ln = 'document.getElementById("deliveryAddressLastName").value = "_Last_name"';
var a1 = 'document.getElementById("deliveryAddressLine1Addr").value = "_Address_1"';
var a2 = 'document.getElementById("deliveryAddressLine2Addr").value = "_Address_2"';
var c = 'document.getElementById("deliveryAddressCityName").value = "_City"';
var s = 'document.getElementById("deliveryAddressState").value = "_State"';
var z = 'document.getElementById("deliveryAddressPostalCode").value = "_Zip"';
var firstName = {Dialog.object}.getValue('FirstName');
var middleInitial = {Dialog.object}.getValue('MI');
var lastName = {Dialog.object}.getValue('LastName');
var address1 = {Dialog.object}.getValue('Address1');
var address2 = {Dialog.object}.getValue('Address2');
var city = {Dialog.object}.getValue('City');
var state = {Dialog.object}.getValue('State');
var zip = {Dialog.object}.getValue('Zip');
debugger;
zip = zip.slice(1,6);
var jsFinal = fn.replace('_First_name',firstName);
jsFinal = jsFinal + ';\n' + mi.replace('_MI',middleInitial);
jsFinal = jsFinal + ';\n' + ln.replace('_Last_name',lastName);
jsFinal = jsFinal + ';\n' + a1.replace('_Address_1',address1);
jsFinal = jsFinal + ';\n' + a2.replace('_Address_2',address2);
jsFinal = jsFinal + ';\n' + c.replace('_City',city);
jsFinal = jsFinal + ';\n' + s.replace('_State',stateNumber[state]);
jsFinal = jsFinal + ';\n' + z.replace('_Zip',zip);';';
jsFinal = jsFinal + '\ndocument.getElementById("deliveryAddressState-button").click();'
jsFinal = jsFinal + '\ndocument.getElementById("deliveryAddressState-button").click();'
{Dialog.object}.setValue('uspsJS',jsFinal);
var myControl = {Dialog.object}.getPointer('uspsJS');
myControl.select();
setTimeout(function(){
document.execCommand('copy'); // to copy the contents of the <textarea> to the clipboard.
},750)
var response = confirm('Did the label print ok?');
var id = {Dialog.object}.getValue('list::list1::ID');
if (response == true){
{dialog.object}.ajaxCallback('','','markRecordMailed','','__id='+id);
{Dialog.object}.setValue('uspsJS','');
{dialog.object}._listRefreshRow('list1');
}else{
alert('Record not marked.\n\nYou can try again.');
}
}
var stateNumber = {
'AL': '5',
'AK': '4',
'AS': '8',
'AZ': '9',
'AR': '7',
'CA': '10',
'CO': '11',
'CT': '12',
'DE': '14',
'DC': '13',
'FM': '16',
'FL': '15',
'GA': '17',
'GU': '18',
'HI': '19',
'ID': '21',
'IL': '22',
'IN': '23',
'IA': '20',
'KS': '24',
'KY': '25',
'LA': '26',
'ME': '29',
'MH': '30',
'MD': '28',
'MA': '27',
'MI': '31',
'MN': '32',
'MS': '35',
'MO': '33',
'MT': '36',
'NE': '39',
'NV': '43',
'NH': '40',
'NJ': '41',
'NM': '42',
'NY': '44',
'NC': '37',
'ND': '38',
'MP': '34',
'OH': '45',
'OK': '46',
'OR': '47',
'PW': '50',
'PA': '48',
'PR': '49',
'RI': '51',
'SC': '52',
'SD': '53',
'TN': '54',
'TX': '55',
'UT': '56',
'VT': '59',
'VA': '57',
'VI': '58',
'WA': '60',
'WV': '62',
'WI': '61',
'WY': '63'
}
If it is not obvious, there is a text area on the form called uspsJS.
The "markRecordMailed" Xbasic AJAX callback looks like this, and marks the record as processed so the USPS label does not get created twice, so:
function markRecordMailed as c (e as p)
' debug(1)
'markRecordMailed = "javascript commands to be executed in the Browser."
dim cn as sql::Connection
dim sqlUpdateStatement as c = ""
dim args as sql::Arguments
args.Set("ID", val(e.__id))
cn.Open("::name::conn")
sqlUpdateStatement = <<%txt1%
UPDATE ORDERS_EXPORT
SET LABEL_CREATED = 1
WHERE ID = :ID
%txt1%
cn.Execute(sqlUpdateStatement,args)
end function
' debug(1)
'markRecordMailed = "javascript commands to be executed in the Browser."
dim cn as sql::Connection
dim sqlUpdateStatement as c = ""
dim args as sql::Arguments
args.Set("ID", val(e.__id))
cn.Open("::name::conn")
sqlUpdateStatement = <<%txt1%
UPDATE ORDERS_EXPORT
SET LABEL_CREATED = 1
WHERE ID = :ID
%txt1%
cn.Execute(sqlUpdateStatement,args)
end function
I hope you find this useful.
Jay
Comment