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
Dagny FernandesDagny Fernandes 

How to give error msg on VF page, overriding the standard button with vf and redirect based on record type

I have override the standerd Edit button with VF page this VF page will redirect to other VF page based on the record type, the redirection is working properly but i want to give an error messsage on this this page.
In the below class line number 26 error message is not diplaying if i add "return null" it is redirectiong to standerd page if i remove this line it stay in the page and showes "Attempt to De-reference null object" error. Need help ASAP

Thanks in advance

here c.Sys_Ticket_Count__c is grater than 0 

Apex Class:
public with sharing class escaEditBtnController {
	
	public escaEditBtnController(ApexPages.StandardController controller) {
        this.controller = controller;
    }
    
    public PageReference getredirect() {

        Escalation__c c = [Select id, recordtypeid,Sys_Ticket_Count__c From Escalation__c Where Id = :ApexPages.currentPage().getParameters().get('id')];

        PageReference newPage;

        if (c.recordtypeid == Constants_PicklistVariables.TICKET_RECORDTYPE_ID) {
            newPage = new PageReference('/apex/TicketEdit?retURL=%2F'+c.id);
			newPage.getParameters().put('id', c.id);
	        newPage.getParameters().put('editMode', 'true');
	        
        } else {
        	if(c.Sys_Ticket_Count__c <= 0 ){
        		newPage = new PageReference('/apex/EscalationEdit?retURL=%2F'+c.id);
	            newPage.getParameters().put('id', c.id);
	            newPage.getParameters().put('editMode', 'true');
	            newPage.getParameters().put('nooverride', '1');
        	}else{
        		System.debug('c.Sys_Ticket_Count__c'+c.Sys_Ticket_Count__c);
        		ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.Error,'Ticket has been rised for this escalation you do not have access to edit this Escalation, Please contact the system admin'));
				return null; 
        	}
            
        }
		
        return newPage.setRedirect(true);
    }

    private final ApexPages.StandardController controller;
}
VF page:
<apex:page standardController="Escalation__c" extensions="escaEditBtnController"  
    action="{!nullValue(redirect.url, urlFor($Action.Escalation__c.Edit, Escalation__c.id, null, true))}">
<apex:pageMessages />
</apex:page>


 
Best Answer chosen by Dagny Fernandes
lakslaks
Hi,

The reason that you're getting "Attempt to De-reference null object" error is because in the case where the control goes inside the if loop in line:26, the PageReference object newPage is not initialized. 
And then you are doing newPage.setRedirect(true); in line: 32 which throws a null reference error.

Hope this helps.

Regards,
Lakshmi.

All Answers

lakslaks
Hi,

The reason that you're getting "Attempt to De-reference null object" error is because in the case where the control goes inside the if loop in line:26, the PageReference object newPage is not initialized. 
And then you are doing newPage.setRedirect(true); in line: 32 which throws a null reference error.

Hope this helps.

Regards,
Lakshmi.
This was selected as the best answer
Sumitkumar_ShingaviSumitkumar_Shingavi
Your code should be
public with sharing class escaEditBtnController {
	
	public escaEditBtnController(ApexPages.StandardController controller) {
        this.controller = controller;
    }
    
    public PageReference getredirect() {

        Escalation__c c = [Select id, recordtypeid,Sys_Ticket_Count__c From Escalation__c Where Id = :ApexPages.currentPage().getParameters().get('id') LIMIT 1];

        PageReference newPage;
		
		if(c != Null) {
			
			if (c.recordtypeid == Constants_PicklistVariables.TICKET_RECORDTYPE_ID) {
				newPage = new PageReference('/apex/TicketEdit?retURL=%2F'+c.id);
				newPage.getParameters().put('id', c.id);
				newPage.getParameters().put('editMode', 'true');
				return newPage.setRedirect(true);
			} else {
				if(c.Sys_Ticket_Count__c <= 0 ){
					newPage = new PageReference('/apex/EscalationEdit?retURL=%2F'+c.id);
					newPage.getParameters().put('id', c.id);
					newPage.getParameters().put('editMode', 'true');
					newPage.getParameters().put('nooverride', '1');
					return newPage.setRedirect(true);
				}else{
					System.debug('c.Sys_Ticket_Count__c'+c.Sys_Ticket_Count__c);
					ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.Error,'Ticket has been rised for this escalation you do not have access to edit this Escalation, Please contact the system admin'));
					
				}            
			}
		}
		return null;
    }

    private final ApexPages.StandardController controller;
}
I corrected it and made it more robust. Hope this helps!

PS: Mark it as solution if this solves your problem.
Dagny FernandesDagny Fernandes
Thanks @Lakshmi it solved my problem 

Thanks @Sumitkumar_Shingavi iam getting the record but, still the code you have sent helped me in understanding the code

Thansk a ton mates