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
Jean-Sebastien DoraisJean-Sebastien Dorais 

Apex: Task link to opportunity and user

I've got the following Apex class that successfully pulls data from the 'Task' object.  I need to lookup the 'Opportunity' object to pull the Opportunity Name.  I also need to grab the name of whoever the task is assigned to (using OwnerID) but I'm not sure what object to link to to get it.

Any pointers would help!
 
public with sharing class SlackNewTaskPublisher {
 	
    private static final String slackURL = 'https://hooks.slack.com/services/abcde';

    public class Tasky {
        @InvocableVariable(label='Subject')
        public String subject;
        @InvocableVariable(label='AccountID')
        public String oppname;
        @InvocableVariable(label='ActivityDate')
        public String duedate;
        @InvocableVariable(label='OwnerId')
        public String assignedto;
    }
     
    @InvocableMethod(label='Post task to Slack')
    public static void postToSlack(List<Tasky> taskies) {
        Tasky o = taskies[0]; // If bulk, only post first to avoid overloading Slack channel
        Map<String,Object> msg = new Map<String,Object>();
        msg.put('text', 'A new task has been added to: *' + o.oppname + '*\nTask: *' + o.subject + '*\nDue on ' + o.duedate + '\nAssigned To: ' + o.assignedto );
        msg.put('mrkdwn', true);
        String body = JSON.serialize(msg);    
        System.enqueueJob(new QueueableSlackCall(slackURL, 'POST', body));
    }
     
    public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
         
        private final String url;
        private final String method;
        private final String body;
         
        public QueueableSlackCall(String url, String method, String body) {
            this.url = url;
            this.method = method;
            this.body = body;
        }
         
        public void execute(System.QueueableContext ctx) {
            HttpRequest req = new HttpRequest();
            req.setEndpoint(url);
            req.setMethod(method);
            req.setBody(body);
            Http http = new Http();
            HttpResponse res = http.send(req);
        }
 
    }
    
}

Thank you!
Raj VakatiRaj Vakati
Try like this
 
public with sharing class SlackNewTaskPublisher {
 	
    private static final String slackURL = 'https://hooks.slack.com/services/abcde';

    public class Tasky {
        @InvocableVariable(label='Subject')
        public String subject;
        @InvocableVariable(label='AccountID')
        public String oppname;
        @InvocableVariable(label='ActivityDate')
        public String duedate;
        @InvocableVariable(label='OwnerId')
        public String assignedto;
    }
     
    @InvocableMethod(label='Post task to Slack')
    public static void postToSlack(List<Tasky> taskies) {
        Tasky o = taskies[0]; // If bulk, only post first to avoid overloading Slack channel
		String whatIds = o.WhatId ; 
		String owner = o .OwnerId ;
		Opportunity op = [Select id , Name from Opportunity where id = : whatIds]; 
		User u  =[Select Name from User where id =: owner];
		
        Map<String,Object> msg = new Map<String,Object>();
        msg.put('text', 'A new task has been added to: *' + o.oppname + '*\nTask: *' + o.subject + '*\nDue on ' + o.duedate + '\nAssigned To: ' + o.assignedto );
        msg.put('mrkdwn', true);
        String body = JSON.serialize(msg);    
        System.enqueueJob(new QueueableSlackCall(slackURL, 'POST', body));
    }
     
    public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
         
        private final String url;
        private final String method;
        private final String body;
         
        public QueueableSlackCall(String url, String method, String body) {
            this.url = url;
            this.method = method;
            this.body = body;
        }
         
        public void execute(System.QueueableContext ctx) {
            HttpRequest req = new HttpRequest();
            req.setEndpoint(url);
            req.setMethod(method);
            req.setBody(body);
            Http http = new Http();
            HttpResponse res = http.send(req);
        }
 
    }
    
}

 
Jean-Sebastien DoraisJean-Sebastien Dorais
Thanks Raj.

So, for the msg.put statement, would the following be correct to display the opportunity name and username?
 
msg.put('text', 'A new task has been added to: *' + Opportunity + '*\nTask: *' + o.subject + '*\nDue on ' + o.duedate + '\nAssigned To: ' + User );

 
Raj VakatiRaj Vakati
try this 

 
public with sharing class SlackNewTaskPublisher {
 	
    private static final String slackURL = 'https://hooks.slack.com/services/abcde';

    public class Tasky {
        @InvocableVariable(label='Subject')
        public String subject;
        @InvocableVariable(label='AccountID')
        public String oppname;
        @InvocableVariable(label='ActivityDate')
        public String duedate;
        @InvocableVariable(label='OwnerId')
        public String assignedto;
    }
     
    @InvocableMethod(label='Post task to Slack')
    public static void postToSlack(List<Tasky> taskies) {
        Tasky o = taskies[0]; // If bulk, only post first to avoid overloading Slack channel
	String whatIds = o.WhatId ; 
		String owner = o .OwnerId ;
		Opportunity op = [Select id , Name from Opportunity where id = : whatIds]; 
		User u  =[Select Name from User where id =: owner];
		
        Map<String,Object> msg = new Map<String,Object>();
        msg.put('text', 'A new task has been added to: *' + op.Name + '*\nTask: *' + o.subject + '*\nDue on ' + o.duedate + '\nAssigned To: ' + u.Name );
        msg.put('mrkdwn', true);
        String body = JSON.serialize(msg);    
        System.enqueueJob(new QueueableSlackCall(slackURL, 'POST', body));
    }
     
    public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
         
        private final String url;
        private final String method;
        private final String body;
         
        public QueueableSlackCall(String url, String method, String body) {
            this.url = url;
            this.method = method;
            this.body = body;
        }
         
        public void execute(System.QueueableContext ctx) {
            HttpRequest req = new HttpRequest();
            req.setEndpoint(url);
            req.setMethod(method);
            req.setBody(body);
            Http http = new Http();
            HttpResponse res = http.send(req);
        }
 
    }
    
}