• Wajeed Khan Hb
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 3
    Questions
  • 1
    Replies
My Controller

public with sharing  class Call_Controller {
    public Call__c call{get;set;}
    public Id accountId {get;set;}
    public List<Account_Affiliation__c> accAf{get;Set;}
    public List<wrapperClass> wrapClassList{get;set;}
    public wrapperClass wc{get;set;}
    public Account acc{get;set;}
    public List<Call__c> callList{get;set;}
    
    public Call_Controller(ApexPages.StandardController ctrl)
    {
        ctrl.addFields(new List<String>{'Account__c'});
        wrapClassList = new List<wrapperClass>();
        accountId =ApexPages.currentPage().getParameters().get('accId');
        accAf = new List<Account_Affiliation__c>();
        call = (Call__c)ctrl.getRecord();
        if(accountId==null)
        {
            accountId=call.Account__c;
        }
        else
        {
            call.Account__c = accountId; 
            
        }
        acc=[select ID,Name from Account where id=:accountId];
        
        accAf=[Select Status__c,To__r.name,From__c,To__c From Account_Affiliation__c 
               where From__c=:accountId and Status__c=true];
        
        for(Account_Affiliation__c af:accAf)
        {
            wc = new wrapperClass(af.To__r.Name);
            wc.flag=false;
            wc.accWrap=af;
            wrapClassList.add(wc);            
        }
    }
    
    public void clear()
    {
        call.Comments__c=null;
    }
    
    public class wrapperClass
    {
        public boolean flag{get;set;}
        public Account_Affiliation__c accWrap{get;set;}
        public String accName{get;set;}
        
        public wrapperClass(String Name)
        {
            accName=Name;
            flag=false;
        }
    }
    
    //Record Creation for Related Records on Save
    public void createRelRecordOnSave()
    {
        for(wrapperClass wc:wrapClassList)
        {
            if(wc.flag==true )
            {
                Call__c relCall= new Call__c();
                relCall.Account__c=wc.accWrap.To__c;
                relCall.Parent_Account__c=wc.accWrap.From__c;
                relCall.Out_of_office__c=call.Out_of_office__c;
                relCall.Call_Date__c=call.Call_Date__c;
                relCall.Comments__c=call.Comments__c;
                relCall.Status__c='Draft';
                upsert relCall;
            }
        }
    }
    
    //Record Creation for Related Records on Submit
    public void createRelRecordOnSubmit()
    {
        for(wrapperClass wc:wrapClassList)
        {
            if(wc.flag==true )
            {
                Call__c relCall= new Call__c();
                relCall.Account__c=wc.accWrap.To__c;
                relCall.Parent_Account__c=wc.accWrap.From__c;
                relCall.Out_of_office__c=call.Out_of_office__c;
                relCall.Call_Date__c=call.Call_Date__c;
                relCall.Comments__c=call.Comments__c;
                relCall.Status__c='Submitted';
                upsert relCall;
            }
        }
    }
    //Save Button 
    public PageReference Save()
    {   
        if(call.Call_Date__c>System.today())
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You Cannot Enter A future Date'));
            return null;
        }
        else
        {
            try{
                call.Status__c='Draft';
                upsert call;
                createRelRecordOnSave();
                
                PageReference reRend = new PageReference('/'+AccountID);
                reRend.setRedirect(true);
                return reRend;
            }
            catch(Exception ex)
            {
                ex.getMessage();
            }
        }
        return null;
        
    }
    
    public PageReference cancel()
    {
        PageReference reRend = new PageReference('/'+AccountID);
        reRend.setRedirect(true);
        return reRend;
    }
    
    //Submit Button
    public PageReference Submit()
    {      
        if(call.Call_Date__c>system.today())
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You Cannot Enter A future Date'));
        }
        try{
            createRelRecordOnSubmit();
            call.Status__c='Submitted';
            upsert call;
            
        }
        catch(Exception ex)
        {
            ApexPages.addMessages(ex);
        }
        PageReference reRend = new PageReference('/'+AccountID);
        reRend.setRedirect(true);
        return reRend;
    }  
}


My Visualforce Page



<apex:page StandardController="Call__c" extensions="Call_Controller" >
    <apex:form >
     <script type="text/javascript">
var callStatus= '{!Call__c.Status__c }';
if(callStatus =='Submitted')
{
    alert('You cannot edit submitted calls');
    window.open('/{!Call__c.id}','_top');
}
</script>
    <apex:pageMessages />
    <apex:pageBlock title="Edit Call">
    <apex:pageBlockSection columns="1" title="INFORMATION SECTION" id="pgSection" >
    
      <apex:outputField value="{!acc.Name}"/>
        
       <apex:inputField value="{!Call__c.Call_Date__c}"/>  
         <apex:inputField value="{!Call__c.Out_of_office__c}">
        <apex:actionSupport event="onchange" reRender="pgSection" action="{!clear}"/>  
         </apex:inputField>

           <apex:pageBlockSectionItem rendered="{!if(call.Out_of_office__c=='Yes',True,False)}" >
           <apex:outputLabel value="Comments"></apex:outputLabel>
           <apex:inputField value="{!Call__c.Comments__c}">

           </apex:inputField>
           </apex:pageBlockSectionItem>
           </apex:pageBlockSection>
           
           
    <apex:pageBlockSection title=" ATTENDEES SECTION" columns="2" >
    
    <apex:pageBlockTable value="{!wrapClassList}" var="af" id="afTable">
            <apex:column headerValue="Preffered">
            <apex:inputCheckbox value="{!af.flag}"/>
            </apex:column>
            
            <apex:column headerValue="Related Account">
            <apex:outputText value="{!af.accWrap.to__r.Name}"/>
            </apex:column>
            
    </apex:pageBlockTable>   
    </apex:pageBlockSection>
    
     <apex:pageblockButtons >
     <apex:commandButton value="Save" action="{!Save}" />         
     <apex:commandButton value="Submit" action="{!Submit}"/>    
     <apex:commandButton action="{!Cancel}" value="Cancel"/>
     </apex:pageblockButtons>
    </apex:pageBlock>  
    </apex:form>
</apex:page>
public with sharing  class Call_Controller {
    public apexpages.StandardController stdctrl{get;set;}
    public Call__c call{get;set;}
   // public List<Account> acc{get;set;}
    public List<Account_Affiliation__c> accAf{get;Set;}
    public List<wrapperClass> wrapClassList{get;set;}
    public wrapperClass wc{get;set;}
    
    public Call_Controller(ApexPages.StandardController ctrl)
    {
        wrapClassList = new List<wrapperClass>();
        accAf = new List<Account_Affiliation__c>();  
        call=[Select Id,Status__c,Created__c,Account__c,Account__r.Name,Call_Date__c,Comments__c,Out_of_Office__c from Call__c where Account__c=:ApexPages.currentPage().getparameters().get('accId')][0];
        accAf=[Select Status__c,To__r.name,From__c,To__c From Account_Affiliation__c where From__c=:ApexPages.currentPage().getparameters().get('accId')];
        
        for(Account_Affiliation__c af:accAf)
        {
            wc = new wrapperClass(af.To__r.Name);
            wc.flag=false;
            wc.accWrap=af;
            wrapClassList.add(wc);            
        }
        stdctrl=ctrl;
    }
    
    public class wrapperClass
    {
        public boolean flag{get;set;}
        public Account_Affiliation__c accWrap{get;set;}
        public String accName{get;set;}
        
        public wrapperClass(String Name)
        {
            accName=Name;
            flag=false;
        }
    }
    
    //Record Creation for Related Records on Save
    public void createRelRecordOnSave()
    {
        for(wrapperClass wc:wrapClassList)
        {
            if(wc.flag==true )
            {
                Call__c Rcl =new Call__c();   
                Rcl.Account__c=wc.accWrap.To__c;
                Rcl.Call_Date__c=call.Call_Date__c;
                Rcl.Comments__c=call.Comments__c;
                Rcl.Out_of_office__c=call.Out_of_office__c;
                Rcl.Status__c='Draft';
                upsert rcl;
            }
        }
    }
    
    //Record Creation for Related Records on Submit
    public void createRelRecordOnSubmit()
    {
        for(wrapperClass wc:wrapClassList)
        {
            if(wc.flag==true )
            {
                Call__c Rcl =new Call__c();   
                Rcl.Account__c=wc.accWrap.To__c;
                Rcl.Call_Date__c=call.Call_Date__c;
                Rcl.Comments__c=call.Comments__c;
                Rcl.Out_of_office__c=call.Out_of_office__c;
                Rcl.Status__c='Submitted';
                upsert rcl;
            }
        }
    }
    //Validation on EditButton 
   /** public PageReference validate()
    {
        
        PageReference pgectrl=new PageReference('/a0B?fcf');
        
        
        if(call.Status__c=='Submitted')
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,'You Cannot Edit the Submitted Record'));
            return null;
        }
        
        return null;
    }**/
    
    /** public PageReference relRecordOnSave()
{        
for(Account_Affiliation__c Af: accAf)
{   
Call__c Rcl =new Call__c();
Rcl.Account__c= Af.To__c;
Rcl.Call_Date__c=call.Call_Date__c;
Rcl.Comments__c=call.Comments__c;
Rcl.Out_of_office__c=call.Out_of_office__c;
rcl.Status__c='Draft';
upsert rcl;   
}

PageReference reRend = new PageReference('/a0B?fcf');
reRend.setRedirect(true);
return reRend;
}

public PageReference relRecordOnSubmit()
{

for(Account_Affiliation__c Af: accAf)
{
Call__c Rcl =new Call__c();
Rcl.Account__c= Af.To__c;
Rcl.Call_Date__c=call.Call_Date__c;
Rcl.Comments__c=call.Comments__c;
Rcl.Out_of_office__c=call.Out_of_office__c;
rcl.Status__c='Submitted';
upsert rcl;   
}

PageReference reRend = new PageReference('/a0B?fcf');
reRend.setRedirect(true);
return reRend;
}**/
    
    //Save Button 
    public PageReference Save()
    {   
        if(call.Call_Date__c>System.today() )
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You Cannot Enter A future Date'));
            return null;
        }
      /**  else if(call.Status__c=='Submitted')
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You Cannot Edit which is alredy Submitted'));
            return null;
        }**/
        else
        {
            try{
                
                Call__c Call1=New Call__C(); 
                call1.Account__c=call.Account__c;
                call1.Call_Date__c=call.Call_Date__c;
                call1.Comments__c=call.Comments__c;
                call1.Out_of_office__c=call.Out_of_office__c;
                call1.Status__c='Draft';
                upsert call1;
                createRelRecordOnSave();
            }
            
            catch(Exception ex)
            {
                ApexPages.addMessages(ex);
            }
        }
        PageReference reRend = new PageReference('/a0B?fcf');
        reRend.setRedirect(true);
        return reRend;
    }
    
    //Submit Button
    public PageReference Submit()
    {      
        if(call.Call_Date__c>system.today())
        {
            call.Call_Date__c.addError('The Date Should not be Future Date');
        }
        try{
            
            Call__c Call1=New Call__C(); 
            call1.Account__c=call.Account__c;
            call1.Call_Date__c=call.Call_Date__c;
            call1.Comments__c=call.Comments__c;
            call1.Out_of_office__c=call.Out_of_office__c;
            call1.Status__c='Submitted';
            upsert call1;
            createRelRecordOnSubmit();
            
        }
        catch(Exception ex)
        {
            ApexPages.addMessages(ex);
        }
        PageReference reRend = new PageReference('/a0B?fcf');
        reRend.setRedirect(true);
        return reRend;
    }  
}

 
https://ap4lc-dev-ed.my.salesforce.com/_ui/common/apex/debug/ApexCSIPage
https://ap4lc-dev-ed.my.salesforce.com/_ui/common/apex/debug/ApexCSIPage