0

The task is to notify all Opportunity Team Members every time new chatter post appears in feed inside of the Oppty. Now we use the following script:

trigger emailFollowers on FeedItem (after insert) { 
  String OppKeyPrefix = Opportunity.sObjectType.getDescribe().getKeyPrefix();
    for (FeedItem f: trigger.new) { 
    String parentId = f.parentId;
        if (parentId.startsWith(OppKeyPrefix)) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 List<Opportunity> OppOb = [select Opportunity.Name  from Opportunity where Opportunity.id = :f.ParentID]; 
//List<OpportunityTeamMember> OTMList = [Select User.name from OpportunityTeamMember where OpporunityId =:f.ParentID ];
 List<EntitySubscription> followers = [select id, subscriberid, subscriber.name, subscriber.email from EntitySubscription where parentid = :f.ParentID];

        String[] toAddresses = new List<String>();


        for (EntitySubscription follower : followers) {
            toAddresses.add(follower.subscriber.email);
        }

        mail.setToAddresses(toAddresses);


        //mail.setTemplateID(templateId.Id);
        String Subject = 'New post in opportunity feed ' + OppOb[0].Name ;
        mail.setSubject(Subject); 
        List<User> UserList = [select User.Name from User where User.Id =: f.CreatedById ];

String Body ='User '+UserList[0].name+' posted: ' +f.body+' '+f.CreatedDate ;
        mail.setHTMLBody(Body);


        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    }
}

The script works very well but all the members have to FOLLOW every opportunity in chatter. When uncomment line 8 the error is:

Invalid type OpportunityTeamMember

We also tried to replace line 8 with:

SELECT User.Email FROM OpportunityTeamMember WHERE OpportunityId = :oppId AND User.isActive = true

Error: Invalid type: Schema.OpportunityTeamMember

and

SELECT Id, (SELECT User.Email FROM OpportunityTeamMembers WHERE User.IsActive = true) FROM Opportunity WHERE Id IN :changedOppsIds

ERROR at Row:1: Column:36 Didn't understand relationship 'OpportunityTeamMembers' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

Any suggestions on how to improve the script to make it work and avoid following opptys for Team Members?

2
  • spelling of your OpportunityteamMember is wrong Commented Aug 2, 2017 at 6:53
  • 1
    Use this query List<OpportunityTeamMember> OTMList = [Select User.name from OpportunityTeamMember where OpportunityId =: f.ParentID]; Commented Aug 2, 2017 at 6:54

1 Answer 1

1

use this code, I have tested it and it is working fine in my org:

trigger emailFollowers on FeedItem (after insert) { 
  String OppKeyPrefix = Opportunity.sObjectType.getDescribe().getKeyPrefix();
    for (FeedItem f: trigger.new) { 
    String parentId = f.parentId;
        if (parentId.startsWith(OppKeyPrefix)) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 List<Opportunity> OppOb = [select Opportunity.Name  from Opportunity where Opportunity.id = :f.ParentID]; 
List<OpportunityTeamMember> followers = [Select User.name,user.email from OpportunityTeamMember where OpportunityId =: f.ParentID];
// List<EntitySubscription> followers = [select id, subscriberid, subscriber.name, subscriber.email from EntitySubscription where parentid = :f.ParentID];

        String[] toAddresses = new List<String>();


        for (OpportunityTeamMember follower : followers) {
            toAddresses.add(follower.user.email);
        }

        mail.setToAddresses(toAddresses);


        //mail.setTemplateID(templateId.Id);
        String Subject = 'New post in opportunity feed ' + OppOb[0].Name ;
        mail.setSubject(Subject); 
        List<User> UserList = [select User.Name from User where User.Id =: f.CreatedById ];

String Body ='User '+UserList[0].name+' posted: ' +f.body+' '+f.CreatedDate ;
        mail.setHTMLBody(Body);


        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    }
}
7
  • For the script, you offered there are 3 errors for lines 10, 14: Invalid type: OpportunityTeamMember. For line 15: Variable Doesn't Exist: follow.user Commented Aug 2, 2017 at 8:23
  • What error you are getting? Commented Aug 2, 2017 at 8:25
  • Mentioned them above Commented Aug 2, 2017 at 8:29
  • Seems like this class "OpportunityTeamMember" isn't supported or doesn't exist at all. Commented Aug 2, 2017 at 8:33
  • Have you enabled opportunity team? Commented Aug 2, 2017 at 8:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .