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
PriscillaO_PriscillaO_ 

button to update picklist field from one object to another object

Hi, 
I'd like to create a button/checkbox that will update three picklist fields and one text field in the Opportunity record. These values would be coming from the Account record.
I'd like it to have the same functionality as a checkbox for "same as bill-to address" when processing a payment...

Field update logic:
Opportunity_Application__c  UPDATED FROM account.application__c   (PICKLIST)

Opportunity_Industry_Field__c UPDATED FROM account.industry_field__c   (PICKLIST)

Opportunity_Business_Type__c UPDATED FROM account.business_type__c    (PICKLIST)

Opportunity_Materials__c UPDATED FROM  account.materials__c   (TEXT AREA (255))

Thank you!!!
 
Tapas-TripathiTapas-Tripathi
Hi Priscilla,
You can have an <apex:actionSupport> along with that checkbox so that action attribute of the actionSupport tag can call the desired apex class where this logic needs to  be written to copy fields from account to opportunity.
Please let me know if still any doubts.
thanks.
PriscillaO_PriscillaO_
Hi Tapas,
I'm sorry i'm a new Admin and I don't understand any of it. Could you please explain in details? I've only created one button so far, it was to create a new record.
Thanks for your patience.
Tapas-TripathiTapas-Tripathi
Hi Priscilla,
There are plenty number of options available to achieve this functionality,
1. Using Process Builder:
Create a checkbox field in the Oppy object and create the process builder with below steps and activate it,
Setup->Process Builder->New
User-added image
->Save->Select the Object as Opportunity and Start the process as When the record is created and edited.->save
User-added image
Select the Critera as shown above and select the checkbox field you created and click on save button.
User-added image
Then select the record and set the other values like above and select the fields to update and save and activate.
This will update the fields from account to oppy whenever checkbox is checked or unchked.

2. Using Trigger and Checkbox Field:

Create a checkbox field in the Oppy object and create the below trigger and helper class to achieve the functionality.
OppyTrigger.Trigger
trigger oppyTrigger on Contact (before insert, before update) {
	OppyTriggerHelper.updateSameBillingAddr(Trigger.new);
}
 
public class OppyTriggerHelper(){
	public static void updateSameBillingAddr(List<Opportunity> newOppyList){
		
		Map<Id,Account> accList=new Map<Id,Account>();
		Set<Id> accIdList=new set<Id>();
		try{
			if(newOppyList.size()>0){
				for(Opportunity oppyRec:newOppyList){
					if(oppyRec.chkboxfield){
						accIdList.add(oppyRec.AccountId);
					}
				}
				if(accIdList.size>0){
					accList=(Map<Id,Account>)[Select id,field1,field2,filed3 from Account where id in=:accIdList];
				}
				if(accIdList.size>0){
					for(Opportunity oppyRec:newOppyList)
					{
						if(accList.containsKey(oppyRec.AccountId){
							oppyRec.field1=accList.get(oppyRec.AccountId).filed1;
							oppyRec.field2=accList.get(oppyRec.AccountId).filed2;
							oppyRec.field3=accList.get(oppyRec.AccountId).filed3;
						}
					}
				}
			}
		}
		catch(Exception ex){
			System.debug('@@@Exception@@@'+ex.getMessage());
		}
		
	}
}

3. Custom VF Page with standard controller and controller extension/ custom vf page with custom controller.

Please let me know if either of the solution 1 or 2 solved your purpose or you want me to elaborate about the point no 3.
 
Tapas-TripathiTapas-Tripathi
Alternatively you can create detail page button which excutes below javascript to update the oppy.
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
if('{!Opportunity.AccountId}'!=undefined && '{!Opportunity.AccountId}'!=null && '{!Opportunity.AccountId}'!=''){
	var relatedAccountRecord = sforce.connection.query("SELECT Id,Filed1,field2,field3 from Account where id ='{!Opportunity.AccountId}' limit 1");
	console.log(relatedAccountRecord.getArray("records"));
	var accountRecords=relatedAccountRecord.getArray("records");
	if(accountRecords.length>0){
		var oppyRecords = [];
		var oppy = new sforce.SObject("Opportunity"); 
		oppy.id ='{!Opportunity.Id}';
		oppy.field1=accountRecords[0].field1;
		oppy.field2=accountRecords[0].field2;
		oppy.field3=accountRecords[0].field3;
		oppyRecords.push(oppy); 
		result = sforce.connection.update(oppyRecords); 
		if (result[0].getBoolean("success")) {
			  alert("Address successfully updated from Account");
					window.location.reload(); 
			
		} 
		else 
		{
		  alert('failed to update Address from Account.');
		}
	}
	else{
		alert('Account record doesn\'t exists');
	}
}
else{
	alert('No Account linked with this Opportunity');
}

It all depends on the bussiness logic you want to implement, if you bulk processing then go for Process Builder or Trigger , and if you want logic for single record processing then you can stack this option in your mind.

Thanks,
PriscillaO_PriscillaO_
Thanks Tapas I'm sorry i had not seen your reply!
 will try it out in my sandbox and will let you know. I really appreciate your help!!
PriscillaO_PriscillaO_
Hi Tapas,
I'm sorry it's been awhile. I tried the process builder and it works almost perfect with one exception: I'd like the opportunity fields to be updated before the opportunity record is saved. I'd like the fields to be updated when i check the checkbox field "Update from Account". 
Could you please help? 
Tapas-TripathiTapas-Tripathi
@PriscillaO_ Process Builder will work only when you save the record, this javascript button code will work as expected. If you have a Visualforce Page then same kind of logic needs to be there as well. Are you using VF page for this?