Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How can I write && in Apex if statement?

Asked by Diane Carr Aug 29, 2023 918 views 1 answer
Share

About this question

I want to know the proper way to write && condition in Apex and to check whether the condition if(days Difference < 14 xss=removed>

The code is as follows:


List ptoevents = new List();
List ptorequest = Trigger.new;
for(Thrive_HR_PTO_Request__c ptr : ptorequest) {
    datetime StartDate = ptr.Start_Date__c;
    datetime EndDate = ptr.End_Date__c;
    date StartDateOnly = StartDate.date(); 
    date EndDateOnly = EndDate.date();
    Integer daysDifference = StartDateOnly.daysBetween(EndDateOnly);
    if(daysDifference < 14 xss=removed>        Event evt = new Event(
            Ownerid=ptr.CreatedById,
            WhatId=ptr.Id,
            Subject='Sicktastic' + ' on ' + ptr.Start_Date__c.date() + ' to ' + ptr.End_Date__c.date(),
            StartDateTime=ptr.Start_Date__c,
            EndDateTime=ptr.End_Date__c,
            ActivityDateTime=ptr.Start_Date__c,
            ActivityDate=ptr.Start_Date__c.date(),
            IsAllDayEvent = ptr.All_Day_Event__c
        );
        // add events to record
        ptoevents.add(evt);
    }
    else if (daysDifference >= 14) {
        ptr.End_Date__c.addError('You can not exceed more than 14 days');
        }
  }
     insert ptoevents;
 }

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.