function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Krishan Gopal 27Krishan Gopal 27 

try to complete that task

After inserting a new candidate record, candidate may insert wrong date in the field “Application Date”. Write a trigger so that every time a new candidate record is created in a system, an “Application Date” field would have a value as system generated CreatedDate field of that record only and not using any Date functions.
User-added image
               can't understand typecasting 
               thank you!
Best Answer chosen by Krishan Gopal 27
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Krishan,

We cannot use Created date in before context because created date will be calculated once the record is saved to database. In case of after context as the record will be in read only mode so it cannot be achieved in the after trigger as well.

The best option would be using the Today date as the record creation date will be always equal to Todays date.
 
trigger ApplyDate on candidate__c (before insert) {
    for(candidate__c cb: Trigger.new){
 cb.ApplicationDate__c = system.today();
    }
}

If this solution helps, Please mark it as best answer.

Thanks

All Answers

Maharajan CMaharajan C
Hi Krishan,

Please try the below code:
 
trigger ApplyDate on Candidate__c(before insert){
	for(Candidate__c cb : Trigger.New){
		if(cb.ApplicationDate__c != system.today()){
            cb.ApplicationDate__c = system.today();  // anyway both created date and today will be same.So you can use today date itself. 
        }
	}
}

Thanks,
Maharajan.C
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Krishan,

We cannot use Created date in before context because created date will be calculated once the record is saved to database. In case of after context as the record will be in read only mode so it cannot be achieved in the after trigger as well.

The best option would be using the Today date as the record creation date will be always equal to Todays date.
 
trigger ApplyDate on candidate__c (before insert) {
    for(candidate__c cb: Trigger.new){
 cb.ApplicationDate__c = system.today();
    }
}

If this solution helps, Please mark it as best answer.

Thanks
This was selected as the best answer
Krishan Gopal 27Krishan Gopal 27
thank you Sir for your time and trouble😊 i am new in this technology I have one more Query Candidate applying for a job whose status is Not Active(deactivate job). He should get an error, “This Job is not active. You can not apply for this job. [image: trigger2.PNG] not getting the idea how to fetch old data with new one to validate. thank you
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Krishan,

Can you please give us more information the relation and stucture of the Job and application objects. How does Candidate apply for the job . Is it creating some record?

Thanks,
 
Krishan Gopal 27Krishan Gopal 27
there is a relation bw job and candidate parent to child i am using master Detail to pick job [image: trigger3.PNG]
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Krishan,

Please let me know of my understaning is correct.

if user is trying to create a candidate record while selecting the job which is inactive . Then we should show and error message ?

Thanks,
 
Krishan Gopal 27Krishan Gopal 27
yes job have a field having checkbox active or inactive we have to restrict for inactive jobs using trigger Thank you
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Krishan,

As this is a follow up can you please create an other question for the same with all these details so it will be easy  for other members in the community who are facing the same type of issues.

Thanks,