• Ryan Reese
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies
Newb apex - how do I build test class for the following trigger and someone told me not to hardcode the chatter group id how should I reference it in the code?

trigger OppWonChatter on Opportunity (after insert, after update) {

String status;
String OppAccName;
String OppOwnerName;
String OppName;   
FeedItem post = new FeedItem();
    
    for(Opportunity o : Trigger.new) {
        if(o.OwnerId == '00560000001MxXV') { //It will not post record for for this user to group. changed last letter to cap v so i can test
            return;
        }
        else {
            if(Trigger.isInsert ) {
                if( o.IsWon == true ) { //This will be executed on new record insertion
                    for (Opportunity oppty : [SELECT Account.Name, Owner.Name, Name FROM Opportunity WHERE Id =:o.Id] ) {
                        OppAccName = oppty.Account.Name;
                     OppName = oppty.Name;
                    
                        
                        OppOwnerName = oppty.Owner.Name;
                    }   
                    
                    status = OppOwnerName + ' just won ' + OppName + '!';

                                                            
                    post.ParentId = '0F9m00000008ZTM';
                    post.Title = o.Name;   
                    post.Body = status;
                    
                    insert post;
                }
            }    
            else {
                if ( Trigger.isUpdate ) {
                    if( o.IsWon == true ) { //This will be executed on update to existing record
                        for (Opportunity oppty : [SELECT Account.Name, Owner.Name, Name FROM Opportunity WHERE Id =:o.Id] ) {
                            OppName = oppty.Name;
                            OppAccName = oppty.Account.Name;  
                            OppOwnerName = oppty.Owner.Name;
                        }        
                        status = OppOwnerName + ' just won ' + OppName + '!';
                                               
                        post.ParentId = '0F9m00000008ZTM';
                        post.Title = o.Name;
                        post.Body = status;
             
So I entered this code in my sandbox with a test opportunity and chatter group but how do I deploy to production

trigger OppWonChatter on Opportunity (after insert, after update) {

String status;
String OppAccName;
String OppOwnerName;
FeedItem post = new FeedItem();
    
    for(Opportunity o : Trigger.new) {
        if(o.OwnerId == '00560000001MxXV') { //It will not post record for for this user to group. changed last letter to cap v so i can test
            return;
        }
        else {
            if(Trigger.isInsert ) {
                if( o.IsWon == true ) { //This will be executed on new record insertion
                    for (Opportunity oppty : [SELECT Account.Name, Owner.Name FROM Opportunity WHERE Id =:o.Id] ) {
                        OppAccName = oppty.Account.Name;
                        OppOwnerName = oppty.Owner.Name;
                    }    
                    status = OppOwnerName + ' just won ' + OppAccName + ' for ' + o.expectedrevenue + '!';

                    
                    post.ParentId = '0F9m00000008ZTM';
                    post.Title = o.Name;   
                    post.Body = status;
                    
                    insert post;
                }
            }    
            else {
                if ( Trigger.isUpdate ) {
                    if( o.IsWon == true ) { //This will be executed on update to existing record
                        for (Opportunity oppty : [SELECT Account.Name, Owner.Name FROM Opportunity WHERE Id =:o.Id] ) {
                            OppAccName = oppty.Account.Name;
                            OppOwnerName = oppty.Owner.Name;
                        }    
                        status = OppOwnerName + ' just won ' + OppAccName + '!';
                                          
                        post.ParentId = '0F9m00000008ZTM';
                        post.Title = o.Name;
                        post.Body = status;
                        
                        insert post;      
                    }
                }
            }
      
When an opportunity closes to won, I want a chatter update to a specified chatter group (which i dont have listed)
I tried editing this code from someone but need some assistance

trigger OppWonChatter on Opportunity (after insert, after update) {

String status;
String OppAccName;
String OppOwnerName;
FeedItem post = new FeedItem();
    
    for(Opportunity o : Trigger.new) {
        if(o.OwnerId == '00560000001MxXV') { //It will not post record for for this user to group. changed last letter to cap v so i can test
            return;
        }
        else {
            if(Trigger.isInsert ) {
                if( o.IsWon == true ) { //This will be executed on new record insertion
                    for (Opportunity oppty : [SELECT Account.Name, Owner.Name FROM Opportunity WHERE Id =:o.Id] ) {
                        OppAccName = oppty.Account.Name;
                        OppOwnerName = oppty.Owner.Name;
                    }    
                    status = OppOwnerName + ' just won ' + OppAccName + ' for ' + o.expectedrevenue + '!';

                    
                    post.ParentId = OppAccName;
                    post.Title = o.Name;
                    post.Body = status;
                    
                    insert post;
                }
            }    
            else {
                if ( Trigger.isUpdate ) {
                    if( o.IsWon == true && Trigger.oldMap.get(o.id).IsWon == false) { //This will be executed on update to existing record
                        for (Opportunity oppty : [SELECT Account.Name, Owner.Name FROM Opportunity WHERE Id =:o.Id] ) {
                            OppAccName = oppty.Account.Name;
                            OppOwnerName = oppty.Owner.Name;
                        }    
                        status = OppOwnerName + ' just won ' + OppAccName + '!';
                            
                        post.ParentId = OppAccName;
                        post.Title = o.Name;
                        post.Body = status;
                        
                        insert post;      
                    }
                }
            }
        }
    }    
}
Newb apex - how do I build test class for the following trigger and someone told me not to hardcode the chatter group id how should I reference it in the code?

trigger OppWonChatter on Opportunity (after insert, after update) {

String status;
String OppAccName;
String OppOwnerName;
String OppName;   
FeedItem post = new FeedItem();
    
    for(Opportunity o : Trigger.new) {
        if(o.OwnerId == '00560000001MxXV') { //It will not post record for for this user to group. changed last letter to cap v so i can test
            return;
        }
        else {
            if(Trigger.isInsert ) {
                if( o.IsWon == true ) { //This will be executed on new record insertion
                    for (Opportunity oppty : [SELECT Account.Name, Owner.Name, Name FROM Opportunity WHERE Id =:o.Id] ) {
                        OppAccName = oppty.Account.Name;
                     OppName = oppty.Name;
                    
                        
                        OppOwnerName = oppty.Owner.Name;
                    }   
                    
                    status = OppOwnerName + ' just won ' + OppName + '!';

                                                            
                    post.ParentId = '0F9m00000008ZTM';
                    post.Title = o.Name;   
                    post.Body = status;
                    
                    insert post;
                }
            }    
            else {
                if ( Trigger.isUpdate ) {
                    if( o.IsWon == true ) { //This will be executed on update to existing record
                        for (Opportunity oppty : [SELECT Account.Name, Owner.Name, Name FROM Opportunity WHERE Id =:o.Id] ) {
                            OppName = oppty.Name;
                            OppAccName = oppty.Account.Name;  
                            OppOwnerName = oppty.Owner.Name;
                        }        
                        status = OppOwnerName + ' just won ' + OppName + '!';
                                               
                        post.ParentId = '0F9m00000008ZTM';
                        post.Title = o.Name;
                        post.Body = status;
             
So I entered this code in my sandbox with a test opportunity and chatter group but how do I deploy to production

trigger OppWonChatter on Opportunity (after insert, after update) {

String status;
String OppAccName;
String OppOwnerName;
FeedItem post = new FeedItem();
    
    for(Opportunity o : Trigger.new) {
        if(o.OwnerId == '00560000001MxXV') { //It will not post record for for this user to group. changed last letter to cap v so i can test
            return;
        }
        else {
            if(Trigger.isInsert ) {
                if( o.IsWon == true ) { //This will be executed on new record insertion
                    for (Opportunity oppty : [SELECT Account.Name, Owner.Name FROM Opportunity WHERE Id =:o.Id] ) {
                        OppAccName = oppty.Account.Name;
                        OppOwnerName = oppty.Owner.Name;
                    }    
                    status = OppOwnerName + ' just won ' + OppAccName + ' for ' + o.expectedrevenue + '!';

                    
                    post.ParentId = '0F9m00000008ZTM';
                    post.Title = o.Name;   
                    post.Body = status;
                    
                    insert post;
                }
            }    
            else {
                if ( Trigger.isUpdate ) {
                    if( o.IsWon == true ) { //This will be executed on update to existing record
                        for (Opportunity oppty : [SELECT Account.Name, Owner.Name FROM Opportunity WHERE Id =:o.Id] ) {
                            OppAccName = oppty.Account.Name;
                            OppOwnerName = oppty.Owner.Name;
                        }    
                        status = OppOwnerName + ' just won ' + OppAccName + '!';
                                          
                        post.ParentId = '0F9m00000008ZTM';
                        post.Title = o.Name;
                        post.Body = status;
                        
                        insert post;      
                    }
                }
            }