+ Start a Discussion
kumar tanwarkumar tanwar 

can we update opportunity stage picklist value on pageload?

Ajay Nagar 7Ajay Nagar 7
Yes you can update opportunity stage picklist value on pageload using Action attribute of page tag call function from apex which will update.Note you can not perform this in constructor.
kumar tanwarkumar tanwar
@Ajay can you give me any code for update opportunity stage picklist value?
Ajay Nagar 7Ajay Nagar 7
<apex:page  controller="Changestatus"   action="{!updatestatus}" >
=================================================================
 //function in Changestatus class
  
  public void updatestatus(){
     Id opportunityid;
    if(Apexpages.currentPage().getParameters().containsKey('opportunityid'))
       opportunityid= Apexpages.currentPage().getParameters().get('opportunityid');
    if(opportunityid!=null){
	      Opportunity opp=[Select Id,stage from opportunity where Id=:opportunityid];
	       if(opp!=null){
		       opp.stage='New status';
		       update opp;
	       }
	}
  }
kumar tanwarkumar tanwar
@Ajay This code update Opportunity record  Stage value, but my requirment is update the Stage picklist value. I replace Stage picklis value with custom value.
Ajay Nagar 7Ajay Nagar 7
Schema.DescribeFieldResult fieldResult = Opportunity.stage.getDescribe();
List<Schema.PicklistEntry> picklistEntries = fieldResult.getPicklistValues();
for(Schema.PicklistEntry pi: picklistEntries){
   if(pi.getValue()=='selected value')
   { pi.getValue()='updated value';
   }
 }
update picklistEntries;

Try this code.
kumar tanwarkumar tanwar
@ Ajay When i run this code am getting error "Expression cannot be assigned"
Ajay Nagar 7Ajay Nagar 7
Hi kumar tanwar,

This error is comming in line no 5 , i try to find out a way to assign this value but did not find any function or code to do so.As it is an ordered list you can try assigning this value using index of list in line no 5.

Thanks
Ajay