Update Adress in CRM

How to update contact address in Dynamics CRM with Javascript  when account adress parent changes?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:

We have similar question asked some time back... Have a look-

Let us know if you find any issues.
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:

On request of one of the moderators I am adding full answer in one post. Let me know if you find any issues.

You can use following script to update the address. This script contains reference of account where you can use contact instead of account.

var AccountLookUp = crmForm.all.parentcustomerid.DataValue;
if (AccountLookUp != null && AccountLookUp != "undefined") {
    var AccountID = AccountLookUp[0].id;
    var Accountxml = "" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +
" <q1:EntityName>account</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:AllColumns\" />" +
" <q1:Distinct>false</q1:Distinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>accountid</q1:AttributeName>" +
" <q1:Operator>Like</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:string\">" + AccountID + "</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </RetrieveMultiple>" +
" </soap:Body>" +
"</soap:Envelope>" + "";    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", Accountxml.length);
    var resultAccountXml = xmlHttpRequest.responseXML;
    var entityNodes = resultAccountXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");    for (var i = 0; i < entityNodes.length; i++) {
        var entityNode = entityNodes[i];
        var TelephoneNode = entityNode.selectSingleNode("q1:telephone1");
        var Telephone = (TelephoneNode == null) ? null : TelephoneNode.text;
        var Line1Node = entityNode.selectSingleNode("q1:address1_line1");
        var Line1 = (Line1Node == null) ? null : Line1Node.text;
        var CityNode = entityNode.selectSingleNode("q1:address1_city");
        var City = (CityNode == null) ? null : CityNode.text;
        var StateNode = entityNode.selectSingleNode("q1:address1_stateorprovince");
        var State = (StateNode == null) ? null : StateNode.text;
        var ZipNode = entityNode.selectSingleNode("q1:address1_postalcode");
        var Zip = (ZipNode == null) ? null : ZipNode.text;      

if (crmForm.all.telephone1.DataValue == null)
{crmForm.all.telephone1.DataValue = Telephone;}

if(crmForm.all.address1_line1.DataValue == null)
{crmForm.all.address1_line1.DataValue = Line1; }

if (crmForm.all.address1_city.DataValue == null)
{crmForm.all.address1_city.DataValue = City; }

if (crmForm.all.address1_stateorprovince.DataValue == null)
{crmForm.all.address1_stateorprovince.DataValue = State;}

if(crmForm.all.address1_postalcode.DataValue == null)
{crmForm.all.address1_postalcode.DataValue = Zip;}

Open in new window

As the script is written for Dynamics CRM 4.0 you need to convert it to CRM 2011. You can use the following tool to convert-

Option 2:
If you want to populate it without writing a single line of code you can use relationship mapping. But yes it will only work when you create the record.

Option 3:
Check this walkthrough. It uses a tool SoapLogger from CRM 2011 sdk.

You can download latest Dynamics CRM 2011 sdk from http://www.microsoft.com/en-in/download/details.aspx?id=24004

You just need to add equivalent programming code of c#/vb and you will get it converted to javascript.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
armitchouAuthor Commented:

thanks for your help.
your answer makes me in the good direction.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Dynamics

From novice to tech pro — start learning today.