You need to sign in to do that
Don't have an account?
Bryan Cerrati
Isolating Time from DateTime class
hey guys im very new to programming in general, i dont know advanced techniques or anything but you all seem so generous and helpful so i thought id give it a shot. ok so here we go... i have a custom object that is a lead recieving platform from jungo. i need to write a trigger for leads that come in after buisness hours to send a specific message. i didnt see a way for the point and click tools to diferentiate the date from the time. it doesnt matter the date the leads will be coming in every day and all days. but leads that come in after 6pm est i woulld like to send specific messages and set specific tasks.
i came to the conclusion that (i may be wrong and doing a terrible job) i would need to convert the datetime field of CreatedDate to a string, then use substringAfter(' ') to get just the time as a string. then from there i can use basic operators to compare to after hours times.
i created a class with a method for just adding the time as a string to a field i created called time__c
im not sure what i am doing wrong, so any advice will help
i am obviously going to have to create this as a after trigger but i am literally trying to get a feel for how salesforce works, and like i said before i know absolutly nothing about coding.
i came to the conclusion that (i may be wrong and doing a terrible job) i would need to convert the datetime field of CreatedDate to a string, then use substringAfter(' ') to get just the time as a string. then from there i can use basic operators to compare to after hours times.
i created a class with a method for just adding the time as a string to a field i created called time__c
im not sure what i am doing wrong, so any advice will help
public with sharing class timingClass { public static void createTimeStamp() { List<JungoLeadsFrc__JungoLeads__c> dateAndTime = [SELECT Id, CreatedDate, Time__c from JungoLeadsFrc__JungoLeads__c]; for (JungoLeadsFrc__JungoLeads__c lead : dateAndTime) { String dateNtime = string.valueof(dateAndTime.CreatedDate); String s2 = dateNtime.substringBefore(' '); lead.Time = s2; } update dateAndTime; } }
i am obviously going to have to create this as a after trigger but i am literally trying to get a feel for how salesforce works, and like i said before i know absolutly nothing about coding.
All Answers
I can give you pseudo kind of code.
public class JungoLeadTriggerHandler
{
public void onAfterInsert(List<JungoLeadsFrc__JungoLeads__c> lstNewLead)
{
sendAlertAfter6pm(lstNewLead);
}
private void sendAlertAfter6pm(List<JungoLeadsFrc__JungoLeads__c> lstNewLead)
{
for(JungoLeadsFrc__JungoLeads__c objLead: lstNewLead)
{
if(objLead.CreatedDate.hour() >= 18)
{
// Add a code to send email.
}
}
}
}
Trigger :
trigger JungoLeadTrigger on JungoLeadsFrc__JungoLeads__c(after insert)
{
JungoLeadHandler objHandler = new JungoLeadHandler();
if(Trigger.isAfter && Trigger.isInsert)
objHandler.onAfterInsert(Trigger.New);
}
Try this class
and trigger
Let me know if it helps.
Make it as best answer if it helps.
Thanks.
i need it to work at 9am to 6pm. is there a way to add a range.
if(objLead.CreatedDate.hour() >= 9 && objLead.CreatedDate.hour() <= 18) which
i ran into an issue.
i dont under stand the line:
you listed it as number 5. as add you email to master list... im not sure what you mean by that?
and i get a complile error of "mails variable is not found" but im not sure what the assignment should have been because i do not understand the goal of the line.
thank you nayana! i have activated it inside of my sandbox and i will let you know what is the end result. thank you again i will followup tommorrow on the progress.
are both loops going to run each time or do i have to crete some kind of break?
i had to add an else to the whole loop as i need diffrent messages to be sent during the hours of 9-5 so it got quite big, but i also needed to create tasks diffrent ones for 9-5 and diffrent ones for 6-8.59am so i set them in the if's
heres the code i have so far.
i put the new for loop outside of the if that contins any hours because i need the field updates to execute no matter what. reguarless of time.
thaank you again.
ohhhh i just added zeros as a place holder... and i dont want to use and variables for the user. it will be very specific whom will be assigned... and the creator isnt nessicarily the owner. so i just hardcoded it in.
creating a list for the tasks outside the loop at
i have 2 tasks assigned to be created in the first main else. that wont be a problem? are they 2 new objects?
thanx for the link i am going to print it out and put it on my cubicle...thank you :-)
now i just need to lear how to write a test for this class and i should be good. everything seems great from here.
- No issues in this. It should actually coded in this way. First, create a list instance, then add individual task instanced to this list and after the loop do DML operation. (which means in bulk you are handling code)
Imagine you are creating a space(List<Task> lstTaskToInsert) to hold records which you are going to insert in bulk. In salesforce bulk handling is important.
And then in for loop, you are creating a new Task record (new Task(.....)) and adding to this space.After the loop collections are records resides in lstTaskToInsert which you will insert at the end.
i have another question... i am writing out a test class, but createdDate cannot be written... so im not sure how i can test any of the parts that rely on created date. which is 40% of the code.
i know this executes in sandbox ive tested it physically.
the test is not taking the time into consideration... so i dont know if its the test class or the class
i can execute all methods and none of the following is included in the test.
all of this is untested. so i get only 61% of code coverage. im lost i guess.