• Neha Kakroo
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 11
    Questions
  • 6
    Replies

I am writing a test class for a trigger that tries to identify account duplicates in the system based on Account Name, Street & Phone matching or Name & Phone matching or Name & Street matching. The test class that I have written is giving me 100% coverage, but I am getting 1 test failure:

 

Message: System.AssertException: Assertion Failed: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, <BR/>Agency record with this Name/Phone/Physical Street exists in the system. If you wish to create agency record, change field value of "Create New Record" to YES.<BR/><BR/> Potential Duplicate Agencies Include:<BR/>Agencies with Name, Phone & Physical Street match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> | <BR/>Agencies with Name & Phone match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> | <BR/>Agencies with Phone & Physical Street match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> | <BR/>Agencies with Name & Physical Street match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> |

 

Stack Trace: Class.Test_trg_AccountDuplicatePreventer_Final.Test0_TestInsertWithValue: line 18, column 1

 

Test Class is:

 

@isTest(seeAllData = true)

public class Test_trg_AccountDuplicatePreventer_Final{
static testMethod void Test0_TestInsertWithValue()
{

//Set<Account> Accset = new Set<Account>();

Account acc1 = new Account(Is_record_near_to_duplicate__c='No', Name = 'Agency0', Phone='(981) 130-9977',Physical_Street__c = 'ABC0', Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');
Account acc2 = new Account(Is_record_near_to_duplicate__c='No', Name = 'Agency00', Phone='(981) 130-9988',Physical_Street__c = 'ABC00', Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');
Account acc3 = new Account(Is_record_near_to_duplicate__c='No', Name = 'Agency000', Phone='(981) 130-9999',Physical_Street__c = 'ABC000', Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

Account[] accs = new Account[]{acc1,acc2,acc3};
try{
insert accs;
}catch(DMLException ex)
{
System.assert(ex.getMessage().contains('Insert failed. First exception on'+ 'row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION,'+'<BR/>Agency record with this Name/Phone/Physical Street exists in the system. If you wish to create agency record, change field value of "Create New Record" to YES.<BR/><BR/> Potential Duplicate Agencies Include:<BR/>Agencies with Name, Phone & Physical Street match:<a href=https://cs14.salesforce.com/accs.id>accs.Name</a>'),
ex.getMessage());
}


acc2.Phone='(981) 130-7777';
acc3.Physical_Street__c='ABC0000';
acc3.Phone='(981) 130-8888';
update accs;

Account dupe1 = new Account(Name = 'Agency0', Phone='(981) 130-9977',Physical_Street__c = 'ABC0',
Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

try{
insert dupe1;
System.assert(false);
}catch(DMLException e){
for (Integer i = 0; i < e.getNumDml(); i++)
{
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(i) == 0);
System.assert(e.getDmlFields(i).size() == 3);
System.assert(e.getDmlFields(i)[0] == Account.Name);
System.assert(e.getDmlFields(i)[1] == Account.Phone);
System.assert(e.getDmlFields(i)[2] == Account.Physical_Street__c);
System.assert(e.getDmlMessage(i).indexOf('An account with this name, phone, street already exists.') > -1);
}
}

Account dupe2 = new Account(Name = 'Agency00', Phone='(981) 130-9988',Physical_Street__c = 'ZZZ',
Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

try{
insert dupe2;
System.assert(false);
}catch(DMLException e){
for (Integer i = 0; i < e.getNumDml(); i++)
{
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(i) == 0);
System.assert(e.getDmlFields(i).size() == 3);
System.assert(e.getDmlFields(i)[0] == Account.Name);
System.assert(e.getDmlFields(i)[1] == Account.Phone);
System.assert(e.getDmlFields(i)[2] == Account.Physical_Street__c);
System.assert(e.getDmlMessage(i).indexOf('An account with this name, phone already exists.') > -1);
}
}

dupe1 = new Account(Id=acc1.id, Name = 'Agency0', Phone='(981) 130-0000',Physical_Street__c = 'ABC0',
Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

update dupe1;
System.assert(false);
}
}

 

 

Can someone pls tell me why this test failure?

 

Can somebody tell me how can we display an alert message (not error message) through the trigger. I have written a trigger that checks for duplicate accounts in the system. At present my trigger gives the error message to the user telling that there is a duplicate account. But if the user changes the value of a field "Is record near to duplicate" to YES, the trigger allows the user to save the record.

But, I want to display the error message in an alert pop up box like "Account with this Name exists,are you sure you want to continue" and then user clicks Yes and the record gets created. Any thoughts on how I can do this.

 

 
 

Hi,

 

Does anyone have experience of creating a report on Opportunities along with the Approval History fields like Date, Approved By, Approval Status etc.

 

I have a requirement where I need to pull the Approval Dates for Opportunity in a report (there is an approval process for oppotunity object)

 

Pls let me know how can we generate a report like this?

 

Thanks!

Can somebody pls tell me hoe can I cobe the sort method in the test class;

 

public List<SelectOption> Sort(List<SelectOption> lstToSort)
    {
        System.Debug('Original List: ' + lstToSort);
        List<SelectOption> returnList = new List<SelectOption>();
        try
        {
            // Convert SelectOption list To String List
            List<String> convertedList = new List<String>();
            for(SelectOption opt: lstToSort)
            {
                convertedList.add(opt.getLabel()); 
            }
    
            convertedList = sortList(convertedList);
            System.Debug('Convert and Sorted: ' + convertedList);
    
            // Convert String List to SelectOption list
            for(String strOpt: convertedList)
            {
                returnList.add(new SelectOption(strOpt,strOpt)); 
            }
            System.Debug('Returning: ' + returnList);
        }
        catch(Exception exp)
        {
            System.Debug('Exception in Sort(): ' + exp.getMessage());
        }
        return returnList;
    }

Hi,

 

I am not sure how to test this code. I dont know how to use the SelectOption in the test class. Can somebody tell me how should i write it??

 

 

public with sharing class ctrl_MultiSelectPickList 
{
    public String Label{get; set;}
    
    public List<String> selected{get; set;}
    public List<String> deselected{get; set;}

    public SelectOption Available { get; set; }
    public SelectOption Chosen { get; set; }
    
    public List<SelectOption> selectedOptions = null;
    public List<SelectOption> AllOptions = null;

    // A Reference list (all options)
    List<Options> opt{get; set;}
 
    public List<SelectOption> getselectedOptions()
    {
        return selectedOptions;
    }
    public void setselectedOptions(List<SelectOption> value)
    {
        selectedOptions = value;
    }

    public List<SelectOption> getAllOptions()
    {
        return AllOptions;
    }
    
    public void setAllOptions(List<SelectOption> value)
    {
        AllOptions = value;
    }


    public class Options
    {
        public Integer Index{get; set;}
        public String Item{get; set;}
    }

public with sharing class ctrl_MultiSelectPickList 
{
    public String Label{get; set;}
    
    public List<String> selected{get; set;}
    public List<String> deselected{get; set;}

    public SelectOption Available { get; set; }
    public SelectOption Chosen { get; set; }
    
   

public with sharing class ctrl_MultiSelectPickList 
{
    public String Label{get; set;}
    
    public List<String> selected{get; set;}
    public List<String> deselected{get; set;}

    public SelectOption Available { get; set; }
    public SelectOption Chosen { get; set; }
    
    private List<SelectOption> selectedOptions = null;
    private List<SelectOption> AllOptions = null;

    // A Reference list (all options)
    List<Options> opt{get; set;}
 
    public List<SelectOption> getselectedOptions()
    {
        return selectedOptions;
    }
    public void setselectedOptions(List<SelectOption> value)
    {
        selectedOptions = value;
    }

    public List<SelectOption> getAllOptions()
    {
        return AllOptions;
    }
    
    public void setAllOptions(List<SelectOption> value)
    {
        AllOptions = value;
    }


    private class Options
    {
        public Integer Index{get; set;}
        public String Item{get; set;}
    }

    public ctrl_MultiSelectPickList()
    {
        System.Debug('Amritaa Calling constructor ctrl_MultiSelectPickList()');
        selected = new List<String>();
        deselected = new List<String>();
        
        Available = new SelectOption('Available','Available', true);
        Chosen = new SelectOption('Chosen','Chosen', true);
        
         // A map to store all competitors 
        List<Competitor__c> mapCompetitor = new List<Competitor__c>([SELECT Name FROM Competitor__c ORDER BY Competitor__c.Name ASC]);
        Integer i=0;
        opt = new List<Options>();
        for(Competitor__c comp : mapCompetitor)
        {
            Options tempOpt = new Options();
            tempOpt.Index = i++;
            tempOpt.Item = comp.Name;
            opt.add(tempOpt);
        }    
        
        //System.Debug('Amritaa Exiting constructor ctrl_MultiSelectPickList()');
        
    }

    public void Selecting() 
    {    
        //System.Debug('Amritaa Calling Selecting()');
        //System.Debug('Amritaa selected is : ' + selected);
        //Amrita selected : (Compensation Strategies, Inc., HMX, Conrad Siegel Actuaries, FPL Advisory)
        try
        {
            for(String toSelect: selected) 
            {  
                Integer i = 0;  
                While(i < AllOptions.size()) 
                {  
                    if(AllOptions[i].getvalue()==toSelect) 
                    {   
                        selectedOptions.add(new SelectOption(toSelect,toSelect));   
                        AllOptions.remove(i);
                        break;  
                    }                      
                    i++;  
                } 
            }
            selectedOptions = Sort(selectedOptions); 
            System.Debug('Amritaa chosen box has : ' + selectedOptions);
        }
        catch(Exception exp)
        {
            System.Debug(exp.getMessage());
        }
        //System.Debug('Amritaa Exiting Selecting()');
    }

    public void Deselecting() 
    {  
        //System.Debug('Amritaa Calling Deselecting()');
        //System.Debug('Amritaa de-selecting is : ' + deselected);

        for(String toDeselect: deselected) 
        {              
            Integer i = 0;  
            While(i < selectedOptions.size()) 
            {  
                if(selectedOptions[i].getvalue() == toDeselect) 
                {   
                    AllOptions.add(new SelectOption(toDeselect, toDeselect));   
                    selectedOptions.remove(i);  
                    break;
                }                      
                i++;  
            } 
        }
        AllOptions = sort(AllOptions);  
        //selectedOptions = SortOptionList(selectedOptions); 
        
        //System.Debug('Amritaa Exiting Deselecting()');
    } 
    //Arun
    private List<String> sortList(List<String> competitors) 
    {
        Map<String, String> interimMap = new Map<String, String>();
        List<String> interimList = new List<String>();
        
        for(String comp : Competitors)
        {
            interimMap.put(comp.toUpperCase(), comp);
            interimList.add(comp.toUpperCase());
        }

        interimList.sort();
    
        // purge the initial competitors list
        competitors.clear();
                    
        for(String comp : interimList){
            competitors.add(interimMap.get(comp));
        }
                    
        return competitors;
    }

    //---Arun
    
    public List<SelectOption> Sort(List<SelectOption> lstToSort)
    {
        System.Debug('Original List: ' + lstToSort);
        List<SelectOption> returnList = new List<SelectOption>();

        // Convert SelectOption list To String List
        List<String> convertedList = new List<String>();
        for(SelectOption opt: lstToSort)
        {
            //convertedList.add((opt.getLabel()).toUpperCase()); 
            convertedList.add(opt.getLabel()); 
        }

        
        //convertedList.sort();
        convertedList = sortList(convertedList);
        System.Debug('Convert and Sorted: ' + convertedList);

        // Convert String List to SelectOption list
        for(String strOpt: convertedList)
        {
            returnList.add(new SelectOption(strOpt,strOpt)); 
        }
        System.Debug('Returning: ' + returnList);
        return returnList;
    }
    
    // A method to sort the list of "SelectOption"
    public static List<SelectOption> SortOptionList(List<SelectOption> ListToSort)
    {
        //System.Debug('Amritaa Calling POC SortOptionList()');
    
        if(ListToSort == null || ListToSort.size() <= 1)
        {
            return ListToSort;
        }
            
        List<SelectOption> Less = new List<SelectOption>();
        List<SelectOption> Greater = new List<SelectOption>();
        integer pivot = 0;
        
        // save the pivot and remove it from the list
        SelectOption pivotValue = ListToSort[pivot];
        ListToSort.remove(pivot);
        
        for(SelectOption opt : ListToSort)
        {
            if(opt.getLabel() <= pivotValue.getLabel())
            {
                Less.add(opt);
            }
            else if(opt.getLabel() > pivotValue.getLabel())
            {
                Greater.add(opt);
            }
        }
        List<SelectOption> returnList = new List<SelectOption> ();
        returnList.addAll(SortOptionList(Less));
        returnList.add(pivotValue);
        returnList.addAll(SortOptionList(Greater));
        //System.Debug('Amritaa Exiting POC SortOptionList()');
        
        return returnList; 
    }
    
}

Hi can somebody help me in writting a test class for the code below:

 

 

global class DailyChangeHistoryMailer implements Schedulable {
    global void execute(SchedulableContext SC) {
        Database.executeBatch(new ChangeHistoryMailer()); 
    }
}



Hi I am new to writing test classes. My code coverage is only 65%. Can somebody pls help.

 

The class is:

public class customObjectController{


    public static Integer indexGen {get; set;} //index generator
    public List<CustomObjectWrapper> coList;
    public Integer numRows {get; set;}

    
    /*
     * Wrapper class for holding a 'CustomObjects__c' record and its row index together.
     * The row index is used to delete the row using commandLink
     */
    private class CustomObjectWrapper {
        private CustomObjects__c customObject;
        private Integer index;
     
     
     public CustomObjectWrapper() {
            this.customObject = new CustomObjects__c();
            this.index = indexGen++;
        }
        
        public CustomObjects__c getCustomObject() {
            return customObject;
        }
        
        public Integer getIndex() {
            return index;
        }
    }
        
    /*
     * Constructor
     * -----------
     * Initializes 'coList' and 'numRows'
     */
    public customObjectController(ApexPages.StandardController controller) {
        if(indexGen == null) indexGen = 1;
        coList = new List<CustomObjectWrapper>();
        numRows = 1;
    }
    public List<CustomObjectWrapper> getCoList() {
        return coList;
    }


     
    /*
     * upserts all records currently in the 'coList'
     */
    public PageReference save() {
        if(coList.size()==0)
            return new PageReference('/' + ApexPages.currentPage().getParameters().get('retURL'));
        try {
            List<CustomObjects__c> tempList = new List<CustomObjects__c>();
            for(Integer i=0; i<coList.size(); ++i)
                tempList.add(coList[i].getCustomObject());
            upsert(tempList);
            return new PageReference('/' + ApexPages.currentPage().getParameters().get('retURL'));
        } catch (System.DMLException ex) {
            ApexPages.addMessages(ex);
            return null;
        }
    }
    
    
    
     /*
     * appends new records to the 'coList'.
     * The number of records added is determined by the value of 'numRows'
     * Issues with the method: Not working if mandatory fields are left empty
     */
    public void add() {
          try {
            if(numRows > 0)
                for(Integer i=0; i<numRows; ++i)
                    coList.add(new CustomObjectWrapper());
        } catch (Exception ex) {
             ApexPages.addMessages(ex);
        }
   }
    
    
    /*
     * Clears all records from the 'coList'
     * Issues with the method: Not working if mandatory fields are left empty
     */        
    public void clear(){
        coList.clear();
        numRows = 1;
         }
     

    /*
     * deletes a record from 'coList' depending on the 'index' of 'CustomObjects__c' within the 'CustomObjectWrapper' class
     */
     public void delRecords(){
        try {
            Integer delIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('delRow'));
            
            for(Integer i=0; i<coList.size(); ++i)
                if(coList[i].index == delIndex) {
                    coList.remove(i);
                    break;
                }
        } catch (Exception ex) {
            ApexPages.addMessages(ex);
        }
    }
    
       
             
   }

Test class;

 

@isTest

public class customObjectController_Test{

public static testMethod void testcls(){
CustomObjects__c cust = new CustomObjects__c();
PageReference pageRef = Page.ReportPage;
Test.setCurrentPageReference(pageRef);
ApexPages.StandardController sc = new ApexPages.StandardController(cust);
customObjectController obj = new customObjectController(sc);
customObjectController.CustomObjectWrapper wra = new customObjectController.CustomObjectWrapper();
obj.save();
obj.add();
obj.clear();
obj.delRecords();
wra.getIndex();
wra.getCustomObject();
}
}

Hi I am trying to write the test class for the below code but getting the above mentioned error. I guess this is due to the wrapper class being private. Can anybody pls help me with the test class.

 

The class is:

public class customObjectController{


    public static Integer indexGen {get; set;} //index generator
    public List<CustomObjectWrapper> coList;
    public Integer numRows {get; set;}

    
    /*
     * Wrapper class for holding a 'CustomObjects__c' record and its row index together.
     * The row index is used to delete the row using commandLink
     */
    private class CustomObjectWrapper {
        private CustomObjects__c customObject;
        private Integer index;
     
     
     public CustomObjectWrapper() {
            this.customObject = new CustomObjects__c();
            this.index = indexGen++;
        }
        
        public CustomObjects__c getCustomObject() {
            return customObject;
        }
        
        public Integer getIndex() {
            return index;
        }
    }
        
    /*
     * Constructor
     * -----------
     * Initializes 'coList' and 'numRows'
     */
    public customObjectController(ApexPages.StandardController controller) {
        if(indexGen == null) indexGen = 1;
        coList = new List<CustomObjectWrapper>();
        numRows = 1;
    }
    public List<CustomObjectWrapper> getCoList() {
        return coList;
    }


     
    /*
     * upserts all records currently in the 'coList'
     */
    public PageReference save() {
        if(coList.size()==0)
            return new PageReference('/' + ApexPages.currentPage().getParameters().get('retURL'));
        try {
            List<CustomObjects__c> tempList = new List<CustomObjects__c>();
            for(Integer i=0; i<coList.size(); ++i)
                tempList.add(coList[i].getCustomObject());
            upsert(tempList);
            return new PageReference('/' + ApexPages.currentPage().getParameters().get('retURL'));
        } catch (System.DMLException ex) {
            ApexPages.addMessages(ex);
            return null;
        }
    }
    
    
    
     /*
     * appends new records to the 'coList'.
     * The number of records added is determined by the value of 'numRows'
     * Issues with the method: Not working if mandatory fields are left empty
     */
    public void add() {
          try {
            if(numRows > 0)
                for(Integer i=0; i<numRows; ++i)
                    coList.add(new CustomObjectWrapper());
        } catch (Exception ex) {
             ApexPages.addMessages(ex);
        }
   }
    
    
    /*
     * Clears all records from the 'coList'
     * Issues with the method: Not working if mandatory fields are left empty
     */        
    public void clear(){
        coList.clear();
        numRows = 1;
         }
     

    /*
     * deletes a record from 'coList' depending on the 'index' of 'CustomObjects__c' within the 'CustomObjectWrapper' class
     */
     public void delRecords(){
        try {
            Integer delIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('delRow'));
            
            for(Integer i=0; i<coList.size(); ++i)
                if(coList[i].index == delIndex) {
                    coList.remove(i);
                    break;
                }
        } catch (Exception ex) {
            ApexPages.addMessages(ex);
        }
    }
    
       
             
   }

the test class that I have written is:

@isTest

public class customObjectController_Test{

public static testMethod void testcls(){
CustomObjects__c cust = new CustomObjects__c();
ApexPages.StandardController sc = new ApexPages.StandardController(cust);
customObjectController obj = new customObjectController(sc);
customObjectController.CustomObjectWrapper wra = new customObjectController.CustomObjectWrapper();
obj.save();
obj.add();
obj.clear();
obj.delRecords();
wra.getIndex();
wra.getcustomObject();
}
}



I have 2 custom objects on Contact. One is "Address" and the other object is a junction object
between Account & Contact. It is called "Account_Contact_Relationship__c".

Now whenever a user creates a new Account Contact Relationship object, the address of the
Account on this object should come as record under the "Address" related list on Contact Page.

I am writing the trigger as below but it is giving me Null Pointer exception at line 29 column 1.
(ac.Account__r.Marketting_City__c= imap.get(ac.Account__c).Marketting_City__c;)
Can somebody please help me with the trigger:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
trigger AddressUpdate on Account_Contact_Relationship__c (before insert,after insert) {

List<Address__c> addr = new List<Address__c>();
List<id> insertaid = new List<id>();
//List<id> updateaid = new List<id>();

if(trigger.isinsert)
{
for(Account_Contact_Relationship__c ac: Trigger.new)
{
insertaid.add(ac.Account__c);
}
system.debug(insertaid);
/*
if(trigger.isupdate)
{
for(Account_Contact_Relationship__c ac: Trigger.new)
{
updateaid.add(ac.accountid);
}
}*/
Map<id,Account> imap = new Map<id,Account>([Select Marketting_Street__c,Marketting_City__c,MarketingState__c,Marketting_PostalCode__c,ISO_Country_Code__c from Account where id IN: insertaid]);
//Map<id,Account> umap = new Map<id,Account>([Select Marketting_Street__c,Marketting_City__c,MarketingState__c,Marketting_PostalCode__c,ISO_Country_Code__c from Account where id IN: updateaid]);

if(trigger.isinsert)
{
for(Account_Contact_Relationship__c ac: Trigger.new)
{
ac.Account__r.Marketting_City__c= imap.get(ac.Account__c).Marketting_City__c;
ac.Account__r.Marketting_Street__c=imap.get(ac.Account__c).Marketting_Street__c;
}
}/*
if(trigger.isupdate)
{
for(Account_Contact_Relationship__c ac: Trigger.new)
{
ac.Account__r.Marketting_City__c= umap.get(ac.accountid).Marketting_City__c;
ac.Account__r.Marketting_Street__c=umap.get(ac.accountid).Marketting_Street__c;
}
}*/
for(Account_Contact_Relationship__c ac: Trigger.new)
{
if(trigger.isinsert)
{
Address__c a = new Address__c
(City__c=ac.Account__r.Marketting_City__c,Name=ac.Account__r.Marketting_Street__c);
addr.add(a);
}
}
insert(addr);
}
}
 



I am writing a test class for a trigger that tries to identify account duplicates in the system based on Account Name, Street & Phone matching or Name & Phone matching or Name & Street matching. The test class that I have written is giving me 100% coverage, but I am getting 1 test failure:

 

Message: System.AssertException: Assertion Failed: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, <BR/>Agency record with this Name/Phone/Physical Street exists in the system. If you wish to create agency record, change field value of "Create New Record" to YES.<BR/><BR/> Potential Duplicate Agencies Include:<BR/>Agencies with Name, Phone & Physical Street match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> | <BR/>Agencies with Name & Phone match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> | <BR/>Agencies with Phone & Physical Street match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> | <BR/>Agencies with Name & Physical Street match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> |

 

Stack Trace: Class.Test_trg_AccountDuplicatePreventer_Final.Test0_TestInsertWithValue: line 18, column 1

 

Test Class is:

 

@isTest(seeAllData = true)

public class Test_trg_AccountDuplicatePreventer_Final{
static testMethod void Test0_TestInsertWithValue()
{

//Set<Account> Accset = new Set<Account>();

Account acc1 = new Account(Is_record_near_to_duplicate__c='No', Name = 'Agency0', Phone='(981) 130-9977',Physical_Street__c = 'ABC0', Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');
Account acc2 = new Account(Is_record_near_to_duplicate__c='No', Name = 'Agency00', Phone='(981) 130-9988',Physical_Street__c = 'ABC00', Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');
Account acc3 = new Account(Is_record_near_to_duplicate__c='No', Name = 'Agency000', Phone='(981) 130-9999',Physical_Street__c = 'ABC000', Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

Account[] accs = new Account[]{acc1,acc2,acc3};
try{
insert accs;
}catch(DMLException ex)
{
System.assert(ex.getMessage().contains('Insert failed. First exception on'+ 'row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION,'+'<BR/>Agency record with this Name/Phone/Physical Street exists in the system. If you wish to create agency record, change field value of "Create New Record" to YES.<BR/><BR/> Potential Duplicate Agencies Include:<BR/>Agencies with Name, Phone & Physical Street match:<a href=https://cs14.salesforce.com/accs.id>accs.Name</a>'),
ex.getMessage());
}


acc2.Phone='(981) 130-7777';
acc3.Physical_Street__c='ABC0000';
acc3.Phone='(981) 130-8888';
update accs;

Account dupe1 = new Account(Name = 'Agency0', Phone='(981) 130-9977',Physical_Street__c = 'ABC0',
Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

try{
insert dupe1;
System.assert(false);
}catch(DMLException e){
for (Integer i = 0; i < e.getNumDml(); i++)
{
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(i) == 0);
System.assert(e.getDmlFields(i).size() == 3);
System.assert(e.getDmlFields(i)[0] == Account.Name);
System.assert(e.getDmlFields(i)[1] == Account.Phone);
System.assert(e.getDmlFields(i)[2] == Account.Physical_Street__c);
System.assert(e.getDmlMessage(i).indexOf('An account with this name, phone, street already exists.') > -1);
}
}

Account dupe2 = new Account(Name = 'Agency00', Phone='(981) 130-9988',Physical_Street__c = 'ZZZ',
Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

try{
insert dupe2;
System.assert(false);
}catch(DMLException e){
for (Integer i = 0; i < e.getNumDml(); i++)
{
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(i) == 0);
System.assert(e.getDmlFields(i).size() == 3);
System.assert(e.getDmlFields(i)[0] == Account.Name);
System.assert(e.getDmlFields(i)[1] == Account.Phone);
System.assert(e.getDmlFields(i)[2] == Account.Physical_Street__c);
System.assert(e.getDmlMessage(i).indexOf('An account with this name, phone already exists.') > -1);
}
}

dupe1 = new Account(Id=acc1.id, Name = 'Agency0', Phone='(981) 130-0000',Physical_Street__c = 'ABC0',
Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

update dupe1;
System.assert(false);
}
}

 

 

Can someone pls tell me why this test failure?

 

Hi,

 

I am not sure how to test this code. I dont know how to use the SelectOption in the test class. Can somebody tell me how should i write it??

 

 

public with sharing class ctrl_MultiSelectPickList 
{
    public String Label{get; set;}
    
    public List<String> selected{get; set;}
    public List<String> deselected{get; set;}

    public SelectOption Available { get; set; }
    public SelectOption Chosen { get; set; }
    
    public List<SelectOption> selectedOptions = null;
    public List<SelectOption> AllOptions = null;

    // A Reference list (all options)
    List<Options> opt{get; set;}
 
    public List<SelectOption> getselectedOptions()
    {
        return selectedOptions;
    }
    public void setselectedOptions(List<SelectOption> value)
    {
        selectedOptions = value;
    }

    public List<SelectOption> getAllOptions()
    {
        return AllOptions;
    }
    
    public void setAllOptions(List<SelectOption> value)
    {
        AllOptions = value;
    }


    public class Options
    {
        public Integer Index{get; set;}
        public String Item{get; set;}
    }

Hi I am new to writing test classes. My code coverage is only 65%. Can somebody pls help.

 

The class is:

public class customObjectController{


    public static Integer indexGen {get; set;} //index generator
    public List<CustomObjectWrapper> coList;
    public Integer numRows {get; set;}

    
    /*
     * Wrapper class for holding a 'CustomObjects__c' record and its row index together.
     * The row index is used to delete the row using commandLink
     */
    private class CustomObjectWrapper {
        private CustomObjects__c customObject;
        private Integer index;
     
     
     public CustomObjectWrapper() {
            this.customObject = new CustomObjects__c();
            this.index = indexGen++;
        }
        
        public CustomObjects__c getCustomObject() {
            return customObject;
        }
        
        public Integer getIndex() {
            return index;
        }
    }
        
    /*
     * Constructor
     * -----------
     * Initializes 'coList' and 'numRows'
     */
    public customObjectController(ApexPages.StandardController controller) {
        if(indexGen == null) indexGen = 1;
        coList = new List<CustomObjectWrapper>();
        numRows = 1;
    }
    public List<CustomObjectWrapper> getCoList() {
        return coList;
    }


     
    /*
     * upserts all records currently in the 'coList'
     */
    public PageReference save() {
        if(coList.size()==0)
            return new PageReference('/' + ApexPages.currentPage().getParameters().get('retURL'));
        try {
            List<CustomObjects__c> tempList = new List<CustomObjects__c>();
            for(Integer i=0; i<coList.size(); ++i)
                tempList.add(coList[i].getCustomObject());
            upsert(tempList);
            return new PageReference('/' + ApexPages.currentPage().getParameters().get('retURL'));
        } catch (System.DMLException ex) {
            ApexPages.addMessages(ex);
            return null;
        }
    }
    
    
    
     /*
     * appends new records to the 'coList'.
     * The number of records added is determined by the value of 'numRows'
     * Issues with the method: Not working if mandatory fields are left empty
     */
    public void add() {
          try {
            if(numRows > 0)
                for(Integer i=0; i<numRows; ++i)
                    coList.add(new CustomObjectWrapper());
        } catch (Exception ex) {
             ApexPages.addMessages(ex);
        }
   }
    
    
    /*
     * Clears all records from the 'coList'
     * Issues with the method: Not working if mandatory fields are left empty
     */        
    public void clear(){
        coList.clear();
        numRows = 1;
         }
     

    /*
     * deletes a record from 'coList' depending on the 'index' of 'CustomObjects__c' within the 'CustomObjectWrapper' class
     */
     public void delRecords(){
        try {
            Integer delIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('delRow'));
            
            for(Integer i=0; i<coList.size(); ++i)
                if(coList[i].index == delIndex) {
                    coList.remove(i);
                    break;
                }
        } catch (Exception ex) {
            ApexPages.addMessages(ex);
        }
    }
    
       
             
   }

Test class;

 

@isTest

public class customObjectController_Test{

public static testMethod void testcls(){
CustomObjects__c cust = new CustomObjects__c();
PageReference pageRef = Page.ReportPage;
Test.setCurrentPageReference(pageRef);
ApexPages.StandardController sc = new ApexPages.StandardController(cust);
customObjectController obj = new customObjectController(sc);
customObjectController.CustomObjectWrapper wra = new customObjectController.CustomObjectWrapper();
obj.save();
obj.add();
obj.clear();
obj.delRecords();
wra.getIndex();
wra.getCustomObject();
}
}

Hi I am trying to write the test class for the below code but getting the above mentioned error. I guess this is due to the wrapper class being private. Can anybody pls help me with the test class.

 

The class is:

public class customObjectController{


    public static Integer indexGen {get; set;} //index generator
    public List<CustomObjectWrapper> coList;
    public Integer numRows {get; set;}

    
    /*
     * Wrapper class for holding a 'CustomObjects__c' record and its row index together.
     * The row index is used to delete the row using commandLink
     */
    private class CustomObjectWrapper {
        private CustomObjects__c customObject;
        private Integer index;
     
     
     public CustomObjectWrapper() {
            this.customObject = new CustomObjects__c();
            this.index = indexGen++;
        }
        
        public CustomObjects__c getCustomObject() {
            return customObject;
        }
        
        public Integer getIndex() {
            return index;
        }
    }
        
    /*
     * Constructor
     * -----------
     * Initializes 'coList' and 'numRows'
     */
    public customObjectController(ApexPages.StandardController controller) {
        if(indexGen == null) indexGen = 1;
        coList = new List<CustomObjectWrapper>();
        numRows = 1;
    }
    public List<CustomObjectWrapper> getCoList() {
        return coList;
    }


     
    /*
     * upserts all records currently in the 'coList'
     */
    public PageReference save() {
        if(coList.size()==0)
            return new PageReference('/' + ApexPages.currentPage().getParameters().get('retURL'));
        try {
            List<CustomObjects__c> tempList = new List<CustomObjects__c>();
            for(Integer i=0; i<coList.size(); ++i)
                tempList.add(coList[i].getCustomObject());
            upsert(tempList);
            return new PageReference('/' + ApexPages.currentPage().getParameters().get('retURL'));
        } catch (System.DMLException ex) {
            ApexPages.addMessages(ex);
            return null;
        }
    }
    
    
    
     /*
     * appends new records to the 'coList'.
     * The number of records added is determined by the value of 'numRows'
     * Issues with the method: Not working if mandatory fields are left empty
     */
    public void add() {
          try {
            if(numRows > 0)
                for(Integer i=0; i<numRows; ++i)
                    coList.add(new CustomObjectWrapper());
        } catch (Exception ex) {
             ApexPages.addMessages(ex);
        }
   }
    
    
    /*
     * Clears all records from the 'coList'
     * Issues with the method: Not working if mandatory fields are left empty
     */        
    public void clear(){
        coList.clear();
        numRows = 1;
         }
     

    /*
     * deletes a record from 'coList' depending on the 'index' of 'CustomObjects__c' within the 'CustomObjectWrapper' class
     */
     public void delRecords(){
        try {
            Integer delIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('delRow'));
            
            for(Integer i=0; i<coList.size(); ++i)
                if(coList[i].index == delIndex) {
                    coList.remove(i);
                    break;
                }
        } catch (Exception ex) {
            ApexPages.addMessages(ex);
        }
    }
    
       
             
   }

the test class that I have written is:

@isTest

public class customObjectController_Test{

public static testMethod void testcls(){
CustomObjects__c cust = new CustomObjects__c();
ApexPages.StandardController sc = new ApexPages.StandardController(cust);
customObjectController obj = new customObjectController(sc);
customObjectController.CustomObjectWrapper wra = new customObjectController.CustomObjectWrapper();
obj.save();
obj.add();
obj.clear();
obj.delRecords();
wra.getIndex();
wra.getcustomObject();
}
}



I have 2 custom objects on Contact. One is "Address" and the other object is a junction object
between Account & Contact. It is called "Account_Contact_Relationship__c".

Now whenever a user creates a new Account Contact Relationship object, the address of the
Account on this object should come as record under the "Address" related list on Contact Page.

I am writing the trigger as below but it is giving me Null Pointer exception at line 29 column 1.
(ac.Account__r.Marketting_City__c= imap.get(ac.Account__c).Marketting_City__c;)
Can somebody please help me with the trigger:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
trigger AddressUpdate on Account_Contact_Relationship__c (before insert,after insert) {

List<Address__c> addr = new List<Address__c>();
List<id> insertaid = new List<id>();
//List<id> updateaid = new List<id>();

if(trigger.isinsert)
{
for(Account_Contact_Relationship__c ac: Trigger.new)
{
insertaid.add(ac.Account__c);
}
system.debug(insertaid);
/*
if(trigger.isupdate)
{
for(Account_Contact_Relationship__c ac: Trigger.new)
{
updateaid.add(ac.accountid);
}
}*/
Map<id,Account> imap = new Map<id,Account>([Select Marketting_Street__c,Marketting_City__c,MarketingState__c,Marketting_PostalCode__c,ISO_Country_Code__c from Account where id IN: insertaid]);
//Map<id,Account> umap = new Map<id,Account>([Select Marketting_Street__c,Marketting_City__c,MarketingState__c,Marketting_PostalCode__c,ISO_Country_Code__c from Account where id IN: updateaid]);

if(trigger.isinsert)
{
for(Account_Contact_Relationship__c ac: Trigger.new)
{
ac.Account__r.Marketting_City__c= imap.get(ac.Account__c).Marketting_City__c;
ac.Account__r.Marketting_Street__c=imap.get(ac.Account__c).Marketting_Street__c;
}
}/*
if(trigger.isupdate)
{
for(Account_Contact_Relationship__c ac: Trigger.new)
{
ac.Account__r.Marketting_City__c= umap.get(ac.accountid).Marketting_City__c;
ac.Account__r.Marketting_Street__c=umap.get(ac.accountid).Marketting_Street__c;
}
}*/
for(Account_Contact_Relationship__c ac: Trigger.new)
{
if(trigger.isinsert)
{
Address__c a = new Address__c
(City__c=ac.Account__r.Marketting_City__c,Name=ac.Account__r.Marketting_Street__c);
addr.add(a);
}
}
insert(addr);
}
}