+ Start a Discussion
ministe_2003ministe_2003 

populate fields with script while creating a new record

Hi,
I've signed up with postcode anywhere and have some script which gets the latitude and longitude of an address while it is being entered, and populates these values into custom number fields.  This works perfectly well on records which exist because I can populate the values with script by first querying the record from the database then entering the values.  Here's my code:

function (control, address) {
  var imported = document.createElement('script');
  imported.src = '/soap/ajax/30.0/connection.js';
  document.head.appendChild(imported);

  switch (control.salesforce.fields.Street) {
    case "acc17street": {
      var location = document.getElementById("acc17street").value + ", " +
      document.getElementById("acc17state").value + ", " +
      document.getElementById("acc17zip").value;
    break;
    }
    case "acc18street": {
      location = document.getElementById("acc18street").value + ", " +
      document.getElementById("acc18state").value + ", " +
      document.getElementById("acc18zip").value;
    break;
    }
    case "lea16street":{
      location = document.getElementById("lea16street").value + ", " +
      document.getElementById("lea16state").value + ", " +
      document.getElementById("lea16zip").value;
    break;
    }
  }

  pca.fetch("Geocoding/International/Geocode/v1.10", { Key: control.key, Location: location, Country: control.country },
    function (response) {
      debugger;

      var newRec;
      var recId;
      recId = window.location.href.substring( window.location.href.indexOf('.com/') + 5, window.location.href.indexOf('.com/') + 20 );

      switch (control.salesforce.fields.Street) {
        case "acc17street": {
          newRec = new sforce.SObject("Account");
          newRec.id = recId;
          newRec.Bill_To_Lat__c = response[0].Latitude;
          newRec.Bill_To_Long__c = response[0].Longitude;
        break;
        }
        case "acc18street": {
          newRec = new sforce.SObject("Account");
          newRec.id = recId;
          newRec.Ship_To_Lat__c = response[0].Latitude;
          newRec.Ship_To_Long__c = response[0].Longitude;
        break;
        }
        case "lea16street":{
          newRec = new sforce.SObject("Lead");
          newRec.id = recId;
          newRec.Lat__c = response[0].Latitude;
          newRec.Long__c = response[0].Longitude;
        break;
        }
      }

      var updResult = sforce.connection.update([newRec]);
    }
  );
}



The problem arrises when a user is creating a NEW record.  There is no record in the database for me to query so I don't know how I can populate the values.  Note that I do not wish to have the custom Lat__c and Long__c fields on the page layout (otherwise I could do this just by entering the values in the fields with javascript) and I cannot populate the values on the New event by url hacking, because I need the user to enter the address.

Does anyone know of a way I can populate fields on a record whilst it is being created?

Thanks
ministe_2003ministe_2003
I ought to add, this script works by adding it to a postcode anywhere section of salesforce and is run by them once an address is selected - it is not run by a salesforce button therefore I can't use merge fields :(