+ Start a Discussion
Cristian WhiteCristian White 

Error in my code

Hi, anybody can help me why I have an error in my code at line 5
public class ContactController {

    
    Profile PROFILE = [SELECT Id, Name FROM Profile WHERE Id=:userinfo.getProfileId()];
   
    if(Profile.ID == '00e1i000000p80k') {
        u.addError('You are not allowed to create contact from this button');
    }
    Else {
        public PageReference saveNew() {
    
   		 try { 
    	    insert Contact; 
   			 } catch(System.DMLException e) {
       		  ApexPages.addMessages(e);
              return null;
   			 }   

   		 return (new ApexPages.StandardController(new Contact())).edit();        

   		}
    }
  
}

On the line: if(Profile.ID = '001i00000p80k')  I have this error
Expecting '}' but was: 'if'

 
MagulanDuraipandianMagulanDuraipandian
Method declaration is incorrect.

Try the below

public class ContactController {

    
    public PageReference saveNew() {
        Profile PROFILE = [SELECT Id, Name FROM Profile WHERE Id=:userinfo.getProfileId()];
       
        if(Profile.ID == '00e1i000000p80k') {
            //u.addError('You are not allowed to create contact from this button');
            return null;
        }
        Else {
        
             try { 
                //insert Contact; 
                 } catch(System.DMLException e) {
                  ApexPages.addMessages(e);
                  return null;
                 }   
    
             return (new ApexPages.StandardController(new Contact())).edit();  
        }      

    }
  
}
--
Magulan Duraipandian
www.infallibletechie.com
Cristian WhiteCristian White

Thanks it works. 

I have one more question... it is not related to the question before, maybe you can help me :)

Do you know how can I override the 'New' button from Contacts with a visualforce page? I don't want all the users to have access to this button only certains profiles. For example users with profile == 'ServiceMAX' are not allow to create contacts using this button, only users with profile == 'Sales'. So technically what I need is to maintain the standard functionality of the button but if the profile =='ServiceMax' then an error message should display: "You are not allow to create contacts using this button".

Ashima nidhiAshima nidhi
For button and other restriction cant you go through record types with page layout assignment. As that would be the best option in solving your purpose.
Also for the code added above you are using the method incorrectly , Requesy you to use developer manual/apex code before going through it

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.