web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

JavaScript to lookup address and update text field on order form

(0) ShareShare
ReportReport
Posted on by 2

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);
}

*This post is locked for comments

I have the same question (0)
  • Rashid2002 Profile Picture
    20 on at

    Hello there! I am trying to solve a similar issue. I want to obtain the Bill to and Ship to Address ID, however, I always receive a null value. (Bill and Ship to addresses are populated using the Look Up Address Button).

    Any idea why that may be the case?

  • wperkinson3 Profile Picture
    2 on at

    I submitted a ticket to Microsoft and finally escalated it enough to find out it was a bug.

    After about six months, finally had an update from Microsoft where it started working again.

    Did it stop working for you recently?  I may need to go check to make sure it is not broken again.

    Regards,

    Will

  • Rashid2002 Profile Picture
    20 on at

    It doesn't work for me, no. Just to clarify. I used Xrm.WebApi.retrieveRecords("quote", quoteID, "?$select=billto_addressid").then(

        function success(result) {

           var billto_addressid = result["billto_addressid];  // Returns null

        },

    );

    Tried  ' formContext.getAttribute("billto_addressid").getValue(); '  Received a null here as well.

    Is it not working for you as well, or is there something wrong with the code?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans