• Alastair Phipps
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
So, I've started learning a little apex (having never looked at it before this morning) to help my org with making minor edits to existing functionality (adding variables to conditional statements etc.)

But I've run into a bit of a snag, the original developers (3 years ago or more) did not reach full test coverage (I guess they uploaded directly rather than writing in Salesforce). So I'm now retrospectively tying to write test classes for code I don't necessarily understand in full, so that I can implement my small tweaks. eek!

I've been doing OK, but got stuck on a particular IF, I thought my test class should cover it, but it doesn't! I'm guessing I need to cover some other variable, but I can't for the life of me work out what it is! Anyone that can point out where I'm going wrong would be my personal hero!

Here is the code I'm trying to test:
(I've put my existing code coverage in bold)
List<Payment_Records__c> lstPayment=new List<Payment_Records__c>();
Map<Id,Opportunity> mapoppId=new Map<Id,Opportunity>(); 
List<String> lstprogramTypes=new List<String>();
       
        for(Opportunity opp : opps){
            if(opp.RecordtypeId == schoolRecTypeId || opp.RecordtypeId == fundraisingRecTypeId || opp.RecordtypeId == orgRecTypeId){
                if(!opp.Payment_records_created__c){
                    Payment_Records__c precord = new Payment_Records__c();
                    if(opp.No_of_Years__c == '1'){
                        precord.Amount__c = opp.Amount;
                        if(opp.RecordtypeId == schoolRecTypeId || opp.RecordtypeId == orgRecTypeId){
                            precord.Payment_year__c = opp.Year__c;
                        }
                    }
                    precord.Opportunity__c =opp.Id;
                    lstPayment.add(precord);
                    opp.Payment_records_created__c =true;   
                }              
                else if(opp.Payment_records_created__c==True && oldMap.get(opp.Id).Amount != opp.Amount && opp.No_of_Years__c == '1'){
                    mapOppId.put(opp.Id,opp);
                }
            }       
            if(opp.RecordtypeId == schoolRecTypeId || opp.RecordtypeId== orgRecTypeId){
                if(opp.Package_Prog__c != null){
                    lstprogramTypes.add(opp.Package_Prog__c);
                }
            }
        }
And my test class:
(sorry if it's a mess, I've been refactoring this for well over an hour!)
@isTest static void testpaymentrecordcreation() {
        List<Payment_Records__c> lstPayment=new List<Payment_Records__c>();
        List<Payment_Records__c> new_payments = new List<Payment_Records__c>();
        Id schoolRecTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('School Opportunity').getRecordTypeId();
        Id orgRecTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('Organisation Opportunity').getRecordTypeId();
        Id fundraisingRecTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('Fundraising').getRecordTypeId();
        Id recTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('Business Partner Opportunity').getRecordTypeId();
        
        Account acc = new Account(Name='APtest');
        Insert acc;
        
        Programme__c prog = new Programme__c(School__c = acc.Id);
        insert prog;
        
        Programme_Type__c progType = new Programme_Type__c(name = 'Bespoke');
        Insert progtype;
        
        Opportunity opp = new Opportunity (AccountId = acc.Id, Name = 'APtestOpportunity', CloseDate = system.today(),
            StageName = 'Prospecting', Programme__c = prog.Id, Payment_records_created__c = True,
            Amount = 100, Year__c = '2016', No_of_Years__c = '1', RecordTypeId = orgRecTypeId);
        Insert opp;
        
        Payment_Records__c precord = new Payment_Records__c(Opportunity__c = opp.Id, Payment_year__c = opp.Year__c, Amount__c = opp.Amount);
        Insert precord;
            
        Test.StartTest();
        	system.assertNotEquals(False, opp.Payment_records_created__c);
            system.assert(opp.No_of_Years__c == '1');
            system.assert(opp.RecordtypeId == '0120O000000dSyq');
            system.assert(precord.Opportunity__c == opp.Id);
            system.assert(precord.Amount__c == opp.amount);
            system.assert(precord.Payment_year__c == opp.Year__c);
        Test.StopTest();
    }

Again, if you can point out where I'm going wrong, you'd be my personal hero!

A.
So, I've started learning a little apex (having never looked at it before this morning) to help my org with making minor edits to existing functionality (adding variables to conditional statements etc.)

But I've run into a bit of a snag, the original developers (3 years ago or more) did not reach full test coverage (I guess they uploaded directly rather than writing in Salesforce). So I'm now retrospectively tying to write test classes for code I don't necessarily understand in full, so that I can implement my small tweaks. eek!

I've been doing OK, but got stuck on a particular IF, I thought my test class should cover it, but it doesn't! I'm guessing I need to cover some other variable, but I can't for the life of me work out what it is! Anyone that can point out where I'm going wrong would be my personal hero!

Here is the code I'm trying to test:
(I've put my existing code coverage in bold)
List<Payment_Records__c> lstPayment=new List<Payment_Records__c>();
Map<Id,Opportunity> mapoppId=new Map<Id,Opportunity>(); 
List<String> lstprogramTypes=new List<String>();
       
        for(Opportunity opp : opps){
            if(opp.RecordtypeId == schoolRecTypeId || opp.RecordtypeId == fundraisingRecTypeId || opp.RecordtypeId == orgRecTypeId){
                if(!opp.Payment_records_created__c){
                    Payment_Records__c precord = new Payment_Records__c();
                    if(opp.No_of_Years__c == '1'){
                        precord.Amount__c = opp.Amount;
                        if(opp.RecordtypeId == schoolRecTypeId || opp.RecordtypeId == orgRecTypeId){
                            precord.Payment_year__c = opp.Year__c;
                        }
                    }
                    precord.Opportunity__c =opp.Id;
                    lstPayment.add(precord);
                    opp.Payment_records_created__c =true;   
                }              
                else if(opp.Payment_records_created__c==True && oldMap.get(opp.Id).Amount != opp.Amount && opp.No_of_Years__c == '1'){
                    mapOppId.put(opp.Id,opp);
                }
            }       
            if(opp.RecordtypeId == schoolRecTypeId || opp.RecordtypeId== orgRecTypeId){
                if(opp.Package_Prog__c != null){
                    lstprogramTypes.add(opp.Package_Prog__c);
                }
            }
        }
And my test class:
(sorry if it's a mess, I've been refactoring this for well over an hour!)
@isTest static void testpaymentrecordcreation() {
        List<Payment_Records__c> lstPayment=new List<Payment_Records__c>();
        List<Payment_Records__c> new_payments = new List<Payment_Records__c>();
        Id schoolRecTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('School Opportunity').getRecordTypeId();
        Id orgRecTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('Organisation Opportunity').getRecordTypeId();
        Id fundraisingRecTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('Fundraising').getRecordTypeId();
        Id recTypeId=Schema.SobjectType.Opportunity.getRecordTypeInfosByName().get('Business Partner Opportunity').getRecordTypeId();
        
        Account acc = new Account(Name='APtest');
        Insert acc;
        
        Programme__c prog = new Programme__c(School__c = acc.Id);
        insert prog;
        
        Programme_Type__c progType = new Programme_Type__c(name = 'Bespoke');
        Insert progtype;
        
        Opportunity opp = new Opportunity (AccountId = acc.Id, Name = 'APtestOpportunity', CloseDate = system.today(),
            StageName = 'Prospecting', Programme__c = prog.Id, Payment_records_created__c = True,
            Amount = 100, Year__c = '2016', No_of_Years__c = '1', RecordTypeId = orgRecTypeId);
        Insert opp;
        
        Payment_Records__c precord = new Payment_Records__c(Opportunity__c = opp.Id, Payment_year__c = opp.Year__c, Amount__c = opp.Amount);
        Insert precord;
            
        Test.StartTest();
        	system.assertNotEquals(False, opp.Payment_records_created__c);
            system.assert(opp.No_of_Years__c == '1');
            system.assert(opp.RecordtypeId == '0120O000000dSyq');
            system.assert(precord.Opportunity__c == opp.Id);
            system.assert(precord.Amount__c == opp.amount);
            system.assert(precord.Payment_year__c == opp.Year__c);
        Test.StopTest();
    }

Again, if you can point out where I'm going wrong, you'd be my personal hero!

A.
I am trying to update an existing code in my org to do some zip code lookup. We have an existing visualforce page with a field that asks for a zip code. I want to convert this to a lookup field that returns a list of City and State combination (example: Boston, MA) matching the zip code in my zip code custom object (ZipCode__c) and populates the City and State fields when user selects the city/state combination.

I read that I can use actionSupport to do this. I have very little experience in apex/vf so I hope somebody can help with the code.

Here's my org's existing VF page code for zip code input:
<apex:pageBlockSectionItem > 
    PG Zip: <apex:inputtext value="{!PGZip}"/> 
</apex:pageBlockSectionItem>
In the controller, the PGZip and the city and state fields are declared/saved like below:
public String PGZip{get;set;} 
PG_ct.MailingPostalCode = PGZip; 
myPageCon.PGZip = '21345'; 

public String PGCity{get;set;} 
PG_ct.MailingCity = PGCity; 
myPageCon.PGCity = 'Boston'; 

public String PGState{get;set;} 
PG_ct.MailingState = PGState; 
myPageCon.ECState = 'MA';