• Jewelyn Fregoe
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 5
    Replies
So I posted on the SFDC help forums about a problem I'm having where my Opportunity Record Type doesn't seem to stick. I think I've narrowed down the problem to this Apex Class that was already in my SFDC site when I inherited it. Can anyone confirm that's what's happening? Bonus points if you know how to fix it so it allows for different record types to be used! :) 
 
public class NewOpportunityOverrideController
{
    private Opportunity opp;
    
    
    public NewOpportunityOverrideController(ApexPages.StandardController controller)
    {
        opp = (Opportunity) controller.getRecord();
    }



    public PageReference getOverrideURL()
    {
        
        Account acc;
        Opportunity_Prefix__c updateSeq;
        if (opp!=null && opp.AccountId!=null)
        {
          acc = [SELECT Id
                        ,Name
                          ,Opportunity_Prefix__c
                          ,Opportunity_Prefix__r.Name
                          ,Opportunity_Prefix__r.Next_Sequence__c
                     FROM Account
                    WHERE Id=:opp.AccountId];
          // increment seq
          if (acc.Opportunity_Prefix__c!=null)
          {
            updateSeq = new Opportunity_Prefix__c(Id=acc.Opportunity_Prefix__c, Next_Sequence__c=acc.Opportunity_Prefix__r.Next_Sequence__c+1);
            update updateSeq ;
          }
        }
        else
        {
            throw new OpportunityException('::::: Please create opportunities from the Account page :::::');
        }
        
        
        String oppName = 'Please associate account with an Opportunity Prefix';
        
        if (acc!=null && acc.Opportunity_Prefix__c!=null)
        {
            string seq = String.ValueOf(acc.Opportunity_Prefix__r.Next_Sequence__c);
            string paddedSeq = seq.leftPad(3);
            paddedSeq = paddedSeq.replace(' ', '0');
            oppName = acc.Opportunity_Prefix__r.Name + paddedSeq + ' -';
        }
        
        
        return new PageReference('/006/e?opp3='+oppName+'&nooverride=1');
    }

  public class OpportunityException extends Exception {}

}

 
I'm still new to writing triggers/Apex Classes, so I think I may have taken on something a bit too complicated for my first one. Any help is appreciated!

Here's my Apex Trigger. I have it in my sandbox and it does work! Everytime an opportunity's status changes, a post is made in a group chatter.

trigger trgr_CreateChatterGroupAlerts on Opportunity (after Update) {

   List<Opportunity> updatedOpptyList = 
       [
           SELECT
               Id,
               Name,
               Account.Name,
               StageName,
               LastModifiedBy.Name
           FROM
               Opportunity
           WHERE
               Id IN :trigger.newMap.keyset()
       ];
       
   String changedOpptyList = '';
   
   for(Opportunity oppty : updatedOpptyList){
       if(oppty.StageName != trigger.oldMap.get(oppty.Id).StageName){
           changedOpptyList +=
               'Opportunity ' + oppty.Name + ' (' + oppty.Account.Name + ')'+
               ' has its Stage changed from ' + trigger.oldMap.get(oppty.Id).StageName + ' to ' +
               oppty.StageName + '\r\n';
       }
   }
   
   if(changedOpptyList!=''){
       FeedItem feedItem = new FeedItem();
    
       feedItem.ParentId = System.Label.Opportunity_Alerts_Group_Id;
       feedItem.Body = changedOpptyList;
    
       INSERT feedItem;
   }
}



I've never written an Apex Class before, so this part is new to me... Here's what I've done so far and it keeps failing!! I am open to starting over if I need to because I can't figure out how to fix it. I think it's probably going wrong at the boolean, but I really don't know.

@isTest
public class trgr_CreateChatterGroupAlerts_Test{
   static Opportunity tOppty;
   static CollaborationGroup chatterGrp;
   
   public static void createTestData(){
       tOppty = new Opportunity(
           Name = 'Test Oppty',
           Proposal__c = 'DMO003',
           StageName = 'Ballpark',
           CloseDate = System.today() + 10
       );
       
       INSERT tOppty;
       
       chatterGrp = new CollaborationGroup(
           Name = 'Test Group',
           CollaborationType = 'Public'
       );
       
       INSERT chatterGrp;
   }
   
   public static void updateOpptyStage(){
       tOppty.StageName = 'Proposal';
       UPDATE tOppty;
   }
   
   public static Boolean isFeedItemCreated(){
       FeedItem feedItem = 
           [
               SELECT
                   Id,
                   ParentId,
                   Body
               
               FROM
                   FeedItem
               
               WHERE
                   ParentId = :chatterGrp.Id
                   
               ORDER BY CreatedDate DESC
               
               LIMIT 1
           ];    
           
           if(feedItem!=NULL && feedItem.Body.contains(tOppty.Name)){
               return TRUE;
           }
           else{
               return FALSE;
           }
   }
   
   public testMethod static void triggerInAction(){
       test.startTest();
           
           createTestData();
           
           updateOpptyStage();
           
           System.assert(true, isFeedItemCreated());
               
       test.stopTest();
   }
}



This is the error message I get when I run it:

Class: trgr_CreateChatterGroupAlerts3_Test
Method Name: triggerInAction
Pass/Fail: Fail
Error Message: System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.trgr_CreateChatterGroupAlerts3_Test.isFeedItemCreated: line 30, column 1
Class.trgr_CreateChatterGroupAlerts3_Test.triggerInAction: line 63, column 1



Thanks in advance for any help!!!
So I posted on the SFDC help forums about a problem I'm having where my Opportunity Record Type doesn't seem to stick. I think I've narrowed down the problem to this Apex Class that was already in my SFDC site when I inherited it. Can anyone confirm that's what's happening? Bonus points if you know how to fix it so it allows for different record types to be used! :) 
 
public class NewOpportunityOverrideController
{
    private Opportunity opp;
    
    
    public NewOpportunityOverrideController(ApexPages.StandardController controller)
    {
        opp = (Opportunity) controller.getRecord();
    }



    public PageReference getOverrideURL()
    {
        
        Account acc;
        Opportunity_Prefix__c updateSeq;
        if (opp!=null && opp.AccountId!=null)
        {
          acc = [SELECT Id
                        ,Name
                          ,Opportunity_Prefix__c
                          ,Opportunity_Prefix__r.Name
                          ,Opportunity_Prefix__r.Next_Sequence__c
                     FROM Account
                    WHERE Id=:opp.AccountId];
          // increment seq
          if (acc.Opportunity_Prefix__c!=null)
          {
            updateSeq = new Opportunity_Prefix__c(Id=acc.Opportunity_Prefix__c, Next_Sequence__c=acc.Opportunity_Prefix__r.Next_Sequence__c+1);
            update updateSeq ;
          }
        }
        else
        {
            throw new OpportunityException('::::: Please create opportunities from the Account page :::::');
        }
        
        
        String oppName = 'Please associate account with an Opportunity Prefix';
        
        if (acc!=null && acc.Opportunity_Prefix__c!=null)
        {
            string seq = String.ValueOf(acc.Opportunity_Prefix__r.Next_Sequence__c);
            string paddedSeq = seq.leftPad(3);
            paddedSeq = paddedSeq.replace(' ', '0');
            oppName = acc.Opportunity_Prefix__r.Name + paddedSeq + ' -';
        }
        
        
        return new PageReference('/006/e?opp3='+oppName+'&nooverride=1');
    }

  public class OpportunityException extends Exception {}

}

 
I'm still new to writing triggers/Apex Classes, so I think I may have taken on something a bit too complicated for my first one. Any help is appreciated!

Here's my Apex Trigger. I have it in my sandbox and it does work! Everytime an opportunity's status changes, a post is made in a group chatter.

trigger trgr_CreateChatterGroupAlerts on Opportunity (after Update) {

   List<Opportunity> updatedOpptyList = 
       [
           SELECT
               Id,
               Name,
               Account.Name,
               StageName,
               LastModifiedBy.Name
           FROM
               Opportunity
           WHERE
               Id IN :trigger.newMap.keyset()
       ];
       
   String changedOpptyList = '';
   
   for(Opportunity oppty : updatedOpptyList){
       if(oppty.StageName != trigger.oldMap.get(oppty.Id).StageName){
           changedOpptyList +=
               'Opportunity ' + oppty.Name + ' (' + oppty.Account.Name + ')'+
               ' has its Stage changed from ' + trigger.oldMap.get(oppty.Id).StageName + ' to ' +
               oppty.StageName + '\r\n';
       }
   }
   
   if(changedOpptyList!=''){
       FeedItem feedItem = new FeedItem();
    
       feedItem.ParentId = System.Label.Opportunity_Alerts_Group_Id;
       feedItem.Body = changedOpptyList;
    
       INSERT feedItem;
   }
}



I've never written an Apex Class before, so this part is new to me... Here's what I've done so far and it keeps failing!! I am open to starting over if I need to because I can't figure out how to fix it. I think it's probably going wrong at the boolean, but I really don't know.

@isTest
public class trgr_CreateChatterGroupAlerts_Test{
   static Opportunity tOppty;
   static CollaborationGroup chatterGrp;
   
   public static void createTestData(){
       tOppty = new Opportunity(
           Name = 'Test Oppty',
           Proposal__c = 'DMO003',
           StageName = 'Ballpark',
           CloseDate = System.today() + 10
       );
       
       INSERT tOppty;
       
       chatterGrp = new CollaborationGroup(
           Name = 'Test Group',
           CollaborationType = 'Public'
       );
       
       INSERT chatterGrp;
   }
   
   public static void updateOpptyStage(){
       tOppty.StageName = 'Proposal';
       UPDATE tOppty;
   }
   
   public static Boolean isFeedItemCreated(){
       FeedItem feedItem = 
           [
               SELECT
                   Id,
                   ParentId,
                   Body
               
               FROM
                   FeedItem
               
               WHERE
                   ParentId = :chatterGrp.Id
                   
               ORDER BY CreatedDate DESC
               
               LIMIT 1
           ];    
           
           if(feedItem!=NULL && feedItem.Body.contains(tOppty.Name)){
               return TRUE;
           }
           else{
               return FALSE;
           }
   }
   
   public testMethod static void triggerInAction(){
       test.startTest();
           
           createTestData();
           
           updateOpptyStage();
           
           System.assert(true, isFeedItemCreated());
               
       test.stopTest();
   }
}



This is the error message I get when I run it:

Class: trgr_CreateChatterGroupAlerts3_Test
Method Name: triggerInAction
Pass/Fail: Fail
Error Message: System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.trgr_CreateChatterGroupAlerts3_Test.isFeedItemCreated: line 30, column 1
Class.trgr_CreateChatterGroupAlerts3_Test.triggerInAction: line 63, column 1



Thanks in advance for any help!!!
Hi,
I am trying to login to the sandbox, and I am prompted to enter the verification code but the email seems to have sent to a different email (*******@****le.com) than the email (*******@****nk.com) I have in Salesforce personal information. The verification email was sent to the correct email when I login to the Salesforce.com account the first time. I have verified my email in the personal information page. Any idea what is going wrong? I can't access the sandbox without the verification code. Thanks!