You need to sign in to do that
Don't have an account?
Tanuja 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.
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.
Can you try the below:
All Answers
{!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);
}
}
Can you try the below: