+ Start a Discussion
satakshisatakshi 

How to add alert message if record is already present in the object?

Heelo, 

I want functionality where if already records are present in the object then it should through error message that record are already created and does not create any record. I am writing this code on custom button. And on click of custom button reords get created. My code is as below

Controller:

public with sharing class Calender {
    public List<DTP__c> mydtpList{get;set;}
    public  MTP__c obj{get;set;}
       private ApexPages.StandardController controller;
    public Calender(ApexPages.StandardController controller)
     {
     
   this.controller = controller;
          tid = ApexPages.currentPage().getParameters().get('id');
         System.debug('***************dtp added*******************'+tid); 
 }
  public PageReference saveAndReturn()
    {
        PageReference cancel = controller.cancel();
        controller.save();
        return cancel;
     }

public Decimal dy;
public date myDate;

public  id tid{get;set;}

    public PageReference createdtp() {
     
        System.debug('***************tid tid*******************'+tid);
        obj = [Select  Id, Month__c, Year__c,  February__c, NumberOfDays__c from MTP__c where id=:tid ];
    
        dy= obj.NumberOfDays__c;
       
        if(obj.February__c !=null)      
        {
         String str=obj.February__c;
         dy=Decimal.ValueOf(str);
        }  
           else 
           {
            Decimal str= obj.NumberOfDays__c;
         dy=str;
         }
       
    
  List<DTP__c> newdtp= new List<DTP__c>();
   System.debug('***************dy size******************'+dy);
    for(Integer i = 1; i<=dy; i++)

    {
       DTP__c  dt = new DTP__c();
       dt.MTP__c=tid;
   /* myDate = date.newInstance(obj.Year__c, obj.Month__c, 1);
       Integer day = myDate.dayOfYear();
       system.assertEquals(294, day); */
     /*   myDate = date.newInstance(integer.valueOf(obj.Year__c), integer.valueOf(obj.Month__c), 1);
        Integer day = myDate.dayOfYear();
       system.assertEquals(294, day);*/
       newdtp.add(dt); 
       System.debug('****************dtp added********************'+tid);
     
      
   
    }
    insert newdtp;
    return null;
    }
   
    }


VF PAge:

<!--<apex:page standardController="MTP__c"  extensions="Calender" action="{!createdtp}">
<apex:form >
 <apex:pageBlock title="All Dtp">
 <apex:pageblockTable value="{!mydtpList}" var="item">
 <apex:column value="{!item.Name}"/> 
 <apex:column value="{!item.Date__c}"/> 
 <apex:column value="{!item.Visit_type__c}"/> 
 <apex:column value="{!item.Food_Expenses__c}"/> 
</apex:pageblockTable>
 </apex:pageBlock> 
 </apex:form>
</apex:page>-->

<apex:page standardController="MTP__c"  extensions="Calender" action="{!createdtp}"> 
<apex:form >
<html>
<style type="text/css">

.myButton {
    -moz-box-shadow: 3px -3px 5px -2px #899599;
    -webkit-box-shadow: 3px -3px 5px -2px #899599;
    box-shadow: 3px -3px 5px -2px #899599;
    background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #ededed), color-stop(1, #bab1ba));
    background:-moz-linear-gradient(top, #ededed 5%, #bab1ba 100%);
    background:-webkit-linear-gradient(top, #ededed 5%, #bab1ba 100%);
    background:-o-linear-gradient(top, #ededed 5%, #bab1ba 100%);
    background:-ms-linear-gradient(top, #ededed 5%, #bab1ba 100%);
    background:linear-gradient(to bottom, #ededed 5%, #bab1ba 100%);
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ededed', endColorstr='#bab1ba',GradientType=0);
    background-color:#ededed !Important;
    -moz-border-radius:24px !Important;
    -webkit-border-radius:24px !Important;
    border-radius:24px !Important;
    border:2px solid #d6bcd6 !Important;
    display:inline-block !Important;
    cursor:pointer !Important;
    color:#3a8a9e !Important;
    font-family:Arial !Important;
    font-size:18px !Important;
    font-weight:bold !Important;
    padding:2px 36px !Important;
    text-decoration:none !Important;
    text-shadow:0px 0px 0px #e1e2ed !Important;
    margin: 10px 10px 10px 10px !Important;


}
.myButton:hover {
    background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #bab1ba), color-stop(1, #ededed));
    background:-moz-linear-gradient(top, #bab1ba 5%, #ededed 100%);
    background:-webkit-linear-gradient(top, #bab1ba 5%, #ededed 100%);
    background:-o-linear-gradient(top, #bab1ba 5%, #ededed 100%);
    background:-ms-linear-gradient(top, #bab1ba 5%, #ededed 100%);
    background:linear-gradient(to bottom, #bab1ba 5%, #ededed 100%);
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#bab1ba', endColorstr='#ededed',GradientType=0);
    background-color:#bab1ba;
}
.myButton:active {
    position:relative;
    top:1px;
}
</style>
<center>
<h1 style="font-size: 300%;"> Records Created Successfully </h1>
<br />
<br />
<br />

<apex:commandButton styleClass="myButton" action="{!cancel}" value="Close"/>
    <!--<apex:pageBlock title="All Dtp"> 
     <apex:pageblockTable value="{!mydtpList}" var="item">
            <apex:column > 
                <apex:facet name="header"> Name</apex:facet> 
                <apex:outputText value="{!item.Name}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Visit Date</apex:facet> 
                <apex:outputText value="{!item.Date__c}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Visit Type</apex:facet> 
                <apex:outputText value="{!item.Visit_type__c}"/> 
            </apex:column> 
              <apex:column > 
                <apex:facet name="header">Food Expenses</apex:facet> 
                <apex:outputText value="{!item.Food_Expenses__c}"/> 
            </apex:column> 
        </apex:pageBlockTable> 
    </apex:pageBlock>--> 
 </center>
 </html>
     </apex:form>
</apex:page>
NagendraNagendra (Salesforce Developers) 
Hi Satakshi,

Please check the below thread from the stack overflow community which might help you to accelerate with the above requirement. Kindly mark this post as solved if the information help's so that it gets removed from the unanswered queue and becomes a proper solution which results in helping others who are really in need of it.

Best Regards,
Nagendra.P