We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

How to sent an email from the script ?

dawidkolodz
Tera Contributor

Hi, 

I have a problem, cause I'm trying to sent an email from the script and it is not working.

 

Look, I have a table and I have a group of users which have to create a record once per week.

 

based on this table record I want to sent a remind notification every friday to users which doesn't create a record yet. 

 

I think it will be a good Idea to use "Schedule script Execution" module but probably my script skills are not as good for that.


That's what I've prepared: 

        function getUserAssigned() {
            // var getUser = '';
			var query = 'sys_created_onONThis week@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()';
            var grCheckuser = new GlideRecord('u_calendar_events');
            grCheckuser.addEncoudedQuery(query);
            grCheckuser.addEncoudedQuery(u_calendar_events.u_user, 'f4a92de9870cc610322ea9383cbb354e');
            grCheckuser.query();

            while(grCheckuser.next()){
                gs.eventQueue('incident.inserted', current,"Test message", grCheckuser.u_user); 
				gs.info('TEST info ');
                // gs.eventQueue('incident.inserted', gr, gr.u_user);
                // gs.eventQueue('incident.inserted', gr, gs.getUserName(u_user.name()));
                // gr.update();
                }
        }

do not focus on this script, I know I'do not have a specify a group  and there is a wrong of condition.

 

How to fix this ?

3 REPLIES 3

abbasshaik4
Tera Sage

Hello @dawidkolodz,

Please refer to the below link:
https://www.servicenow.com/community/developer-forum/sending-an-email-from-within-a-script/m-p/20094...

https://docs.servicenow.com/bundle/tokyo-servicenow-platform/page/script/server-scripting/reference/...

 

Mark my correct and helpful, if it is helpful and please hit the thumbs-up button to mark it as the correct solution.
Thanks & Regards,
Abbas Shaik

Sagar Pagar
Tera Patron

Hi @dawidkolodz,

 

Make sure you have records when you apply the conditions in addQuery.

 

Try this updated scripts -

      function getUserAssigned() {
      	// var getUser = '';
      	var query = 'sys_created_onONThis week@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()';
      	var grCheckuser = new GlideRecord('u_calendar_events');
      	grCheckuser.addEncoudedQuery(query);
      	grCheckuser.addEncoudedQuery(u_calendar_events.u_user, 'f4a92de9870cc610322ea9383cbb354e');
      	grCheckuser.query();
      	gs.info('grCheckuser record count: ' + grCheckuser.getRowCount());

      	while (grCheckuser.next()) {
      		gs.eventQueue('incident.inserted', grCheckuser, "Test message", grCheckuser.u_user.toString());

      		gs.info('TEST info ');
      		// gs.eventQueue('incident.inserted', gr, gr.u_user);
      		// gs.eventQueue('incident.inserted', gr, gs.getUserName(u_user.name()));
      		// gr.update();
      	}
      }

 

Thanks,
Sagar Pagar

The world works with ServiceNow