Update Contact Mailing Address with Account Billing Address on Custom Button
Requirement: On Account Detail Page is there is a Custom Button named Copy Address, When We click on this button then Billing Address of Account record is updated in all Contacts of that Account in Mailing Address.
Solution : There are 3 solution
1) First one is write the trigger and invoke at custom button
2) Call javascript code on Custom Button
3) Invoke Web service on Custom Button.
I am explaining 3rd .
Firstly Create a Apex Class:
Class Name : UpdateContactAddress
global class UpdateContactAddress {
webservice static String processDetails(String accID){
List<Account> lstacc =[select id,BillingCity,BillingCountry,BillingState from Account where ID =:accID limit 1];
system.debug('acc====='+lstacc);
List<Contact> lstofcont =[select id ,MailingCity,MailingCountry,MailingState,MailingPostalCode,MailingStreet
from contact where AccountID =: accID];
system.debug('lstofcont ====='+lstofcont);
List<Contact> Updatelstofcon = new List<Contact>();
If(lstacc!=null && lstacc.size()>0)
{
if(lstofcont!=null && lstofcont.size()>0)
{
for(Contact objCon :lstofcont )
{
// objCon .accountId = acc.Id;
system.debug('lstofcont =====dddd');
// For demo i m populating one fiield but similar you can update other fields , those field must be in query
objCon.MailingCity = lstacc[0].BillingCity;
/*objCon.MailingStreet = lstacc[0].BillingStreet;
objCon.MailingState = lstacc[0].BillingState;
objCon.MailingCountry = lstacc[0].BillingCountry;
objCon.MailingPostalCode = lstacc[0].BillingPostalCode;
objCon.MailingCity = lstacc[0].BillingCity; */
Updatelstofcon.add(objCon);
}
}
}
system.debug('Updatelstofcon=====dddd'+Updatelstofcon);
if(Updatelstofcon!=null && Updatelstofcon.size()>0)
{
update Updatelstofcon;
}
return null;
}
}
------------------------------------------------------Custom Button Code------------------------------------------------------------
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}
var url = parent.location.href;
//alert('sss');
var accountid = "{!Account.Id}";
//var billcity = "{!Account.BillingCity}";
//alert(accountid);
//alert(billcity);
var result = sforce.apex.execute("UpdateContactAddress", "processDetails",{accID: accountid});
if(result.length!=null)
{
parent.location.href = url;
}
Solution : There are 3 solution
1) First one is write the trigger and invoke at custom button
2) Call javascript code on Custom Button
3) Invoke Web service on Custom Button.
I am explaining 3rd .
Firstly Create a Apex Class:
Class Name : UpdateContactAddress
global class UpdateContactAddress {
webservice static String processDetails(String accID){
List<Account> lstacc =[select id,BillingCity,BillingCountry,BillingState from Account where ID =:accID limit 1];
system.debug('acc====='+lstacc);
List<Contact> lstofcont =[select id ,MailingCity,MailingCountry,MailingState,MailingPostalCode,MailingStreet
from contact where AccountID =: accID];
system.debug('lstofcont ====='+lstofcont);
List<Contact> Updatelstofcon = new List<Contact>();
If(lstacc!=null && lstacc.size()>0)
{
if(lstofcont!=null && lstofcont.size()>0)
{
for(Contact objCon :lstofcont )
{
// objCon .accountId = acc.Id;
system.debug('lstofcont =====dddd');
// For demo i m populating one fiield but similar you can update other fields , those field must be in query
objCon.MailingCity = lstacc[0].BillingCity;
/*objCon.MailingStreet = lstacc[0].BillingStreet;
objCon.MailingState = lstacc[0].BillingState;
objCon.MailingCountry = lstacc[0].BillingCountry;
objCon.MailingPostalCode = lstacc[0].BillingPostalCode;
objCon.MailingCity = lstacc[0].BillingCity; */
Updatelstofcon.add(objCon);
}
}
}
system.debug('Updatelstofcon=====dddd'+Updatelstofcon);
if(Updatelstofcon!=null && Updatelstofcon.size()>0)
{
update Updatelstofcon;
}
return null;
}
}
------------------------------------------------------Custom Button Code------------------------------------------------------------
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}
var url = parent.location.href;
//alert('sss');
var accountid = "{!Account.Id}";
//var billcity = "{!Account.BillingCity}";
//alert(accountid);
//alert(billcity);
var result = sforce.apex.execute("UpdateContactAddress", "processDetails",{accID: accountid});
if(result.length!=null)
{
parent.location.href = url;
}
In case of any issue feel free call to me .
Please drop a comment or like.
Thanks
Sumit Shukla
Salesforce Developer
sumitshukla.mca@gmail.com
9711055997
Thanks for a code.
ReplyDelete