• BHANU TEJA PULIPALUPULA
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi friends,

I how do i check if the record already exists or not in the database.
If the perticular record already exists then no need to insert it.
Else, insert the record.

Here is my code

Date  d = Date.Today();
List<Account> AllAccounts= new List<Account>([select id,Name,ARR__c,Relationship_Start_Date__c,
                                              (Select Id,Account.Id from Opportunities) from Account]);
for(Account a:AllAccounts)
{
    
    Integer numberDaysDue= a.Relationship_Start_Date__c.daysBetween(d);
    if(numberDaysDue<=60){
       
        Opportunity O= new Opportunity();
        o.StageName='Legal';
        o.Amount=a.ARR__c;
        o.AccountId=a.ID;
        o.Name=o.AccountId  +'Opportunity';
        o.CloseDate=Date.today();
        o.NextStep='Won';
        system.debug('Account Id'+o.AccountId);
        insert o;
      
    }
   
    
    system.debug('Diff==='+numberDaysDue);
 
}