Results 1 to 1 of 1

Thread: USPS Click and Ship fill in fields automated for your shipping pleasure

  1. #1
    Moderator
    Real Name
    James A. Talbott, Jr.
    Join Date
    Apr 2000
    Location
    Versailles, KY
    Posts
    1,395

    Default USPS Click and Ship fill in fields automated for your shipping pleasure

    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.

    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:
    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
    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
    Last edited by Jay Talbott; 09-24-2018 at 04:50 PM.
    Jay Talbott
    Lexington, KY


Similar Threads

  1. fill fields from another table, break down fields
    By FreeBird in forum Alpha Five Version 10 - Desktop Applications
    Replies: 4
    Last Post: 06-13-2010, 02:39 AM
  2. Need to fill 2 tables with one click
    By JackForman in forum Alpha Five Version 9 - Desktop Applications
    Replies: 1
    Last Post: 09-21-2009, 10:51 PM
  3. Fill in Fields
    By jmatson in forum Application Server Version 9 - Web/Browser Applications
    Replies: 4
    Last Post: 03-30-2009, 06:39 PM
  4. Is lookup the right choice to select & fill fields with 2 fields
    By MikeData in forum Alpha Five Version 9 - Desktop Applications
    Replies: 13
    Last Post: 01-10-2009, 05:21 PM
  5. Automated remove empty fields?
    By Bo Vandenberg in forum Alpha Five Version 5
    Replies: 3
    Last Post: 11-25-2003, 11:51 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •