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
Che SFDCChe SFDC 

Help with errors on extension

Dear All, I`m new to Apex and I`m stuck with this issue. I have a standardcontroller and below extension. What I want to do is override the standard Save button with save() method so that it redirects me to the Opportunity view page. Save works fine but it doesnt throw any error. It directly save the page. Can someone pls help? 
Thanks!
 
public with sharing class myclass {

    String oppId;
    String ownerId;
    private Opportunity Oppty;
 
    public myclass(ApexPages.StandardController stdController) {
        oppId = stdController.getId();
        Oppty = (Opportunity) stdController.getRecord();
        ownerId = oppty.OwnerId;
        oppty.StageName = 'Identified Opportunity';
         oppty.Probability = 0;
    }



public transient Map<String, Decimal> probabilityStageNameMap;

public PageReference changeStageName() {

if (probabilityStageNameMap == null) {
 probabilityStageNameMap = new Map<String, Decimal>();
for (OpportunityStage oppStage : [Select MasterLabel, DefaultProbability
                                    From OpportunityStage]) {
 probabilityStageNameMap.put(oppStage.MasterLabel, oppStage.DefaultProbability);
   }
  }

 if (probabilityStageNameMap.containsKey(Oppty.StageName)) {
   Oppty.Probability = probabilityStageNameMap.get(Oppty.StageName);
 
 }

  return null;
 }
 
  public PageReference doSave()
  {
  
      if (Oppty.LeadSource == 'Marketing Campaign' && Oppty.CampaignId == null )
      {
        system.debug('I m in'); 
        oppty.addError('Please select the Marketing Campaign which influenced this opportunity.');
          //ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'Error !!!'));    
      }
      
      if(oppty.Program_Length_in_months__c == 0){
          oppty.addError('The Contract Length Cant be "0".');
      }
      
      if(oppty.StageName != 'Awarded Business' &&  oppty.Probability > 99){
          oppty.addError('Probability of pipeline Opportunity cannot be 100%');
      }
      
      if(oppty.IsClosed != True  &&  oppty.Probability == 0){
          oppty.addError('The Probability of an Active opportunity cant be 0%');
      }
      
      
     upsert oppty;
     pagereference page=new pagereference('/apex/TPDynamicOppNEWLayout?id=' + oppty.id);
      return null;
  }
  
     public PageReference Save()
  {
  
      if (Oppty.LeadSource == 'Marketing Campaign' && Oppty.CampaignId == null )
      {
        system.debug('I m in'); 
        oppty.addError('Please select the Marketing Campaign which influenced this opportunity.');
          //ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'Error !!!'));    
      }
      
      if(oppty.Program_Length_in_months__c == 0){
          oppty.addError('The Contract Length Cant be "0".');
      }
      
      if(oppty.StageName != 'Awarded Business' &&  oppty.Probability > 99){
          oppty.addError('Probability of pipeline Opportunity cannot be 100%');
      }
      
      if(oppty.IsClosed != True  &&  oppty.Probability == 0){
          oppty.addError('The Probability of an Active opportunity cant be 0%');
      }
      
      
     upsert oppty;
     pagereference page2 =new pagereference('/' + oppty.id);
      return page2 ;
  }
  
}

 
Best Answer chosen by Che SFDC
Maxim NikitinMaxim Nikitin
I'm not shure, but you can try add bellow condition (stdController.getRecord() != null) in your constructor:
  
public myclass(ApexPages.StandardController stdController) {
       if (stdController.getRecord() != null) {
            oppId = stdController.getId();
            Oppty = (Opportunity) stdController.getRecord();
            ownerId = oppty.OwnerId;
           oppty.StageName = 'Identified Opportunity';
           oppty.Probability = 0;
       }
}

 

All Answers

Maxim NikitinMaxim Nikitin
Hi Chetan,

Try to use below code instead of "insert oppty":
Database.SaveResult insertResult = Database.insert(License, false);​
if (!insertResult.isSuccess()) {
    String errorMessage = '';             
    for(Database.Error err : insertResult.getErrors()) {
        errorMessage += (err.getMessage() + '  ');
    }
              
ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, errorMessage);
ApexPages.addMessage(errMsg);  
                
    return null;     
}

Also your page should has  <apex:pageMessages />  tag to display error.     

Cheers,
Max
Che SFDCChe SFDC
Hi Maxim, I tried and get this error - Error: Compile Error: Variable does not exist: License at line 60 column 64. 
 
public with sharing class ExampleTP {

    String oppId;
    String ownerId;
    private Opportunity Oppty;
 
    public ExampleTP (ApexPages.StandardController stdController) {
        oppId = stdController.getId();
        Oppty = (Opportunity) stdController.getRecord();
        ownerId = oppty.OwnerId;
        oppty.StageName = 'Identified Opportunity';
         oppty.Probability = 0;
    }



public transient Map<String, Decimal> probabilityStageNameMap;

public PageReference changeStageName() {

if (probabilityStageNameMap == null) {
 probabilityStageNameMap = new Map<String, Decimal>();
for (OpportunityStage oppStage : [Select MasterLabel, DefaultProbability
                                    From OpportunityStage]) {
 probabilityStageNameMap.put(oppStage.MasterLabel, oppStage.DefaultProbability);
   }
  }

 if (probabilityStageNameMap.containsKey(Oppty.StageName)) {
   Oppty.Probability = probabilityStageNameMap.get(Oppty.StageName);
 
 }

  return null;
 }
 
  public PageReference doSave()
  {
  
      if (Oppty.LeadSource == 'Marketing Campaign' && Oppty.CampaignId == null )
      {
        system.debug('I m in'); 
        oppty.addError('Please select the Marketing Campaign which influenced this opportunity.');
          //ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'Error !!!'));    
      }
      
      if(oppty.Program_Length_in_months__c == 0){
          oppty.addError('The Contract Length Cant be "0".');
      }
      
      if(oppty.StageName != 'Awarded Business' &&  oppty.Probability > 99){
          oppty.addError('Probability of pipeline Opportunity cannot be 100%');
      }
      
      if(oppty.IsClosed != True  &&  oppty.Probability == 0){
          oppty.addError('The Probability of an Active opportunity cant be 0%');
      }
      
      
            Database.SaveResult insertResult = Database.insert(License, false);
                if (!insertResult.isSuccess()) {
        String errorMessage = '';             
        for(Database.Error err : insertResult.getErrors()) {
            errorMessage += (err.getMessage() + '  ');
            }
                      
            ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, errorMessage);
            ApexPages.addMessage(errMsg);  
                
    return null;     
  }
  


 
}
}





 
Maxim NikitinMaxim Nikitin

Chetan, "License" - it's just for example. Try "oppty".
Che SFDCChe SFDC
Done, it now throws error which is great. One last question, I now get an error "Error:cannot specify Id in an insert call  ". How can I resolve this? 
Maxim NikitinMaxim Nikitin
I'm not shure, but you can try add bellow condition (stdController.getRecord() != null) in your constructor:
  
public myclass(ApexPages.StandardController stdController) {
       if (stdController.getRecord() != null) {
            oppId = stdController.getId();
            Oppty = (Opportunity) stdController.getRecord();
            ownerId = oppty.OwnerId;
           oppty.StageName = 'Identified Opportunity';
           oppty.Probability = 0;
       }
}

 
This was selected as the best answer
Che SFDCChe SFDC
Looks like its working now. Thank you so much Maxim. Have a good weekend !