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
Vishnu7700Vishnu7700 

Need urgent help on opportunity triggers

Hi,

Can any one pls provide idea how to achive this triggers it will be greate help for me.

1.Write a trigger to check only one opportunity is in open status of all the opportunity

2.Write a trigger once opportunity is closed only Admin can delete or  update the opportunity

 

Waiting for nay help pls.

 

Best Answer chosen by Admin (Salesforce Developers) 
Yoganand GadekarYoganand Gadekar

for backward stage prevention use trigger.old map .. updated code as below.... here oty cannot move back from closed to open stage.. modify stage names as per your need

 

trigger mytrigger on opportunity (before insert,before update,before delete){

List<string> OpenStages = New List<string>();
Set<string> ClosedStages = New set<string>();
/*Add your open stages in this list*/
OpenStages.add('Prospecting');
OpenStages.add('Qualification');

/*Add your closed stages in this set*/
ClosedStages.add('Closed Won');
ClosedStages.add('Closed Lost');

  
List<Opportunity> opty = [Select id from opportunity where StageName In:OpenStages limit 2];

string userprofile =[select name from profile where id =: UserInfo.getProfileId()].name;

  for(Opportunity o:trigger.new){
       If(trigger.isinsert || trigger.isupdate){
       

   if(trigger.isupdate){
             If(ClosedStages.contains(system.Trigger.oldMap.get(o.Id).stagename) && OpenStages.contains(o.stagename);
                o.adderror('stage cannot move back from closed to open');
              }
     

     If(Opty.size()>1){
             if(ClosedStages.contains(o.stagename))   
             o.adderror('Already one opty with open status present');
          }
       }
          if(trigger.isupdate || trigger.isdelete){
             if(ClosedStages.contains(o.stagename) && userprofile != 'system administrator')
                   o.adderror('Only admin can update opty');
          }  
   }
}

All Answers

Dhaval PanchalDhaval Panchal

I am not able to understand your first requirement.

 

For the second requirement create a before delete trigger. And check profile of login user (UserInfo.getProfileId) and query on profile object to get profile name. If user is not an system administrator then generate an error using "addError" method.

Vishnu7700Vishnu7700

First requriemnet is at a time only one opportunity is in open state.

Example in org if 100 opportunity are there out of those 100 only 1 should in open status if user tried to open an another opportinuty system should trows error.

Vishnu7700Vishnu7700

Please provide any sample code it will be great helpfull for me.

Yoganand GadekarYoganand Gadekar

Do you want to check or  vallidate theat there are more than one opportunies wth open status?

 All the opportunities? do you want to check all the oppotunities in your org? if yes the do like below..

Following will validate and show error messege ..

 

trigger mytrigger on opportunity (before insert,before update,before delete){

List<Opportunity> opty = [Select id from opportunity where status =:'open' limit 2];

string userprofile =[select name from profile where id =: UserInfo.getProfileId()].name;

 

   for(Opportunity o:trigger.new){

       If(trigger.isinsert || trigger.isupdate){

          If(Opty.size()>1)

             o.adderror('Already one opty with open status present');

       }

          if(trigger.isupdate || trigger.isdelete){

             if(o.status == 'Closed' && userprofile != 'system administrator')

                   o.adderror('Only admin can update opty');

          }

    

}

}

Vishnu7700Vishnu7700

Thanks...

For second scenario i had written a code can u please check it and correct it neccassary...

trigger Deleteopponlyadmin on Opportunity (before delete) {
    List<Profile> listprofile = [Select Name,UserType from Profile];
    for(opportunity opp : Trigger.new){
        if(opp.StageName != 'Close'){
        //listprofile.Name  = 'System Administrator';
        }
        delete opp;
    }

}

Vishnu7700Vishnu7700

Got it...

Can you please give idea for this scenario

trigger on opportunity stage cannot be move backward i.e. Stage 1,2,3,4 once opportunity is in stage 2 it cannot be updated to stage 1

Yoganand GadekarYoganand Gadekar

trigger mytrigger on opportunity (before insert,before update,before delete){

List<string> OpenStages = New List<string>();
Set<string> ClosedStages = New set<string>();
/*Add your open stages in this list*/
OpenStages.add('Prospecting');
OpenStages.add('Qualification');

/*Add your closed stages in this set*/
ClosedStages.add('Closed Won');
ClosedStages.add('Closed Lost');

  
List<Opportunity> opty = [Select id from opportunity where StageName In:OpenStages limit 2];

string userprofile =[select name from profile where id =: UserInfo.getProfileId()].name;

  for(Opportunity o:trigger.new){
       If(trigger.isinsert || trigger.isupdate){
          If(Opty.size()>1){
             if(ClosedStages.contains(o.stagename))   
             o.adderror('Already one opty with open status present');
          }
       }
          if(trigger.isupdate || trigger.isdelete){
             if(ClosedStages.contains(o.stagename) && userprofile != 'system administrator')
                   o.adderror('Only admin can update opty');
          }  
   }
}

Yoganand GadekarYoganand Gadekar

for backward stage prevention use trigger.old map .. updated code as below.... here oty cannot move back from closed to open stage.. modify stage names as per your need

 

trigger mytrigger on opportunity (before insert,before update,before delete){

List<string> OpenStages = New List<string>();
Set<string> ClosedStages = New set<string>();
/*Add your open stages in this list*/
OpenStages.add('Prospecting');
OpenStages.add('Qualification');

/*Add your closed stages in this set*/
ClosedStages.add('Closed Won');
ClosedStages.add('Closed Lost');

  
List<Opportunity> opty = [Select id from opportunity where StageName In:OpenStages limit 2];

string userprofile =[select name from profile where id =: UserInfo.getProfileId()].name;

  for(Opportunity o:trigger.new){
       If(trigger.isinsert || trigger.isupdate){
       

   if(trigger.isupdate){
             If(ClosedStages.contains(system.Trigger.oldMap.get(o.Id).stagename) && OpenStages.contains(o.stagename);
                o.adderror('stage cannot move back from closed to open');
              }
     

     If(Opty.size()>1){
             if(ClosedStages.contains(o.stagename))   
             o.adderror('Already one opty with open status present');
          }
       }
          if(trigger.isupdate || trigger.isdelete){
             if(ClosedStages.contains(o.stagename) && userprofile != 'system administrator')
                   o.adderror('Only admin can update opty');
          }  
   }
}

This was selected as the best answer
Dhaval PanchalDhaval Panchal


if(Trigger.oldMap.get(o.Id).StageName == 'Closed' && o.StageName == 'Open'){
     o.addError('stage cannot move back from closed to open');
     return;
}

Yoganand GadekarYoganand Gadekar

Updated code

 

 

trigger mytrigger on opportunity (before insert,before update,before delete){

Set<string> OpenStages = New Set<string>();
Set<string> ClosedStages = New set<string>();
/*Add your open stages in this list*/
OpenStages.add('Prospecting');
OpenStages.add('Qualification');

/*Add your closed stages in this set*/
ClosedStages.add('Closed Won');
ClosedStages.add('Closed Lost');

  
List<Opportunity> opty = [Select id from opportunity where StageName In:OpenStages limit 2];

string userprofile =[select name from profile where id =: UserInfo.getProfileId()].name;

  for(Opportunity o:trigger.new){
       If(trigger.isinsert || trigger.isupdate){
          if(trigger.isupdate){
             If(ClosedStages.contains(system.Trigger.oldMap.get(o.Id).stagename) && OpenStages.contains(o.stagename))
                o.adderror('stage cannot move back from closed to open');
    
          }
          If(Opty.size()>1){
             if(ClosedStages.contains(o.stagename))   
             o.adderror('Already one opty with open status present');
          }
       }
          if(trigger.isupdate || trigger.isdelete){
             if(ClosedStages.contains(o.stagename) && userprofile != 'system administrator')
                   o.adderror('Only admin can update opty');
          }  
   }
}

 

Vishnu7700Vishnu7700

Thanks yoganand...

For your timely support.Assigned Kodos.