• Rushita Bavishi 1
  • NEWBIE
  • 40 Points
  • Member since 2019

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 9
    Replies
Hello Folks

i am new to the apex development, i m trying to write test class for the below apex class but not able to do the code coverage, Any help will be appreciated

Apex class
 
public class AggregateApprovalHelper {
    public static void updateOpportunities(Map<Id,Aggregate_Approval__c> newMap, Map<Id,Aggregate_Approval__c> oldMap){
        List<Id> approvedAggregateIds = new List<Id>();
        Map<Id, Aggregate_Approval__c> apprMap = new Map<Id, Aggregate_Approval__c>(); 
        for(Id appId : newMap.keySet()){
            if(newMap.get(appId).Status__c != oldMap.get(appId).Status__c && (newMap.get(appId).Status__c == 'Approved') || newMap.get(appId).Status__c == 'Rejected'){
                approvedAggregateIds.add(appId);
                apprMap.put(appId,newMap.get(appId));
            }
        }
        if(apprMap.size()>0){
       //     BatchUpdateOpportunities bat = new BatchUpdateOpportunities(apprMap);
       //     Database.executeBatch(bat);
        }
        if(approvedAggregateIds.size()>0){
            List<Opportunity> opps = [Select Id, StageName, Aggregate_Approval__r.Status__c from Opportunity where Aggregate_Approval__c IN :approvedAggregateIds];
            List<Opportunity> oppsToBeApproved = new List<Opportunity>();
            for(Opportunity o : opps){
               
                o.Status__c = o.Aggregate_Approval__r.Status__c;
                if(o.Aggregate_Approval__r.Status__c == 'Approved')
                	o.Billing_To_Be_Initiated__c ='Yes';
                else if(o.Aggregate_Approval__r.Status__c == 'Rejected')
                    o.Billing_To_Be_Initiated__c ='No';
                oppsToBeApproved.add(o);
            }
            if(oppsToBeApproved.size()>0){
                 List<Approval.UnlockResult> ur = Approval.unlock(oppsToBeApproved);
                update oppsToBeApproved;
            }
        }
    }
}

My Test class 
 
@isTest
public class AggregateApprovalTestclass {
    
    static testmethod void Method(){

    List<Aggregate_Approval__c> AggreApp = new list <Aggregate_Approval__c>();
    Aggregate_Approval__c Agg = new Aggregate_Approval__c(); 
   
    Agg.Month__c = 'October';
    Agg.Next_Month_Start_Date__c = system.today();
    Agg.Status__c = 'Created';
    Agg.Year__c = '2020';   
    AggreApp.add(Agg);
    insert Agg;
        
    }
    
    static testmethod void Method1(){ 
        List<Opportunity> Oppps = new list <Opportunity>();  
        Opportunity oop = new Opportunity(); 
        Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;
        User u = [Select id,name from user where isactive=true Limit 1];
        oop.Name = 'Test1';
        oop.AccountId = testAcct.ID;
        oop.StageName = 'Demo';
        oop.CloseDate = system.today();
        oop.LeadSource = 'Customer';
        oop.Country__c = 'India';
        Oppps.add(oop);
        insert oop;
    }
}

 
i am writing test class for below apex class. i am new to the apex coding.can anyone please help on this.

Apex class:

public class ContactsapexController {
    
    @AuraEnabled
    public static List<Account> getAccounts(){
        return [select id, Name, description,Owner.Name,Phone from Account];
    }
    
    @AuraEnabled
    public static List<Contact> getContacts(string accountId){
        list<contact> conlist=new list<contact>();
        system.debug(accountId);
        
        list<contact> con=[select id, Name, Title, Email, Phone, Account.Owner.Name from Contact where accountId =: accountId];
        for(contact c:con){
            conlist.add(c);
        }
        return conlist;
    }
    
    @AuraEnabled
    public static List<String> deleteRecords(List<contact> lstId){
        system.debug('lstId'+lstId);
        // for store Error Messages  
        List<String> oErrorMsg = new List<String> ();
        
        List<Contact> contacts = [select id from Contact where id in : lstId];
        Database.DeleteResult[] DR_Dels = Database.delete(contacts, false);
        for (Database.DeleteResult dr: DR_Dels){
            if (dr.isSuccess()) {
                system.debug('successful delete contact');
                // Operation was successful
            } else {
                // Operation failed, so get all errors   
                for (Database.Error err: dr.getErrors()) {
                // add Error message to oErrorMsg list and return the list
                    oErrorMsg.add(err.getStatusCode() + ': ' + err.getMessage());
                }
            }
        }
        return oErrorMsg;
    }
}
Hi All,
         I have written a test class for trigger helper. Can anyone help me with the test coverage as it's not covering 75 %?
 My helper class is given below:-

// Helper class for Trigger_Registration
public without sharing class RegistrationTriggerHelper {

    public static List<rie__Registration__c> newRegistration = new List<rie__Registration__c>();
    public static List<rie__Registration__c> oldRegistration = new List<rie__Registration__c>();    
    public static Map<Id, rie__Registration__c> newMapRegistration = new Map<Id, rie__Registration__c>();
    public static Map<Id, rie__Registration__c> oldMapRegistration = new Map<Id, rie__Registration__c>(); 
    
    public static boolean runTrigger = TRUE;
    
    // method to link the contact and registration
    public static void linkContact(){
    
        List<rie__Registration__c> applicableRegistrations = new List<rie__Registration__c>();
        Set<String> emails = new Set<String>();
        for( rie__Registration__c regObj : newRegistration ) {
            if( regObj.rie__Email__c != Null ) {
               emails.add( regObj.rie__Email__c ); 
               applicableRegistrations.add( regObj ); 
            }
        }
        
        if( applicableRegistrations.size() >  0 ) {
            Map<String, Contact> mapEmailToContact = new Map<String, Contact>();    
            List<Contact> contacts = new List<Contact>([Select Id, Email 
                                                                   From Contact 
                                                                   Where Email IN :emails
                                                                   Order By CreatedDate DESC
                                                                   ]);
            if( contacts != Null && contacts.size() > 0 ){
                for( Contact con : contacts ){
                    String mapKey = con.Email.toLowerCase();
                    if( !mapEmailToContact.containsKey( mapKey )) {
                        mapEmailToContact.put( mapKey, con );  
                    }
                }
            }
        
            List<rie__Registration__c> registrationNeedsNewContact = new List<rie__Registration__c>();
            Map<Id, rie__Registration__c> registrationToUpdate = new Map<Id, rie__Registration__c>();
            Map<Id, Contact> contactsToUpsert = new Map<Id, Contact>();
            
            for(rie__Registration__c regObj : applicableRegistrations){
                String emailLowerCase = regObj.rie__Email__c.toLowerCase();
                if( mapEmailToContact.containsKey( emailLowerCase )){
                    rie__Registration__c regObjToUpdate = new rie__Registration__c( Id = regObj.Id );
                    regObjToUpdate.rie__Contact__c = mapEmailToContact.get(emailLowerCase).Id; 
                    registrationToUpdate.put( regObjToUpdate.Id, regObjToUpdate );
                    
                    Contact conToUpdate  = new Contact( Id = regObjToUpdate.rie__Contact__c );
                    conToUpdate.FMS_2019_Registration__c = regObjToUpdate.Id;
                    contactsToUpsert.put( regObjToUpdate.Id, conToUpdate );
                }
                else {
                    registrationNeedsNewContact.add( regObj );
                }
            }
            
            List<Contact> contactToInsert = new List<Contact>();
            for(rie__Registration__c regObj : registrationNeedsNewContact ){
                Contact con = new Contact();
                con.LastName = regObj.rie__Last_Name__c;
                con.Email = regObj.rie__Email__c;
                con.FMS_2019_Registration__c = regObj.Id;
                contactsToUpsert.put(regObj.Id, con);
                contactToInsert.add( con );
            }
            
            if( contactsToUpsert.size() > 0 ) {
                upsert contactsToUpsert.values();
                for( Contact con : contactToInsert ) {
                    rie__Registration__c regObjToUpdate = new rie__Registration__c( Id = con.FMS_2019_Registration__c );
                    regObjToUpdate.rie__Contact__c = con.Id; 
                    registrationToUpdate.put( regObjToUpdate.Id, regObjToUpdate );
                }
            }
            
            if( registrationToUpdate.size() > 0 ) {
                RegistrationTriggerHelper.runTrigger = false;
                update registrationToUpdate.values();
                RegistrationTriggerHelper.runTrigger = true;
            }
        }
    }    
}
Test class is given below:-
//Test class for RegisterTriggerHelper
@isTest
public class RegistrationTriggerHelperTest {
    public static testMethod void linkContactTest(){
        Contact con = new Contact(LastName = 'Scot');
        insert con;
        rie__Event__c ev = new rie__Event__c();
         
        Insert ev;
        rie__Registration__c regObj  = new rie__Registration__c();
        regObj.rie__Event__c =   ev.id;
        insert regObj;
        
    }
}
Please help me with the coverage.
 
Hi,

I have a scenario 
1.  On editing if the city is changed to Ahmedabad.
I have written the below formula, action is only getting fired on creation of a new record but when i am editing by changing the city to Ahmedabad it is not working.
2. While creating a record on Account, i need to check whether city and State both are not blank.

OR
(
AND
(ISNEW(),
NOT(ISBLANK([Account].City__c)),
NOT(ISBLANK([Account].State__c))
),
AND
(
ISCHANGED([Account].City__c),
IF(([Account].City__c) == 'Ahmedabad', true, false)
)
)


Can anyone help me on this?

Rgds,
Vai


 
How to write validation for when lead status is working then mobile or email is mandatory?

I wrote this validation like this:
--------------------
AND((ISPICKVAL(Status ,"Working"))
OR(
ISBLANK(Email),ISBLANK( MobilePhone )
)
)
---------
But nor working , it is asking for both
 
Hello Developers

i am trying to design a Test class for the Apex class but i am not able to do the code coverage of the following apex class, can anyone please help
 
public class FutureHandler {
    
    @future
    public static void updaterecs(Map<String,Id> oldManagerQuotaMap1){
        Map<Id,Quota__c> oldMapQuota = new Map<Id,Quota__c>();
        if(null != oldManagerQuotaMap1 && oldManagerQuotaMap1.size() > 0){
            for (aggregateResult result: [Select Manager_Quota__c, Sum(Inside_Sales_Roll_Up__c) 
                                          FROM Quota__c WHERE 
                                          Manager_Quota__c IN: oldManagerQuotaMap1.values() 
                                          GROUP BY Manager_Quota__c]) {
                                              system.debug('result'+result)    ;          
                                              oldMapQuota.put((Id)result.get('Manager_Quota__c'),new Quota__c(Id=(Id)result.get('Manager_Quota__c'),Actual_Amount__c =(Decimal)result.get('expr0')));         
                                          }
            
            for(Id qId : oldManagerQuotaMap1.values()){
                if(!oldMapQuota.containskey(qId)){
                    oldMapQuota.put(qId, new Quota__c(Id = qId,Actual_Amount__c = 0.0));
                }
            }
            
            if(null != oldMapQuota && oldMapQuota.size() > 0){
                update oldMapQuota.values();
            }
            
        }
    }

    public static void sendOppsForApproval(List<Opportunity> opps){
        
        List<Opportunity> oppsToBeSentForApproval = new List<Opportunity>();
        String monthYear = System.now().format('MMMM-yyyy');
        String month = monthYear.split('-')[0];
        String year = monthYear.split('-')[1];
        Date nextMonthStart = System.today().toStartOfMonth().addMonths(1).toStartOfMonth();
        List<Aggregate_Approval__c> appr = [Select Id from Aggregate_Approval__c where Month__c=:month And Year__c=:year]; 
        Id approvalId;
        if(appr.size()>0){
            approvalId = appr[0].Id;
            
            for(Opportunity opp : opps){
                opp.Aggregate_Approval__c = approvalId;
                opp.Status__c = 'In Approval';
                oppsToBeSentForApproval.add(opp);
            }
            update oppsToBeSentForApproval;
           // List<Approval.LockResult> li = Approval.lock(opps);
            
            Approval.ProcessSubmitRequest oppAggregateApproval = new Approval.ProcessSubmitRequest();
            oppAggregateApproval.setComments('Submitting opportunities for Approval for '+System.now().format('MMMM-yyyy'));
            oppAggregateApproval.setObjectId(approvalId);
            Approval.ProcessResult approvalResult = Approval.process(oppAggregateApproval); 
            
        } 
    }
}
My Test Class
 
@isTest
public class FutureHandlerTestclass {
    
    static testmethod void Futuretest(){
        
        List<Quota__c> Ftur = new list <Quota__c>();
        Quota__c FF = new Quota__c();
        FF.Name='Julian';
        FF.Assigned_To__c = userinfo.getUserId();
        FF.Month__c='March';  
        FF.Quater__c='Q1';       
        FF.Quater_Year__c = '2020';
        FF.Actual_Amount__c = 100;
        FF.Unique_Identifier_Per_Quater_Per_User__c = 'Hello';
        Ftur.add(FF);
        insert FF;
        
        
        Quota__c FF1 = new Quota__c();
        FF1.Name='rahul';
        FF1.Assigned_To__c = userinfo.getUserId();
        FF1.Month__c='October';  
        FF1.Quater__c='Q4';       
        FF1.Quater_Year__c = '2030';
        FF.Actual_Amount__c = 200;
        FF1.Unique_Identifier_Per_Quater_Per_User__c = 'Helloworld';
        Ftur.add(FF1);
        insert FF1;
        
        Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;
        
        User u = [Select id,name from user where isactive=true Limit 1];
        
        List<Opportunity> Oppps = new list <Opportunity>(); 
        Opportunity oop = new Opportunity();
        
        oop.Name = 'Test1';
        oop.AccountId = testAcct.ID;
        oop.StageName = 'Demo';
        oop.CloseDate = system.today();
        oop.Billing_To_Be_Initiated__c = 'Yes';
        oop.LeadSource = 'Customer';
        oop.Country__c = 'India';        
        Oppps.add(oop);
        insert oop;
    }
    }

 
Hello Folks

i am new to the apex development, i m trying to write test class for the below apex class but not able to do the code coverage, Any help will be appreciated

Apex class
 
public class AggregateApprovalHelper {
    public static void updateOpportunities(Map<Id,Aggregate_Approval__c> newMap, Map<Id,Aggregate_Approval__c> oldMap){
        List<Id> approvedAggregateIds = new List<Id>();
        Map<Id, Aggregate_Approval__c> apprMap = new Map<Id, Aggregate_Approval__c>(); 
        for(Id appId : newMap.keySet()){
            if(newMap.get(appId).Status__c != oldMap.get(appId).Status__c && (newMap.get(appId).Status__c == 'Approved') || newMap.get(appId).Status__c == 'Rejected'){
                approvedAggregateIds.add(appId);
                apprMap.put(appId,newMap.get(appId));
            }
        }
        if(apprMap.size()>0){
       //     BatchUpdateOpportunities bat = new BatchUpdateOpportunities(apprMap);
       //     Database.executeBatch(bat);
        }
        if(approvedAggregateIds.size()>0){
            List<Opportunity> opps = [Select Id, StageName, Aggregate_Approval__r.Status__c from Opportunity where Aggregate_Approval__c IN :approvedAggregateIds];
            List<Opportunity> oppsToBeApproved = new List<Opportunity>();
            for(Opportunity o : opps){
               
                o.Status__c = o.Aggregate_Approval__r.Status__c;
                if(o.Aggregate_Approval__r.Status__c == 'Approved')
                	o.Billing_To_Be_Initiated__c ='Yes';
                else if(o.Aggregate_Approval__r.Status__c == 'Rejected')
                    o.Billing_To_Be_Initiated__c ='No';
                oppsToBeApproved.add(o);
            }
            if(oppsToBeApproved.size()>0){
                 List<Approval.UnlockResult> ur = Approval.unlock(oppsToBeApproved);
                update oppsToBeApproved;
            }
        }
    }
}

My Test class 
 
@isTest
public class AggregateApprovalTestclass {
    
    static testmethod void Method(){

    List<Aggregate_Approval__c> AggreApp = new list <Aggregate_Approval__c>();
    Aggregate_Approval__c Agg = new Aggregate_Approval__c(); 
   
    Agg.Month__c = 'October';
    Agg.Next_Month_Start_Date__c = system.today();
    Agg.Status__c = 'Created';
    Agg.Year__c = '2020';   
    AggreApp.add(Agg);
    insert Agg;
        
    }
    
    static testmethod void Method1(){ 
        List<Opportunity> Oppps = new list <Opportunity>();  
        Opportunity oop = new Opportunity(); 
        Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;
        User u = [Select id,name from user where isactive=true Limit 1];
        oop.Name = 'Test1';
        oop.AccountId = testAcct.ID;
        oop.StageName = 'Demo';
        oop.CloseDate = system.today();
        oop.LeadSource = 'Customer';
        oop.Country__c = 'India';
        Oppps.add(oop);
        insert oop;
    }
}

 
<apex:page controller="OppsController">
<apex:form>
<apex:dataTable value="{!OpportunitiesWithIndex}" var="oppWrapped">
<apex:column>
<apex:facet name="header">Opportunity</apex:facet>
<apex:outputField value="{!oppWrapped.opp.name}"/>
</apex:column>
<apex:column>
<apex:facet name="header">Amount</apex:facet>
<apex:inputField value="{!oppWrapped.opp.amount}"
tabindex="{!oppWrapped.tabIndex}"/>
</apex:column>
</apex:dataTable>
</apex:form>
</apex:page>
plz help , I am getting this error([Error] Error: Unsupported attribute tabindex in <apex:inputField> in tabbedAccount at line 11 column 36)