Category Archives: Salesforce

Ternary Operator in Salesforce Apex

Ternary Operator : (x ? y : z)

Ternary operator is a one liner replacement for if-then-else statement. If x, a Boolean, is true, y is the result. Otherwise z is the result. Note that x cannot be null.

Example:

Boolean isLeapYear = true;

//Using Ternary Operator
String msg = isLeapYear ? 'It is a leap year.' : 'It is not a leap year.';
System.debug('Message - ' + msg);

If “isLeapYear” is true, assign the message value to “It is a leap year”, else assign message value to “It is not a leap year”.

Difference Between Two Datetime Fields in Salesforce Formula Field

Sample Code:

IF (FLOOR((EndDateTime__c - StartDateTime__c)) > 0, TEXT(FLOOR((EndDateTime__c - StartDateTime__c)) ) & " Days ", "") 
& IF(FLOOR(MOD((EndDateTime__c - StartDateTime__c)* 24, 24 )) > 0, TEXT(FLOOR(MOD((EndDateTime__c - StartDateTime__c)* 24, 24 ))) & " Hours ","") 
& TEXT(ROUND(MOD((EndDateTime__c - StartDateTime__c)* 24 * 60, 60 ), 0)) & " Minutes "
& TEXT(ROUND(MOD((EndDateTime__c - StartDateTime__c)* 24 * 60*60, 60 ), 0)) & " Seconds" 

Invoke Apex Callout From Process Builder

Sample Code:

public class ContactProcessBuilderHandler {
    
    @InvocableMethod 
    public static void sendContacts(List<Contact> conList) {
        string jsonData = JSON.serialize(conList);
        sendContactsToOracle(jsonData);
    }
    
    @future(callout = true)
    public static void sendContactsToOracle(string jsonData) {
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        req.setEndpoint('https://your endpoint url');
        req.setMethod('POST');
        req.setHeader('Authorization', 'Authorization Header');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(jsonData);
        req.setCompressed(true); 
		res = http.send(req);
    }
}