function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tanuja JTanuja J 

Update Contact Address with Account address when a link is pressed

Hi Guys,
Requirement: Update Contact Mailing address with Account Address when a link is clicked in contacts pagelayout
What I have done: I have created a link in contact (when clicked it update the mailing address of contacts with accounts Updated address).
                             Execute Javascript with the code

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

//alert('{!Contact.Id}{!Account.BillingCity}{!Account.BillingCountry}');

callback = sforce.apex.execute("MyWebService","myMethod",{billingcity:'{!Account.BillingCity}'},{billingCountry:'{!Account.BillingCountry}'},{billingState:'{!Account.BillingState}'},{contactId:'{!Contact.Id}'});

if(callback== 'OK'){
window.location.reload();
}
else{

alert('Error:' + callback);
}

And Have written an Apex code :

global class MyWebService {
    
/*    webservice static void myMethod(String billingcity, String billingCountry, String billingState,Id contactId ){
        Contact con = new Contact(Id=contactId,
                                  mailingCity=billingcity,
                                  mailingCountry=billingCountry,
                                  mailingState =billingState);
        update con;
    }*/
    
  //  webservice static void updateContact(Id contactId ){
    webservice static void myMethod(Id contactId ){
        List<Contact> conList = [select id, Account.billingcity,
                                   Account.billingCountry,Account.billingState
                                   From Contact Where id=:contactId ];
        Contact con = new Contact(Id=contactId,
                                  mailingCity=conList[0].Account.billingcity,
                                  mailingCountry=conList[0].Account.billingCountry,
                                  mailingState =conList[0].Account.billingState);
        update con;
    }

}

It gives errors and not updating the address.
Please help.Though the code looks small I am unable to debug where I am going wrong.

Error message is:OnSuccess not defined in the callBack.
 
Please help 
Doubt:Cant I use synchronoue or asynchronous ajax tool method..

I have also tried 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

// identify the record 
//var o = new sforce.SObject("Contact"); 
//var a= new sforce.SObject("Account"); 

var result = sforce.connection.query("SELECT "c.accountId " + "FROM Contact c, c.account a"); 



// make the field change 
c.mailingCity=c.account.billingcity; 
c.mailingCountry=c.account.billingCountry; 
c.mailingState =c.account.billingState; 


// save the change 
sforce.connection.update([c]); 

//refresh the page 
//window.location.reload();
Error for this code is :Unexpected token illegal.
 
Best Answer chosen by Tanuja J
Chandra Sekhar CH N VChandra Sekhar CH N V
Hi,

Can you try the below:
 
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")} 
var conid = '{!Contact.Id}'; 
var message = ""; 
var t = confirm("Do you wish to update address...?"); 
window.alert('method called'); 
if (t == true) { 
try{ 
message = sforce.apex.execute("MyWebService","myMethod",{contactId:conid}); 
window.alert('method called1'); 
}catch(err){ 
message += "Error description: " + err.description + "\n\n"; 
} 
window.alert(message); 
document.location.reload(true); 
}

 

All Answers

Josip Juric87Josip Juric87
This should do it:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

var result = sforce.connection.query(
        "SELECT billingcity, billingcountry, billingstate " +
        "FROM Account Where Id IN (SELECT AccountId FROM Contact WHERE Id =  '{!Contact.Id}')");
var accs = result.getArray("records");

if (!accs || !accs.length) {
   alert("No Account found");
} else {
   var acc = accs[0];
   var ctc = new sforce.SObject("Contact");
   ctc.Id = "{!Contact.Id}";
   ctc.mailingCity = acc.billingcity;
   ctc.mailingCountry = acc.billingCountry;
   ctc.mailingState = acc.billingState;

   var updateResult = sforce.connection.update([ctc]);
   if(updateResult[0].success === "true"){
      window.location.reload();
   } else {
      alert("Error: " + updateResult[0].errors.message);
   }
}
Chandra Sekhar CH N VChandra Sekhar CH N V
Hi,

Can you try the below:
 
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")} 
var conid = '{!Contact.Id}'; 
var message = ""; 
var t = confirm("Do you wish to update address...?"); 
window.alert('method called'); 
if (t == true) { 
try{ 
message = sforce.apex.execute("MyWebService","myMethod",{contactId:conid}); 
window.alert('method called1'); 
}catch(err){ 
message += "Error description: " + err.description + "\n\n"; 
} 
window.alert(message); 
document.location.reload(true); 
}

 
This was selected as the best answer