Search Here

Upon Creation of Position (Custom Object) if it is a New Position and Open Date,Min Pay & Max Pay are not populated then populated them with below values: a. Open Date = Today’s Date b. Min Pay = 10000 c. Max Pay = 15000

 Here’s an Apex Trigger that ensures when a Position (custom object) is created, and it is marked as a "New Position," the Open Date, Min Pay, and Max Pay fields are populated with default values if they are not already provided.

trigger SetDefaultValuesOnPosition on Position__c (before insert) {

    for (Position__c position : Trigger.new) {

        // Check if it is a New Position

        if (position.Type__c == 'New Position') {

            // Set Open Date if not already populated

            if (position.Open_Date__c == null) {

                position.Open_Date__c = Date.today();

            }

            // Set Min Pay if not already populated

            if (position.Min_Pay__c == null) {

                position.Min_Pay__c = 10000;

            }

            // Set Max Pay if not already populated

            if (position.Max_Pay__c == null) {

                position.Max_Pay__c = 15000;

            }

        }

    }

}

Explanation of the Code

  1. Trigger Event:

    • Runs on the before insert event to populate fields before the record is saved to the database.
  2. Logic:

    • Checks if the Type__c field (assumed to identify the Position type) is set to "New Position".
    • Ensures that Open Date, Min Pay, and Max Pay fields are only populated with default values if they are null.
  3. Bulk Processing:

    • Loops through all records in Trigger.new to handle bulk inserts.
@isTest
private class SetDefaultValuesOnPositionTest {
    @isTest
    static void testSetDefaultValuesOnPosition() {
        // Test Case 1: Position with missing Open Date, Min Pay, and Max Pay
        Position__c positionWithMissingFields = new Position__c(
            Name = 'Test Position',
            Type__c = 'New Position'
        );
        insert positionWithMissingFields;

        // Query the inserted Position
        Position__c insertedPosition = [SELECT Open_Date__c, Min_Pay__c, Max_Pay__c 
                                        FROM Position__c 
                                        WHERE Id = :positionWithMissingFields.Id];
        System.assertEquals(Date.today(), insertedPosition.Open_Date__c, 'Open Date should be set to Today');
        System.assertEquals(10000, insertedPosition.Min_Pay__c, 'Min Pay should be set to 10000');
        System.assertEquals(15000, insertedPosition.Max_Pay__c, 'Max Pay should be set to 15000');

        // Test Case 2: Position with already populated fields
        Position__c positionWithFields = new Position__c(
            Name = 'Test Position 2',
            Type__c = 'New Position',
            Open_Date__c = Date.today().addDays(1),
            Min_Pay__c = 12000,
            Max_Pay__c = 20000
        );
        insert positionWithFields;

        // Query the second inserted Position
        Position__c insertedPositionWithFields = [SELECT Open_Date__c, Min_Pay__c, Max_Pay__c 
                                                  FROM Position__c 
                                                  WHERE Id = :positionWithFields.Id];
        System.assertEquals(Date.today().addDays(1), insertedPositionWithFields.Open_Date__c, 'Open Date should not be overwritten');
        System.assertEquals(12000, insertedPositionWithFields.Min_Pay__c, 'Min Pay should not be overwritten');
        System.assertEquals(20000, insertedPositionWithFields.Max_Pay__c, 'Max Pay should not be overwritten');
    }
}



Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.