I am close to having a jscript that works to update all of the Contacts' addresses whrn the Account address is changed. I have it set up to iterate through the CustomerAddressSet and load the Addresses associated with the GUID. Once the address loads, the script loads the ContactSet with the same parent account id, compares the Address_Names and makes the update if they are the same. It then moves thorugh the rest of the contacts and when finished it is supposed to go to the next address. However, it stops after going through all of the contacts. I think the problem has to do with loading a data.d.result within another data.d.result.
Here is my script:
var xp = Xrm.Page;
var addressCityAttribute;
var addressNameAttribute;
var addressLine1Attribute;
var addressLine2Attribute;
var addressLine3Attribute;
var addressStateAttribute;
var addressPostalCodeAttribute;
var contactFullNameAttribute;
var contactNameAttribute;
var contactIdAttribute;
function onSave(context) {
var accountId = xp.data.entity.getId();
var isDirty = xp.data.entity.getIsDirty();
if (isDirty) {
//Parse the entity object into JSON
alert("Update process started");
var changes = new Object();
changes.Address1_Name = xp.getAttribute("address1_name").getValue();
changes.Address1_Line1 = xp.getAttribute("address1_line1").getValue();
changes.Address1_Line2 = xp.getAttribute("address1_line2").getValue();
changes.Address1_Line3 = xp.getAttribute("address1_line3").getValue();
changes.Address1_City = xp.getAttribute("address1_city").getValue();
changes.Address1_StateOrProvince = xp.getAttribute("address1_stateorprovince").getValue();
changes.Address1_PostalCode = xp.getAttribute("address1_postalcode").getValue();
var updateAccountReq = new XMLHttpRequest();
updateAccountReq.open("POST", "http://testOrg/myOrg/XRMServices/2011/organizationData.svc/AccountSet(guid'" + accountId + "')", true);
updateAccountReq.setRequestHeader("Accept", "application/json");
updateAccountReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
updateAccountReq.setRequestHeader("X-HTTP-Method", "MERGE");
updateAccountReq.onreadystatechange = function () {
//updateContactReqCallBack(this, contactId);
}
updateAccountReq.send(JSON.stringify(changes));
alert("Update account complete");
}
}
function onLoad(context) {
var accountId = xp.data.entity.getId();
var addressQuery = "http://testOrg/myOrg/XRMServices/2011/organizationData.svc/CustomerAddressSet?$select=City,Line1,Line2,Line3,Name,PostalCode,StateOrProvince&$orderby=Name desc&$filter=ParentId/Id eq guid'" + accountId + "'";
getQuery(addressQuery, 1);
var contactQuery = "http://testOrg/myOrg/xrmservices/2011/OrganizationData.svc/ContactSet?$select=Address1_Line1,Address1_City,Address1_Line2,Address1_Line3,Address1_Name,Address1_PostalCode,Address1_StateOrProvince,FullName,ContactId&$orderby=Address1_Name desc&$filter=AccountId/Id eq guid'" + accountId + "'";
getQuery(contactQuery, 2);
//return;
}
function getQuery(queryName, queryId) {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: queryName,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
//alert("odata succeeded");
if (queryId == 1) {
addressUpdater(data.d.results);
}
else if (queryId == 2) {
contactUpdater(data.d.results);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert('OData Select Failed: ' + queryName);
}//end error
});
} //end get address function
function addressUpdater(ManyEntities) {
alert("addressUpdater Started");
//populate the addresses array
//for each address, look for a contact with the same address_name
//getAddress(addressQuery, "AddressQuery");
for (i = 0; i < ManyEntities.length; i++) {
var oneEntity = ManyEntities[i];
addressCityAttribute = oneEntity.City;
addressNameAttribute = oneEntity.Name;
alert(addressNameAttribute);
addressLine1Attribute = oneEntity.Line1;
addressLine2Attribute = oneEntity.Line2;
addressLine3Attribute = oneEntity.Line3;
addressStateAttribute = oneEntity.StateOrProvince;
addressPostalCodeAttribute = oneEntity.PostalCode;
//I used return; only because it iterates through al of the contacts
//however, it breaks the loop
return;
//this is what is supposed to go in here but it does not work
//getQuery(contactQuery, 2);
} //end for (addresses)
} //end function
function contactUpdater(ManyContacts) {
//addressNameAttribute = addressNameAttribute;
alert("contactUpdater Started");
//populate the addresses array
//for each address, look for a contact with the same address_name
//getAddress(addressQuery, "AddressQuery");
for (i = 0; i < ManyContacts.length; i++) {
var oneContact = ManyContacts[i];
contactFullNameAttribute = oneContact.FullName;
contactIdAttribute = oneContact.ContactId;
contactNameAttribute = oneContact.Address1_Name;
if (contactNameAttribute == addressNameAttribute) {
alert(contactFullNameAttribute);
updateContact(contactIdAttribute);
} //end if
else {
return;
}
}
return; //end for (contacts)
} //end function
function updateContact(contactId) {
//Parse the entity object into JSON
alert("Update process started");
var updateContactReq = new XMLHttpRequest();
var changes = new Object();
changes.Address1_Name = addressNameAttribute;
changes.Address1_Line1 = addressLine1Attribute;
changes.Address1_Line2 = addressLine2Attribute;
changes.Address1_Line3 = addressLine3Attribute;
changes.Address1_City = addressCityAttribute;
changes.Address1_StateOrProvince = addressStateAttribute;
changes.Address1_PostalCode = addressPostalCodeAttribute;
updateContactReq.open("POST", "http://testOrg/myOrg/XRMServices/2011/organizationData.svc/ContactSet(guid'" + contactId + "')", true);
updateContactReq.setRequestHeader("Accept", "application/json");
updateContactReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
updateContactReq.setRequestHeader("X-HTTP-Method", "MERGE");
updateContactReq.onreadystatechange = function () {
//updateContactReqCallBack(this, contactId);
}
updateContactReq.send(JSON.stringify(changes));
alert("Update process complete");
}
Why am I not using a workflow? A workflow will only update the primary contact and will only update using one address.
Any ideas of that I am doing wrong?! Thanks!
Joy