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
Amit Singh1989Amit Singh1989 

Popup message using JavaScript

Hi friends,

 

I have a Session object and Session Line Item object (They are associated with lookup relationship, where Session is master and session line item is child),

 

I am displaying List of session records in visualforce page using <apex:pageblocktable>

I am also using Edit and delete functionality using commandbutton,

 

my requirement is that "I want to restrict user to Edit any Session if any child record is associated with this" so when a user clicks on Edit button a popup should apper with message "You can not Edit this session".

 

How to acheive this functionality?

 

<apex:commandButton value="Edit" action="{!editSession}" reRender="hiddenBlock10">
                      <apex:param name="param1" value="{!Session.id}" assignTo="{!strSessionId}"></apex:param>
              </apex:commandButton>&nbsp;
 public pagereference editSession()
    {
        Pagereference p=new Pagereference('/apex/EditSession?id='+strSessionId);
        p.setRedirect(true);
        return p;
    }

 

 

 

 

Thanks,

Amit Singh

 

Best Answer chosen by Admin (Salesforce Developers) 
Amit Singh1989Amit Singh1989

I got the solution for this requirement,

here is the solution,

 

<script>
   function stopRedirect(toStopRedirect)
   {
        if(toStopRedirect) 
        alert('You can not edit this Session,Because Estimate Line Item created from this Session is approved.');
   }
  </script> 

<apex:commandButton value="Edit" action="{!editSession}" reRender="hiddenBlock10" OnComplete="stopRedirect('{!ChildExists}')">
                      <apex:param name="param1" value="{!Session.id}" assignTo="{!strSessionId}"></apex:param>
              </apex:commandButton>&nbsp;
public Boolean ChildExists{get;set;} 
public pagereference editSession()
    {
        List<session_Line_Item__c> childrecords = new List<Session_Line_Item__c>();
        childrecords = [select id,Name,Session__c from Session_Line_Items__c where Session__c =: strSessionId];
        
        ChildExists = false;
             
        if(childRecords.isEmpty())
        {  
        Pagereference p=new Pagereference('/apex/EditSession?id='+strSessionId);
        p.setRedirect(true);
        return p;
        }
        else
        {
        ChildExists = true;
        return null;
        }
    }

 

Thank you !!!