0

getting error-

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]

error in pagereference method saveAccountContact controller:

public class IGT_TradeShow_Ctl {
    public String selectAccountId{get;set;}
    public String selectContactId{get;set;}
    public Campaign objCampaign{get;set;}
    public Account objAccount{get;set;}
    public Contact objContact{get;set;}
    public List<SelectOption> AccountSelect;
    public List<SelectOption> ContactSelect{get;set;}
    public List<Account> accountList{get;set;}
    public List<Contact> contactList{get;set;}
    public List<OpportunityLineItem> oppLineItem{get;set;}
    public Map<String, Map<Id,ProductCustom>> OppLineItemMap { get; set; }
    public Set<ID> accountIDSet;
    public String campID;
    public Opportunity objOpportunity{get;set;}
    public boolean newAccount{get;set;}
    public ID masterOpportunityID{get;set;}


    //Controller
    public IGT_TradeShow_Ctl(ApexPages.StandardController controller) {
        newAccount = false;
        objAccount = new Account();
        objContact = new Contact();
        accountList= new List<Account>();
        OppLineItemMap = new Map<String, Map<Id,ProductCustom>>();
        selectAccountId ='';
        selectContactId ='';
        accountList= [SELECT ID,Name from Account ORDER BY createdDate DESC Limit 8];
        System.debug('selectAccountId>>>>>>>>>>'+selectAccountId);
        System.debug('AccountList@@@@@@@@@@'+accountList);
        contactList = new List<Contact>(); 
        objOpportunity = new Opportunity();

        //String campID = apexpages.currentpage().getparameters().get('id');
        //Sneha - SOQL for campaign
        //objCampaign = new Campaign([SELECT Id,Name FROM Campaign WHERE StartDate <= Today AND EndDate >= Today]);
        System.debug('objCampaign#########'+objCampaign);
        campID = '701S0000000O2mo'; //For Testing Purpose
        System.debug('campID @@@@@@@@@@'+campID );
        AccountSelect = new List<SelectOption>();
        ContactSelect = new List<SelectOption>();

        //Products for Master Tradeshow Opp OLI
        oppLineItem = new List<OpportunityLineItem>([SELECT Id, name,Quantity,Delivery_Quater__c ,Product2Id , Product_Category__c,CONFIDENCE_LEVEL__c,REC_SALE__c,  OpportunityID FROM OpportunityLineItem WHERE Opportunity.CampaignID =:campID  AND Product_Category__c !=null AND Opportunity.RecordType.DeveloperName='Trade_Show_Master']);
        System.debug('oppLineItem@@@@'+oppLineItem);

        Set<Id> setProductId = new Set<Id>();

        //Map to display Product Category and Product List.
        for(OpportunityLineItem objLineItem : oppLineItem){

            setProductId.add(objLineItem.Product2Id);

            masterOpportunityID = objLineItem.OpportunityID;

            objLineItem.CONFIDENCE_LEVEL__c = '25%';
            objLineItem.REC_SALE__c = 'SALE';

            //

            Map<Id,ProductCustom> productMap = new Map<Id,ProductCustom>();
            if(OppLineItemMap.keySet().contains(objLineItem.Product_Category__c)){
                productMap = OppLineItemMap.get(objLineItem.Product_Category__c);  
                productMap.put(objLineItem.Product2Id,new ProductCustom(objLineItem));
                OppLineItemMap.put(objLineItem.Product_Category__c,productMap);
            }else{
                productMap.put(objLineItem.Product2Id,new ProductCustom(objLineItem));
                OppLineItemMap.put(objLineItem.Product_Category__c,productMap);
            }
        }

        for(OpportunityLineItem oli:[SELECT Id,Delivery_Quater__c, name,Quantity ,Product2Id, Product_Category__c,CONFIDENCE_LEVEL__c,REC_SALE__c,DELIVERY_DATE__c,  
                                    CreatedDate, OpportunityID,Opportunity.RecordType.DeveloperName,Opportunity.CampaignID FROM OpportunityLineItem WHERE 
            Product2Id in :setProductId AND (
            (Opportunity.CampaignID !=null  AND Product_Category__c !=null AND Opportunity.RecordType.DeveloperName='Trade_Show_Lead' ) OR //for Other Tradeshow
            (Opportunity.CampaignID = null AND Opportunity.StageName !='Closed Won' AND Opportunity.ForecastCategoryName = 'Omitted') //other Opportunities
            ) order by createdDate asc]){

            if(OppLineItemMap.keyset().contains(oli.Product_Category__c) && OppLineItemMap.get(oli.Product_Category__c).keyset().contains(oli.Product2Id)){
                ProductCustom product = OppLineItemMap.get(oli.Product_Category__c).get(oli.Product2Id);

                //check if it belongs to an existing tradeshow
                if(oli.Opportunity.CampaignID !=null && oli.Opportunity.RecordType.DeveloperName=='Trade_Show_Lead'){

                    //Add Quantity
                    product.existingTradeShow.Quantity += oli.Quantity;

                    product.existingTradeShow.REC_SALE__c = oli.REC_SALE__c;
                    product.existingTradeShow.DELIVERY_DATE__c = oli.DELIVERY_DATE__c;

                    //Recent confidence level
                    if(product.existingTradeShow.CreatedDate==null || (product.existingTradeShow.CreatedDate<oli.CreatedDate)){
                        product.existingTradeShow.CONFIDENCE_LEVEL__c = oli.CONFIDENCE_LEVEL__c;
                    }
                }
                //check if it belongs to other opportunity
                else if(oli.Opportunity.CampaignID == null && oli.Opportunity.StageName !='Closed Won' && oli.Opportunity.ForecastCategoryName == 'Omitted' ){ //add stage conditions
                    //Add Quantity
                    product.otherOLI.Quantity += oli.Quantity;

                    product.otherOLI .REC_SALE__c = oli.REC_SALE__c;
                    product.otherOLI .DELIVERY_DATE__c = oli.DELIVERY_DATE__c;   

                    //Recent confidence level 
                    if(product.otherOLI.CreatedDate==null || (product.otherOLI.CreatedDate<oli.CreatedDate)){
                        product.otherOLI.CONFIDENCE_LEVEL__c = oli.CONFIDENCE_LEVEL__c;
                    } 
                }
            }
        }

        //
    }

    //Display Related Contact of Selected Account
    public PageReference selectedAccContact(){ //getContacts
        ContactSelect.clear();
        //contactList = [SELECT id,name FROM Contact WHERE AccountID =: selectAccountId ORDER BY Name ASC];
        contactList = [SELECT id,name FROM Contact WHERE AccountID =: objOpportunity.AccountID ORDER BY Name ASC];
        ContactSelect.add(new SelectOption('','--None--'));
        for(Contact objCon : contactList){
            ContactSelect.add(new SelectOption(objCon.ID,objCon.Name));    
        }
        //getContactSelect();
        return null;
    }

    //Displays Account in dropdown
    public List<SelectOption>  getAccountSelect(){
        AccountSelect.clear();
        AccountSelect.add(new SelectOption('','--None--'));
        for(Account objAcc : accountList){
            AccountSelect.add(new SelectOption(objAcc.ID,objAcc.Name));        
        }
        return AccountSelect;       
    }

    //Logic to display Enter Account Name: 
    public pagereference addNewAccount(){
        selectAccountId = null;
        newAccount = true;
        return null;
    }

    public pagereference selectExistingAccount(){
        if(selectAccountId!=null){
            newAccount = false;
        }

        system.debug(newAccount);
        return null;
    }


    //Logic to create Update Opportunity
    public PageReference createUpdateOpportunity(){
        List<OpportunityLineItem> insertCurrentOLILst = new List<OpportunityLineItem>();
        RecordType objRT = new RecordType();
        objRT = [SELECT id, name from RecordType where sObjectType = 'Opportunity' AND name='Trade Show Lead'];
        if(objRT.id!=null){
            objOpportunity.RecordTypeID = objRT.ID;
        }
        objOpportunity.StageName = 'Opportunity Qualified';
        objOpportunity.CloseDate = System.today()+5;
        objOpportunity.Name =objOpportunity.Account.Name+' '+objOpportunity.Campaign.Name;
        //objOpportunity.MasterOpp = masterOpportunityID; - to link with master  Opp.
        insert objOpportunity;

        for(String  prodCategory : OppLineItemMap.keySet()){
            for(ProductCustom objProdCustom : OppLineItemMap.get(prodCategory).values()){
                if(objOpportunity.id!= null){
                    objProdCustom.currentOLI.OpportunityId = objOpportunity.id;
                }
                if(objProdCustom.currentOLI.Quantity>0){
                    insertCurrentOLILst.add(objProdCustom.currentOLI);
                }

            }
        }

        if(insertCurrentOLILst.size()>0){
            insert insertCurrentOLILst; 
        }
        return null;

    }

    // logic to save new account and contact
    public PageReference saveAccountContact(){
        if(selectAccountId != null && selectAccountId !=''){
            objContact.AccountId = selectAccountId;
            insert objContact;

        }else{
            insert ObjAccount;
            objContact.AccountId = ObjAccount.id;
            insert objContact;

            //selectAccountId = ObjAccount.id;
        }
        selectAccountId = ObjAccount.Id;
        PageReference pr = new PageReference('/apex/IGT_TradeShowHomeScreen?id='+campID); 
        pr.setRedirect(true);
        return pr; 

    }

    //Redirects to Create New Account Contact Page  
    public PageReference createAccountContact(){
        system.debug('test');
        selectAccountId = null;
        PageReference pr = new PageReference('/apex/IGT_NewAccountContact'); 
        pr.setRedirect(false);
        return pr; 
    }

    //Redirect to Trade show Page
    public PageReference backToTradeShow(){
        PageReference pr = new PageReference('/apex/IGT_TradeShowHomeScreen?id='+campID); 
        pr.setRedirect(false);
        return pr; 
    }

    //Wrapper Class
    public class ProductCustom{
        public OpportunityLineItem currentOLI{get;set;}
        public OpportunityLineItem existingTradeShow{get;set;}
        public OpportunityLineItem otherOLI{get;set;}

        public ProductCustom(){
            currentOLI = new OpportunityLineItem(Quantity=0);
            existingTradeShow = new OpportunityLineItem();
            otherOLI = new OpportunityLineItem();
        }

        public ProductCustom(OpportunityLineItem oli){
            currentOLI = oli;
            currentOLI.Quantity=0;
            existingTradeShow = new OpportunityLineItem(Quantity=0);
            otherOLI = new OpportunityLineItem(Quantity=0);
        }
    }

}

test class:

@isTest
public class IGT_TradeShow_Ctl_Test{
    public static testMethod void testTradeShow(){

        String PriceBook2Id = Test.getStandardPricebookId();

        Campaign objCampaign = new Campaign();
        objCampaign.StartDate = System.today();
        objCampaign.EndDate = System.today();
        objCampaign.Name = 'Test Campaign';
        insert objCampaign;

        Account objAccount = new Account();
        objAccount.Name = 'test Account';
        insert objAccount;

        Product2 prod = new Product2(
            Name = 'Product X',
            ProductCode = 'Pro-X',
            isActive = true
        );
        insert prod;

        //Create your pricebook entry
        PricebookEntry pbEntry = new PricebookEntry(
            Pricebook2Id = PriceBook2Id,
            Product2Id = prod.Id,
            UnitPrice = 100.00,
            IsActive = true
        );
        insert pbEntry;

        RecordType objRecordType = [SELECT id,name FROM RecordType WHERE sObjectType = 'Opportunity' AND Name ='Trade Show Master'];

        Opportunity objOpportunity = new Opportunity();
        objOpportunity.CampaignId = objCampaign.id;
        objOpportunity.RecordTypeId = objRecordType.id;
        objOpportunity.Name = 'Test Opportunity';
        objOpportunity.StageName = 'Opportunity Qualified';
        objOpportunity.CloseDate = System.today();
        objOpportunity.Install_Date__c = System.today();
        objOpportunity.ForecastCategoryName = 'Pipeline';
        objOpportunity.AccountId = ObjAccount.id;
        //objOpportunity.CurrencyIsoCode = '';
        insert objOpportunity;

        //create your opportunity line item.  This assumes you already have an opportunity created, called opp
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = objOpportunity.Id,
            Product_Category__c = 'Core Hardware',
            Quantity = 5,
            PricebookEntryId = pbEntry.Id
            //TotalPrice = quantity * pbEntry.UnitPrice
        );
        insert oli;

        PageReference pageRef = Page.IGT_TradeShowHomeScreen;
        Test.setCurrentPage(pageRef);
        pageRef.getParameters().put('id',objCampaign.id);

        IGT_TradeShow_Ctl tradeShowController = new IGT_TradeShow_Ctl(new ApexPages.StandardController(objCampaign));
        System.assertNotEquals(null,tradeShowController.saveAccountContact());

        PageReference selectAccountPage = tradeShowController.selectedAccContact();
        PageReference addNewAccountPage = tradeShowController.addNewAccount();
        PageReference selectExistingPage = tradeShowController.selectExistingAccount();
        PageReference createUpdateOppgPage = tradeShowController.createUpdateOpportunity();
        PageReference createAccConPage = tradeShowController.createAccountContact();
        PageReference backToTradeShowPage = tradeShowController.backToTradeShow();
        tradeShowController.getAccountSelect();

        IGT_TradeShow_Ctl.ProductCustom objProdCustWrapper = new IGT_TradeShow_Ctl.ProductCustom();
        IGT_TradeShow_Ctl.ProductCustom objProdCustContWrapper = new IGT_TradeShow_Ctl.ProductCustom(oli);



    }
    public static testMethod void testTradeShow1(){

        String PriceBook2Id = Test.getStandardPricebookId();

        Campaign objCampaign = new Campaign();
        objCampaign.StartDate = System.today();
        objCampaign.EndDate = System.today();
        objCampaign.Name = 'Test Campaign';
        insert objCampaign;

        Account objAccount = new Account();
        objAccount.Name = 'test Account';
        insert objAccount;

        Product2 prod = new Product2(
            Name = 'Product X',
            ProductCode = 'Pro-X',
            isActive = true
        );
        insert prod;

        //Create your pricebook entry
        PricebookEntry pbEntry = new PricebookEntry(
            Pricebook2Id = PriceBook2Id,
            Product2Id = prod.Id,
            UnitPrice = 100.00,
            IsActive = true
        );
        insert pbEntry;

        RecordType objRecordType = [SELECT id,name FROM RecordType WHERE sObjectType = 'Opportunity' AND Name ='Trade Show Lead'];

        Opportunity objOpportunity = new Opportunity();
        objOpportunity.CampaignId = objCampaign.id;
        objOpportunity.RecordTypeId = objRecordType.id;
        objOpportunity.Name = 'Test Opportunity';
        objOpportunity.StageName = 'Opportunity Qualified';
        objOpportunity.CloseDate = System.today();
        objOpportunity.Install_Date__c = System.today();
        objOpportunity.ForecastCategoryName = 'Pipeline';
        objOpportunity.AccountId = ObjAccount.id;
        //objOpportunity.CurrencyIsoCode = '';
        insert objOpportunity;

        //create your opportunity line item.  This assumes you already have an opportunity created, called opp
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = objOpportunity.Id,
            Product_Category__c = 'Core Hardware',
            Quantity = 5,
            PricebookEntryId = pbEntry.Id
            //TotalPrice = quantity * pbEntry.UnitPrice
        );
        insert oli;

        IGT_TradeShow_Ctl tradeShowController = new IGT_TradeShow_Ctl(new ApexPages.StandardController(objCampaign));

        PageReference selectAccountPage = tradeShowController.selectedAccContact();
        PageReference addNewAccountPage = tradeShowController.addNewAccount();
        PageReference selectExistingPage = tradeShowController.selectExistingAccount();
        //PageReference createUpdateOppgPage = tradeShowController.createUpdateOpportunity();
        //PageReference saveAccConPage = tradeShowController.saveAccountContact();
        PageReference backToTradeShowPage = tradeShowController.backToTradeShow();

        IGT_TradeShow_Ctl.ProductCustom objProdCustWrapper = new IGT_TradeShow_Ctl.ProductCustom();
        IGT_TradeShow_Ctl.ProductCustom objProdCustContWrapper = new IGT_TradeShow_Ctl.ProductCustom(oli);

    }
}

can anyone tell how to cover this part of controller- //Map to display Product Category and Product List. for(OpportunityLineItem objLineItem : oppLineItem){

        setProductId.add(objLineItem.Product2Id);

        masterOpportunityID = objLineItem.OpportunityID;

        objLineItem.CONFIDENCE_LEVEL__c = '25%';
        objLineItem.REC_SALE__c = 'SALE';

        //

        Map<Id,ProductCustom> productMap = new Map<Id,ProductCustom>();
        if(OppLineItemMap.keySet().contains(objLineItem.Product_Category__c)){
            productMap = OppLineItemMap.get(objLineItem.Product_Category__c);  
            productMap.put(objLineItem.Product2Id,new ProductCustom(objLineItem));
            OppLineItemMap.put(objLineItem.Product_Category__c,productMap);
        }else{
            productMap.put(objLineItem.Product2Id,new ProductCustom(objLineItem));
            OppLineItemMap.put(objLineItem.Product_Category__c,productMap);
        }
4
  • you need to have name for Account here insert ObjAccount;
    – Reshma
    Jan 18, 2017 at 6:54
  • yeah i got that. but where i should put the name in controller Jan 18, 2017 at 6:55
  • Any where before you insert account.
    – Reshma
    Jan 18, 2017 at 6:56
  • can you tell me how to cover the map part Jan 18, 2017 at 7:30

1 Answer 1

2

I assume in your actual page, you have fetched the name from your page so your page should work - which is my assumption. So you should update the code (Add the name of the Account) in your test class, not in your controller.

What you need to do:

    PageReference pageRef = Page.IGT_TradeShowHomeScreen;
    Test.setCurrentPage(pageRef);
    pageRef.getParameters().put('id',objCampaign.id);

    IGT_TradeShow_Ctl tradeShowController = new IGT_TradeShow_Ctl(new ApexPages.StandardController(objCampaign));
    tradeShowController.objAccount = 'Test Account abc'; //Put it here. 
    System.assertNotEquals(null,tradeShowController.saveAccountContact());

Also, Don't forget to add more System.assert into your test method to actually test your code.

1
  • wow it worked. Thank you so much for your valuable guidance. Jan 18, 2017 at 7:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .