Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 82002

JavaScript to lookup address and update text field on order form

$
0
0

I have a section of javascript that is to pull back information from the bill-to address selected for an order using the "Lookup Address."

I see the form update when I select the new address, but when I pull the addressid(GUID) of the bill-to address, I get the previous address.

When is the address GUID associated with the order updated to the order record? From the code I have inserted below, I have pushed information to the screen to show the GUID, but it is the GUID of the previous address and not the new one that I selected.

When I save the order form, there is a call to "updateAddresses"

function updateAddresses(executionContext) {
  var formContext = executionContext.getFormContext();

    if (isEditable(formContext)) {
        var shipToAddressId = getFieldValue(formContext, "shipto_addressid");
        var billToAddressId = getFieldValue(formContext, "billto_addressid");
window.alert('The ' + shipToAddressId + '<-shipToAddressId');

    getAddress(formContext, shipToAddressId, 'ship', function (shipToAddress) {
      getAddress(formContext, billToAddressId, 'bill', function (billToAddress) {
        updateAddressFields(formContext, shipToAddress, billToAddress);
        var saveArgs = executionContext.getEventArgs();
        if (saveArgs.getSaveMode() !== 1) formContext.data.save(); // prevent infinite save loop
      });
    });
  }
}

function getAddress (formContext, addressId, whichAddress, next) {

  if (!addressId) {
    next();
    return;
  }

  addressId = addressId.replace('{', '').replace('}', '');
  var url = encodeURI('/api/data/v9.0/customeraddresses(' + addressId + ')');
  var address;
  var req = new window.XMLHttpRequest();

//     formContext.ui.setFormNotification("URL has been built","WARNING","urlvaluecall");
  window.alert("url=> " + url);

  req.onreadystatechange = function () {
    if (req.readyState === 4) {
      if (req.status === 200 || req.status === 304) {
        window.alert("getaddress successful" + req.readyState);
   
        address = JSON.parse(req.responseText);
           window.alert("address=> " + address);


      } else if (req.status === 404) {
        window.alert('The ' + whichAddress + '-to address could not be found.  customeraddressid: ' + addressId);
      } else {
        var errorMessage = 'Could not pull ' + whichAddress + '-to address info. Error ' + req.status;
        if (JSON.parse(req.responseText).error) {
          errorMessage += ': ' + JSON.parse(req.responseText).error.message;
        }
        window.alert(errorMessage);
      }
      next(address);
    }
  };
  req.open('GET', url, true);
  req.send();
}

function updateAddressFields (formContext, shipToAddress, billToAddress) {
  if (shipToAddress) {
    var shipToShippingAcct = shipToAddress.fgs_shippingaccount || '';
    var shipToName = shipToAddress.fgs_shiptoname || '';
    var shipToLabel = shipToAddress.name || '';

formContext.ui.setFormNotification('shiptoaddressid.', shipToName);

    setValue(formContext, 'fgs_shiptoshippingacct', shipToShippingAcct);
    setValue(formContext, 'fgs_shiptoname', shipToName);
    setValue(formContext, 'fgs_shiptolabel', shipToLabel);
  }
  if (billToAddress) {
    var billToLabel = billToAddress.name || '';
    setValue(formContext, 'fgs_billtolabel', billToLabel);
  }
}

function setValue (formContext, field, value) {
  window.alert("Inside of setValue:value->"+ value + " Field->" + field);
  formContext.getAttribute(field).setSubmitMode('always');
  formContext.getAttribute(field).setValue(value);
}


Viewing all articles
Browse latest Browse all 82002

Latest Images

Trending Articles



Latest Images