+ Start a Discussion
GYAN ANDRUSGYAN ANDRUS 

I am having the vf page for editing the case,my requirement is, Who ever creating a case only should edit the case,other user should not edit the case,please thelp me with code

Anupam RastogiAnupam Rastogi
Hi Hema,

You can disable the Edit button if the logged in user is not the creator using the disabled attribute of <apex:commandButton> component. I have done similarly in my project. Here is the line of code - 
 
<apex:commandButton action="{! edit }" value="Edit" disabled="{! IF(Case.CreatedById == $User.Id, True, False)}" />

Thanks
AR

If the reply helps solve your problem then please mark it as best answer.
GYAN ANDRUSGYAN ANDRUS
Thanks Anupam,
   But the edit button is in standard default layout, we are using the salesforce default page for viewing the case
Himanshu ParasharHimanshu Parashar
Hi Hema,

You can check the following condition in your vf page controller constructor
 
if(Case.CreatedById!=Userinfo.getuserid())
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Unauthorized access');
ApexPages.addMessage(myMsg);

}


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
 
GYAN ANDRUSGYAN ANDRUS
Thanks,I want to add in save function where to add it
public PageReference MySave() {
        
        if(newCase.ContactID!=null){
            TRY{
                Contact contactUpdate= New Contact();
                contactUpdate= [Select Id, FirstName, LastName, AccountId, Email,Email__c,MobilePhone, HomePhone ,Business_Phone__c, Postal_Address__c, Postal_Address_Suburb__c, Postal_Address_State__c, Postal_Address_Postcode__c, Postal_Address_Country__c,Business_Address__c,Business_Suburb__c,Business_State__c,Business_Postcode__c,Business_Country__c,Residential_Address__c,Residential_Address_Suburb__c,Residential_Address_State__c,Residential_Address_Postcode__c,Residential_Address_Country__c,
                Survey_Opt_Out__c, HasOptedOutOfEmail,Dead_Mail__c,DoNotCall,Do_Not_Contact__c,Deceased__c from Contact where Id =:newCase.ContactId];
                contactUpdate.Email__c= newCase.Email__c;
                contactUpdate.MobilePhone= newCase.Mobile__c;
                contactUpdate.HomePhone= newCase.Home_Phone__c;
       
              Update contactUpdate;

            }Catch(exception e){}   
        }
        try{
        Update newCase;
         PageReference pr= New PageReference('/'+newCase.id);
        
        return pr;  
        }
        
     catch(System.DmlException e) {
     // to show trigger  error messages
 //ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, '');
 //ApexPages.addmessage(myMsg);
 return null;
   


}

 
U JayU Jay
hi Hema,

   This may help you.




public PageReference MySave() {
        if(newCase.CreatedById!=Userinfo.getuserid())
    {
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Unauthorized access');
        ApexPages.addMessage(myMsg);
        return null;
    }
        else {
        if(newCase.ContactID!=null){
            TRY{
                Contact contactUpdate= New Contact();
                contactUpdate= [Select Id, FirstName, LastName, AccountId, Email,Email__c,MobilePhone, HomePhone ,Business_Phone__c, Postal_Address__c, Postal_Address_Suburb__c, Postal_Address_State__c, Postal_Address_Postcode__c, Postal_Address_Country__c,Business_Address__c,Business_Suburb__c,Business_State__c,Business_Postcode__c,Business_Country__c,Residential_Address__c,Residential_Address_Suburb__c,Residential_Address_State__c,Residential_Address_Postcode__c,Residential_Address_Country__c,
                Survey_Opt_Out__c, HasOptedOutOfEmail,Dead_Mail__c,DoNotCall,Do_Not_Contact__c,Deceased__c from Contact where Id =:newCase.ContactId];
                contactUpdate.Email__c= newCase.Email__c;
                contactUpdate.MobilePhone= newCase.Mobile__c;
                contactUpdate.HomePhone= newCase.Home_Phone__c;
       
              Update contactUpdate;

            }Catch(exception e){}   
        }
        try{
        Update newCase;
         PageReference pr= New PageReference('/'+newCase.id);
        
        return pr;  
        }
        
     catch(System.DmlException e) {
     return null;
    }
   
    }
}