You need to sign in to do that
Don't have an account?

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!
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 ; } }
All Answers
Try to use below code instead of "insert oppty":
Also your page should has <apex:pageMessages /> tag to display error.
Cheers,
Max
Chetan, "License" - it's just for example. Try "oppty".