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
Andrew Hoban 6Andrew Hoban 6 

Custom button to change fields

Hi all,

Is there a way I can create a button that once clicked, will update a picklist field called Stage__c to the value 'Recieved' and a date/time field called Recieved_Date__c to the the exact date and time the button was pressed? 

I think this can be done through Javascript but help would be apreciated.

Thanks
Himanshu ParasharHimanshu Parashar
Hi Andrew,

Where you need that button. on record standard detail page or on custom vf page.

Thanks,
Himanshu
Andrew Hoban 6Andrew Hoban 6
Hi Himanshu, its just as a detail page button.

Thanks
Himanshu ParasharHimanshu Parashar
Hi Andrew,

Yes you can do it using a custom button the.

Apex Class:
global class MyClass
{
    webservice static void myMethod() // you can pass parameters
    { 
         // Do something
    }
}



Javascript button code:
 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")}

sforce.apex.execute("MyClass","myMethod",{acc_id:"{!fieldapiname}"});
location.reload(true);

Let me know if it helps you.

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
NaveenReddyNaveenReddy
You need to use Controller Extention to achieve this. Please try below code.
 
<apex:page standardController="Opportunity" extensions="AutoFillExten"
	docType="html-5.0">
	<apex:form>

		<apex:commandButton action="{!action1}" value="AutoFill"
			reRender="pgeBlk" />

		<apex:pageBlock id="pgeBlk">
			<apex:pageblocksection id="pgeBlkSec">

				<apex:selectList value="{!item}" multiselect="false" size="1">
					<apex:selectOptions value="{!items}" />
				</apex:selectList>
				<p />


			</apex:pageblocksection>
		</apex:pageBlock>
	</apex:form>
</apex:page>
 
public class AutoFillExten {

	private final Opportunity opp;
	public String item;
	public Date date1;

	public AutoFillExten(ApexPages.StandardController controller) {
		this.opp=(Opportunity)controller.getRecord();
	}

	public List<SelectOption> getItems() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('Prospecting','Prospecting'));
		options.add(new SelectOption('Qualification','Qualification'));
		options.add(new SelectOption('Recieved','Recieved'));

		return options;
	}
	public String getitem()
	{
		return item;
	}
	public void setitem(String item)
	{
		this.item=item;
	}



	public void action1()
	{
		this.item='Recieved';
	}

}

Thanks,
Naveen.