0

Getting error in the following lines:

if(Datetime.now().hourgmt() == 16){
                    ev.StartDateTime = Datetime.valueOf(Datetime.now().hourgmt()+24);
                }else{
                    if(Datetime.now().hourgmt()>16){
                        DateTime dd = Datetime.valueOf(Datetime.now().hourgmt()-16);
                        ev.StartDateTime = Datetime.valueOf(Datetime.now().hourgmt()-integer.valueOf(dd));

Batch class:


global class BatchToCreateMeeting implements Database.batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC){

        String query = 'SELECT id, name, account.name, OwnerId from Contact where account.name <> null'; 
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Contact> listCon){
        List<Event> listEv = new List<Event>();
        system.debug('list of conlst :'+listCon);
        for(Contact con : listCon){
            Event ev = new Event();
            ev.Type = 'Email';
            ev.Subject = 'Todays Meeting';
            ev.DurationInMinutes = 60;
            system.debug('current value :'+Datetime.now().hourgmt());
            system.debug('current value :' +Datetime.valueOf(Datetime.now().hourgmt()+ 24));
            if(Datetime.now().hourgmt() == 16){
                    ev.StartDateTime = Datetime.valueOf(Datetime.now().hourgmt()+24);
                }else{
                    if(Datetime.now().hourgmt()>16){
                        DateTime dd = Datetime.valueOf(Datetime.now().hourgmt()-16);
                        ev.StartDateTime = Datetime.valueOf(Datetime.now().hourgmt()-integer.valueOf(dd));
                    }else{
                        if(Datetime.now().hourgmt()<16){
                        DateTime dd = Datetime.valueOf(16-Datetime.now().hourgmt());
                        ev.StartDateTime = Datetime.valueOf(Datetime.now().hourgmt()+integer.valueOf(dd));
                        }
                    }
            }
            system.debug('Time value start wali :'+ ev.StartDateTime);
            ev.EndDateTime = ev.StartDateTime +1;
            ev.Description  = 'Message will show here';
            ev.OwnerId = con.OwnerId;
            ev.WhoId = con.id;
            listEv.add(ev);
        }
        system.debug('list of listEv :'+ listEv);
        if(listEv.size()>0){
            insert listEv;
        }
    }
    global void finish(Database.BatchableContext BC) {

     }
}
2
  • Rahul, can you add the error details, expected behaviour, actual behaviour and time zone of user executing the class? – manjit5190 Mar 27 '20 at 9:29
  • First error: Invalid integer: java.util.GregorianCalendar[time=-3,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT",offset – Rahul Chaudhary Mar 27 '20 at 11:30
1

In this line:

ev.StartDateTime = Datetime.valueOf(Datetime.now().hourgmt()+24);

seems that you want to add 1 day. You do not pass a date, so it resets to 1970-01-01. You should change it to:

ev.StartDateTime = Datetime.now().addDays(1);

Also, in

DateTime dd = Datetime.valueOf(Datetime.now().hourgmt()-16);

you should replace it with:

DateTime dd = Datetime.addHours(-16);

It would help, if you write what you want to achieve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.