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
nitin sharma 366nitin sharma 366 

Error when an opportunity trigger code written in a class.

Hi All,
I am only trying to update a check box on an Opp object when stagename is changed to closed won but i get the following error

Compile Error: Variable does not exist: stagename at line 10 column 32

I want to write most of the code in the class not in the trigger .So please tell me how can i write this code in the class.



public class UpdateOppCheckBox
{
public static void UpdateOpp(List<opportunity>listOpps)
{
//list<opportunity>updte=new list<opportunity>();

for(opportunity opp1:ListOpps)
{

if(trigger.oldmap.get(opp1.id).stagename!=trigger.newmap.get(opp1.id).stagename && opp1.stagename=='closed won')
{
opp1.Update_Checkbox__c=true;
}
}

}



 
Sunil RathoreSunil Rathore
Hi Nitin,

Greetings to you!

Please use the below code:
Apex Trigger: 
trigger OpportunityTrigger on Opportunity(before update) {
	
	UpdateOppCheckBox.UpdateOpp(trigger.oldMap , trigger.newMap);
}
Apex Class:
public class UpdateOppCheckBox{
	public static void UpdateOpp(Map<Id,opportunity>mapOldOpp, Map<Id,opportunity>mapNewOpp){
	//list<opportunity>updte=new list<opportunity>();

		for(opportunity opp1:mapNewOpp.values()){
			if(opp1.stagename != mapOldOpp.get(opp1.id).stagename && opp1.stagename == 'Closed Won'){
				opp1.Update_Checkbox__c=true;
			}
		}
	}
}
Hope this will help you. If does then mark it as the best answer so it will also help others in the future.

Many Thanks,
Sunil Rathore