+ Start a Discussion
Forrest MulduneForrest Muldune 

Calculating date in Custom Object

All,
 
I am requesting for some assistance with a trigger I am trying to create within my custom object name  Judgment_Event__c .
 
In the Judgment_Event__c I have the following fields below.
 
Action_Type__c                  (picklist)
 
Recording_Location__c     (picklist)
 
Expiration_Date__c            (date)
 
Recording_Date__c             (date)
 
Years__c                                (Number)
 
 
In my Trigger when Action_Type__c  = = ‘Recording’
And when Recording_Location__c   = = ‘County’
 
When a user enters a value in the Years__c field, I would like the number to be automatically be added to the date in the Recording_Date__c and the final calculation will be automatically populated in the Expiration_Date__c. This will happens whenever a user creates a new record or edits the information.

Below are the coding that I have for now, it is not much since I am a beginner in Apex.

User-added image

your help will be greatly appreciated.
 
Best Answer chosen by Forrest Muldune
KaranrajKaranraj
trigger NewYear on Judgment_Event__c (before insert,before update) {
    
    for(Judgment_Event__c JE: Trigger.new){
        if(je.Action_Type__c == 'Recording' && je.Recording_Location__c == 'County' && je.Years__c  != null && je.Recording_Date__c != null){
          je.Expiration_Date__c =  je.Recording_Date__c.addyears(Integer.valueof(je.Years__c));
        }
    }
}

Try the above code and let me know, if it works for you

Thanks,
Karan

All Answers

KaranrajKaranraj
Hi - I have a question. You mentioned Year is number field. What type of calculation your doing for Expiration_Date__c? 

Let take this example.
If the user enter value in the year field as 4, then recording date will be today(31-12-2014) and Expiration date will be (31-12-2018). Is my understanding is correct?

Thanks,
Karan  
Forrest MulduneForrest Muldune
Karanraj

Yes it is,

if a user enter a  date in the recording date field such as 1/4/2014 and then the user enters 4 in the Years field, then in the Expiration date field, the value will be 1/4/2018

thank you for clarifying this to me. I should wrote an example to you earlier

 
KaranrajKaranraj
trigger NewYear on Judgment_Event__c (before insert,before update) {
    
    for(Judgment_Event__c JE: Trigger.new){
        if(je.Action_Type__c == 'Recording' && je.Recording_Location__c == 'County' && je.Years__c  != null && je.Recording_Date__c != null){
          je.Expiration_Date__c =  je.Recording_Date__c.addyears(Integer.valueof(je.Years__c));
        }
    }
}

Try the above code and let me know, if it works for you

Thanks,
Karan
This was selected as the best answer
Forrest MulduneForrest Muldune
Karanraj,

Thank you for your help  on this . I really appreciate it. I am studying your code to know what updates you did to make this trigger works. 

Do you know of any Apex trigger beginner tutorials I could learn from? I am looking for a real practical website for beginners. 

Currently, I ordered this book http://www.amazon.com/gp/product/0596009208/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1  so that I can learn Java as well. 

Thanks.

Forrest
KaranrajKaranraj
Hi Forrest - I strongly recommend you to take a look into the salesforce Trailehead https://developer.salesforce.com/trailhead?utm_campaign=homepage-banner&utm_source=DSC&utm_medium=website (https://developer.salesforce.com/trailhead?utm_campaign=homepage-banner&utm_source=DSC&utm_medium=website) Is the best place to get start with the salesforce platform. This trailhead module will have videos, tips and many links to dive deeper and also each module will have some practical excersie to do.

Happy learning and Happy New year :)

Thanks,
Karan
Forrest MulduneForrest Muldune
Karanraj,

Happy New Year to you too. Thanks for the website and tips.

Regards,

Forrest