• Alex Skempris
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 12
    Replies
Hello 

I've built a VF page along with a controller extension where the user is able to delete a record via an action button. I've set the chosen record id to go to my controller and from there the controller deletes the the record via a DML Statement. What I'm struggling with is writing the test code for it as every delete resource I've found has failed. Or I've written it incorrectly.

I would be grateful for some assistance on that last bit of my test class. Here's my code below:

*****VG PAGE*****
<apex:page standardController="Opportunity" extensions="ClaimAndExtensionsROR" title="SIPP Claim Internal Questionnaire - Previous Claim Details " sidebar="false" showHeader="false">
<br>
<div style="text-align:center;font-size: 30px;">
<apex:outputlabel value="SIPP Claim Internal Questionnaire - Previous Claim Details"/>
</div>
</br>
<apex:pageMessages />
<apex:form >
<script type="text/javascript">
function CloseWindow()
{
window.top.close();
UpdateOpener();
}
</script>

<apex:pageBlock mode="mainDetail">
<apex:pageBlockButtons location="Bottom" >
<apex:commandButton action="{!save}" value="Save Changes" />
<apex:commandButton action="{!save}" value="Save and Close" status="closer" oncomplete="CloseWindow();" />
<apex:actionStatus startText="(Saving... Window will close shortly)" stopText="" id="closer"/>
</apex:pageBlockButtons>
</apex:pageBlock>

<apex:pageBlock title="Pensions" tabstyle="SIPP_Operator__c">
<apex:outputPanel id="PensionMessage">
<apex:pageMessage summary="{!primarymsg}" severity="error" strength="3" rendered="{!primarymsgck}" />
</apex:outputPanel> <div align="center" draggable="false" >
<apex:commandButton action="{!newPensions}" value="New Pension" rendered="{!ISNULL(Opportunity.SIPP__c)}" rerender="PensionsList"/> </div>
<apex:outputPanel id="PensionsList">
<apex:repeat value="{!Pensions}" var="pen" >
<apex:pageBlockSection columns="1" title="Pension {!pen.Name}" collapsible="true">
<apex:pageBlockSectionItem >
<apex:pageBlockSection columns="2">
<apex:pageBlockSection columns="1">
<apex:outputField value="{!pen.Person_Account__c}"/>
<apex:inputField value="{!pen.SIPP_Provider__c}"/>
<apex:inputField value="{!pen.Date_SIPP_opened__c}" />
<apex:inputField value="{!pen.Pension_Category__c}"/>
<apex:inputField value="{!pen.Pension_Type__c}" />
</apex:pageblockSectionItem>
<apex:commandButton value="Delete Pension" action="{!deletePensions}" rendered="{!ISNULL(Opportunity.SIPP__c)}" rerender="PensionsList, PensionMessage">
<apex:param name="PensionsIdent" value="{!pen.id}" assignTo="{!chosenPensionsId}"/> <!-- id goes to the controller --->
</apex:commandButton>
</apex:pageBlocksection>
</apex:repeat>
</apex:outputPanel>
</apex:pageBlock>


****THE CONTROLLER****

public class CtrlExtension {
   public ApexPages.StandardController std;

   public Id chosenPensionsId {get; set;}
   public List<SIPP_Operator__c> Pensions;

public ClaimAndExtensionsROR(ApexPages.StandardController stdCtrl){
        std = stdCtrl;
        List<string> fields = new list<string>{'AccountId', 'Previous_Relevant_Claim__c', 'StageName', 'Opportunity_Sub_Stage__c', 'SIPP__c', 'Previous_Relevant_Claim__r.SIPP__c', 'Previous_Relevant_Claim__r.Financial_Advice__c', 'Total_Compensation_Payable__c', 'Previous_Relevant_Claim__r.Introducer_Agent__c', 'Introducer_Agent__c'};

        if(!Test.isRunningTest())    
           {
        stdCtrl.addFields(fields);    
         }
    }

    public Opportunity getOpportunity()
    {
     return (Opportunity) std.getRecord();
    }

public void newPensions()
    {
      if (updatePensions())
       {
           SIPP_Operator__c pen=new SIPP_Operator__c(Person_Account__c = getOpportunity().Accountid);   
           insert pen;      
            
           Pensiontoinsert = pen.id;
           
          Pensions=null;

       }
    }

public void deletePensions()
    
    {
       if (updatePensions())
       {
          if (null!=chosenPensionsId)
          {
             SIPP_Operator__c pen = new SIPP_Operator__c(Id=chosenPensionsId);
              try{
              delete pen;
              }
              catch (System.DMLException e)
              {  
               primarymsgck = true;
               primarymsg = 'Error - You cannot delete a Pension that is already associated with a claim. Please inform your Salesforce Administrator.';
              }
        
           // null the contacts list so that it is rebuilt
              Pensions=null;
              chosenPensionsId=null;
          }
       }
    }

****TEST CLASS****

@isTest
public class ClaimAndExtensionsRORTestClass {

    Static ClaimAndExtensionsROR clext;


Account testacc = new Account();
        testAcc.LastName = 'test';
        testacc.Email_Address__c = 'alex@brangaene.com';
        testacc.PersonEmail = 'alex@brangaene.com';
        testacc.RecordTypeId = '01258000000VBdv';
        insert testacc;
        
        SIPP_Operator__c pen = new SIPP_Operator__c();
        pen.Person_Account__c = testacc.Id;
        insert pen;
        clext.newPensions();

        PageReference pref = Page.SIPP_Claim_Internal_Questionnaire_PRC;
        pref.getParameters().put('id', opp.id);
        test.setCurrentPage(pref);
        ApexPages.StandardController con = new ApexPages.StandardController(opp);
        clext = new ClaimAndExtensionsROR(con);

//Testing deletion
pen.id = clext.chosenpensionsId;
​delete pen

Clext.deletePensions();
}


Many thanks
Alex

 
Hello,

I'm trying to write a test class for a simple before insert trigger and I can't for the life of me figure out what i'm missing or if I'm in the wrong track completely (i'm an apex beginner). My code below:


trigger Update_Hol_to_Wyb_Fields on Account (before insert) {
    
     for (Account wyb:Trigger.New){
     
                if (wyb.RecordTypeId == '01258000000VBdv' && wyb.PersonLeadSource == 'Holkham'){
                    
                        List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];
                        
                        try{
                        wyb.DOB__c = holkhamAccount[0].DOB__c;
                        wyb.BillingStreet = holkhamAccount[0].BillingStreet;
                        wyb.BillingCity = holkhamAccount[0].BillingCity;
                        wyb.BillingState = holkhamAccount[0].BillingState;
                        wyb.BillingCountry = holkhamAccount[0].BillingCountry;
                        }
                        Catch (exception e){
                        wyb.adderror(e.getmessage()+' Please contact Alex');
                        }
                   }
            }          
     }


My (unsuccessful) test class attempt:

@isTest
public class Update_Hol_to_Wyb_FieldsTestClass 
{
    Static testMethod void myUnitTest()
    {
        // Create the data
        Account wyb = new Account();
        wyb.FirstName = 'alex';
        wyb.LastName = 'Test';
        wyb.RecordTypeId = '01258000000VBdv';
        insert wyb;

        List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];

        Account hol = new Account();
        hol.FirstName = 'alex';
        hol.LastName = 'Test';
        hol.RecordTypeId = '012580000005mIR';
        hol.BillingStreet = holkhamAccount[0].BillingStreet;
        hol.BillingCity = holkhamAccount[0].BillingCity;
        hol.BillingState = holkhamAccount[0].BillingState;
        hol.BillingCountry = holkhamAccount[0].BillingCountry;        
        hol.DOB__c = holkhamAccount[0].DOB__c;
        insert hol;
        
        Test.startTest();     
        insert wyb;
        Test.stopTest();    

        List <Account> wybAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '01258000000VBdv' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName AND PersonLeadSource =: wyb.PersonLeadSource];
        system.assertEquals(1, wybAccount.size());
        system.assertEquals(wybAccount[0].BillingCity, hol.BillingCity);
        system.assertEquals(wybAccount[0].BillingStreet, hol.BillingStreet);        
        system.assertEquals(wybAccount[0].BillingState, hol.BillingState);        
        system.assertEquals(wybAccount[0].BillingCountry, hol.BillingCountry);  
        system.assertEquals(wybAccount[0].DOB__c, hol.DOB__c); 
    }
        
}

Any help would be greatly appreciated.

Thanks
Alex
Hello,

I've been trying to build a VF page that will call a related opportunity record of an opportunity. The related record is being successfully pulled through but there's something missing in my save method and I cannot store inputs of the related record in the database. Can anyone offer some assistance? Here's my code below 

public class ClaimAndExtensionsROR {
    public ApexPages.StandardController std;
    
   Public Opportunity prclaim{get; set;}
   
   public ClaimAndExtensionsROR(ApexPages.StandardController stdCtrl) {
       
    std = stdCtrl;
    stdCtrl.addFields(new List<string>{'AccountId', 'Previous_Relevant_Claim__c'});  
    
     }
   
public Opportunity getOpportunity()
    {
     return (Opportunity) std.getRecord();
    }

public void PopulatedClaim(){
    
  if (null!=getOpportunity().Previous_Relevant_Claim__c && updateRelevantClaims())
         
         {
              Opportunity prclaim=(Opportunity) std.getRecord();
              prclaim.Previous_Relevant_Claim__r =[SELECT Id, Name, AccountId, FOS_Reference_Number__c, StageName, Opportunity_Sub_Stage__c, Submitted_to_FSCS_CB__c,
                                                  Submitted_to_FSCS_Date__c, SIPP__c, Financial_Advice__c, FSCS_Reference_Number__c, Previous_Relevant_Claim__c 
                                                  FROM Opportunity 
                                                  WHERE id=: prclaim.Previous_Relevant_Claim__c];
         

         }
                 
        }

     public PageReference save()
     {
     try{
     upsert prclaim.Previous_Relevant_Claim__r;
     upsert prclaim;
     }
     catch (Exception e){
         System.debug('Exception is =' +e.getMessage());
         }
     Boolean result=true;
     PageReference pr=Page.ROR_Questionnaire;

     if (result)
     {
        // call standard controller save, but don't capture the return value which will redirect to view page
        std.save();
           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved'));
     }
        pr.getParameters().put('id', getOpportunity().id);
      
     return pr;
        }
}
Hello 

I've built a VF page along with a controller extension where the user is able to delete a record via an action button. I've set the chosen record id to go to my controller and from there the controller deletes the the record via a DML Statement. What I'm struggling with is writing the test code for it as every delete resource I've found has failed. Or I've written it incorrectly.

I would be grateful for some assistance on that last bit of my test class. Here's my code below:

*****VG PAGE*****
<apex:page standardController="Opportunity" extensions="ClaimAndExtensionsROR" title="SIPP Claim Internal Questionnaire - Previous Claim Details " sidebar="false" showHeader="false">
<br>
<div style="text-align:center;font-size: 30px;">
<apex:outputlabel value="SIPP Claim Internal Questionnaire - Previous Claim Details"/>
</div>
</br>
<apex:pageMessages />
<apex:form >
<script type="text/javascript">
function CloseWindow()
{
window.top.close();
UpdateOpener();
}
</script>

<apex:pageBlock mode="mainDetail">
<apex:pageBlockButtons location="Bottom" >
<apex:commandButton action="{!save}" value="Save Changes" />
<apex:commandButton action="{!save}" value="Save and Close" status="closer" oncomplete="CloseWindow();" />
<apex:actionStatus startText="(Saving... Window will close shortly)" stopText="" id="closer"/>
</apex:pageBlockButtons>
</apex:pageBlock>

<apex:pageBlock title="Pensions" tabstyle="SIPP_Operator__c">
<apex:outputPanel id="PensionMessage">
<apex:pageMessage summary="{!primarymsg}" severity="error" strength="3" rendered="{!primarymsgck}" />
</apex:outputPanel> <div align="center" draggable="false" >
<apex:commandButton action="{!newPensions}" value="New Pension" rendered="{!ISNULL(Opportunity.SIPP__c)}" rerender="PensionsList"/> </div>
<apex:outputPanel id="PensionsList">
<apex:repeat value="{!Pensions}" var="pen" >
<apex:pageBlockSection columns="1" title="Pension {!pen.Name}" collapsible="true">
<apex:pageBlockSectionItem >
<apex:pageBlockSection columns="2">
<apex:pageBlockSection columns="1">
<apex:outputField value="{!pen.Person_Account__c}"/>
<apex:inputField value="{!pen.SIPP_Provider__c}"/>
<apex:inputField value="{!pen.Date_SIPP_opened__c}" />
<apex:inputField value="{!pen.Pension_Category__c}"/>
<apex:inputField value="{!pen.Pension_Type__c}" />
</apex:pageblockSectionItem>
<apex:commandButton value="Delete Pension" action="{!deletePensions}" rendered="{!ISNULL(Opportunity.SIPP__c)}" rerender="PensionsList, PensionMessage">
<apex:param name="PensionsIdent" value="{!pen.id}" assignTo="{!chosenPensionsId}"/> <!-- id goes to the controller --->
</apex:commandButton>
</apex:pageBlocksection>
</apex:repeat>
</apex:outputPanel>
</apex:pageBlock>


****THE CONTROLLER****

public class CtrlExtension {
   public ApexPages.StandardController std;

   public Id chosenPensionsId {get; set;}
   public List<SIPP_Operator__c> Pensions;

public ClaimAndExtensionsROR(ApexPages.StandardController stdCtrl){
        std = stdCtrl;
        List<string> fields = new list<string>{'AccountId', 'Previous_Relevant_Claim__c', 'StageName', 'Opportunity_Sub_Stage__c', 'SIPP__c', 'Previous_Relevant_Claim__r.SIPP__c', 'Previous_Relevant_Claim__r.Financial_Advice__c', 'Total_Compensation_Payable__c', 'Previous_Relevant_Claim__r.Introducer_Agent__c', 'Introducer_Agent__c'};

        if(!Test.isRunningTest())    
           {
        stdCtrl.addFields(fields);    
         }
    }

    public Opportunity getOpportunity()
    {
     return (Opportunity) std.getRecord();
    }

public void newPensions()
    {
      if (updatePensions())
       {
           SIPP_Operator__c pen=new SIPP_Operator__c(Person_Account__c = getOpportunity().Accountid);   
           insert pen;      
            
           Pensiontoinsert = pen.id;
           
          Pensions=null;

       }
    }

public void deletePensions()
    
    {
       if (updatePensions())
       {
          if (null!=chosenPensionsId)
          {
             SIPP_Operator__c pen = new SIPP_Operator__c(Id=chosenPensionsId);
              try{
              delete pen;
              }
              catch (System.DMLException e)
              {  
               primarymsgck = true;
               primarymsg = 'Error - You cannot delete a Pension that is already associated with a claim. Please inform your Salesforce Administrator.';
              }
        
           // null the contacts list so that it is rebuilt
              Pensions=null;
              chosenPensionsId=null;
          }
       }
    }

****TEST CLASS****

@isTest
public class ClaimAndExtensionsRORTestClass {

    Static ClaimAndExtensionsROR clext;


Account testacc = new Account();
        testAcc.LastName = 'test';
        testacc.Email_Address__c = 'alex@brangaene.com';
        testacc.PersonEmail = 'alex@brangaene.com';
        testacc.RecordTypeId = '01258000000VBdv';
        insert testacc;
        
        SIPP_Operator__c pen = new SIPP_Operator__c();
        pen.Person_Account__c = testacc.Id;
        insert pen;
        clext.newPensions();

        PageReference pref = Page.SIPP_Claim_Internal_Questionnaire_PRC;
        pref.getParameters().put('id', opp.id);
        test.setCurrentPage(pref);
        ApexPages.StandardController con = new ApexPages.StandardController(opp);
        clext = new ClaimAndExtensionsROR(con);

//Testing deletion
pen.id = clext.chosenpensionsId;
​delete pen

Clext.deletePensions();
}


Many thanks
Alex

 
Hello,

I'm trying to write a test class for a simple before insert trigger and I can't for the life of me figure out what i'm missing or if I'm in the wrong track completely (i'm an apex beginner). My code below:


trigger Update_Hol_to_Wyb_Fields on Account (before insert) {
    
     for (Account wyb:Trigger.New){
     
                if (wyb.RecordTypeId == '01258000000VBdv' && wyb.PersonLeadSource == 'Holkham'){
                    
                        List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];
                        
                        try{
                        wyb.DOB__c = holkhamAccount[0].DOB__c;
                        wyb.BillingStreet = holkhamAccount[0].BillingStreet;
                        wyb.BillingCity = holkhamAccount[0].BillingCity;
                        wyb.BillingState = holkhamAccount[0].BillingState;
                        wyb.BillingCountry = holkhamAccount[0].BillingCountry;
                        }
                        Catch (exception e){
                        wyb.adderror(e.getmessage()+' Please contact Alex');
                        }
                   }
            }          
     }


My (unsuccessful) test class attempt:

@isTest
public class Update_Hol_to_Wyb_FieldsTestClass 
{
    Static testMethod void myUnitTest()
    {
        // Create the data
        Account wyb = new Account();
        wyb.FirstName = 'alex';
        wyb.LastName = 'Test';
        wyb.RecordTypeId = '01258000000VBdv';
        insert wyb;

        List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];

        Account hol = new Account();
        hol.FirstName = 'alex';
        hol.LastName = 'Test';
        hol.RecordTypeId = '012580000005mIR';
        hol.BillingStreet = holkhamAccount[0].BillingStreet;
        hol.BillingCity = holkhamAccount[0].BillingCity;
        hol.BillingState = holkhamAccount[0].BillingState;
        hol.BillingCountry = holkhamAccount[0].BillingCountry;        
        hol.DOB__c = holkhamAccount[0].DOB__c;
        insert hol;
        
        Test.startTest();     
        insert wyb;
        Test.stopTest();    

        List <Account> wybAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '01258000000VBdv' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName AND PersonLeadSource =: wyb.PersonLeadSource];
        system.assertEquals(1, wybAccount.size());
        system.assertEquals(wybAccount[0].BillingCity, hol.BillingCity);
        system.assertEquals(wybAccount[0].BillingStreet, hol.BillingStreet);        
        system.assertEquals(wybAccount[0].BillingState, hol.BillingState);        
        system.assertEquals(wybAccount[0].BillingCountry, hol.BillingCountry);  
        system.assertEquals(wybAccount[0].DOB__c, hol.DOB__c); 
    }
        
}

Any help would be greatly appreciated.

Thanks
Alex
Hello,

I've been trying to build a VF page that will call a related opportunity record of an opportunity. The related record is being successfully pulled through but there's something missing in my save method and I cannot store inputs of the related record in the database. Can anyone offer some assistance? Here's my code below 

public class ClaimAndExtensionsROR {
    public ApexPages.StandardController std;
    
   Public Opportunity prclaim{get; set;}
   
   public ClaimAndExtensionsROR(ApexPages.StandardController stdCtrl) {
       
    std = stdCtrl;
    stdCtrl.addFields(new List<string>{'AccountId', 'Previous_Relevant_Claim__c'});  
    
     }
   
public Opportunity getOpportunity()
    {
     return (Opportunity) std.getRecord();
    }

public void PopulatedClaim(){
    
  if (null!=getOpportunity().Previous_Relevant_Claim__c && updateRelevantClaims())
         
         {
              Opportunity prclaim=(Opportunity) std.getRecord();
              prclaim.Previous_Relevant_Claim__r =[SELECT Id, Name, AccountId, FOS_Reference_Number__c, StageName, Opportunity_Sub_Stage__c, Submitted_to_FSCS_CB__c,
                                                  Submitted_to_FSCS_Date__c, SIPP__c, Financial_Advice__c, FSCS_Reference_Number__c, Previous_Relevant_Claim__c 
                                                  FROM Opportunity 
                                                  WHERE id=: prclaim.Previous_Relevant_Claim__c];
         

         }
                 
        }

     public PageReference save()
     {
     try{
     upsert prclaim.Previous_Relevant_Claim__r;
     upsert prclaim;
     }
     catch (Exception e){
         System.debug('Exception is =' +e.getMessage());
         }
     Boolean result=true;
     PageReference pr=Page.ROR_Questionnaire;

     if (result)
     {
        // call standard controller save, but don't capture the return value which will redirect to view page
        std.save();
           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved'));
     }
        pr.getParameters().put('id', getOpportunity().id);
      
     return pr;
        }
}

I'm playing with the following example: http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_custom.htm

 

I created both the controller and the page. However, I see an error: http://i.imgur.com/JxJbnMy.png

 

I have *no* idea what went wrong. The question is how to debug this?