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
Wajeed Khan HbWajeed Khan Hb 

it is creating the new record for related account on edit ,can anyone hep me how to avoid creating new record if the record is alredy present and also the Checked button should be checked if the record is present in the call object please help me

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>