• medemaza
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 1
    Replies

Please Help 

 

Hi I have a apex classes and custom object My Class that creates an Opportunity and Line Items Records when saved on My Visualforce Page. I created a test class to deploy into prod, but can only get 64%coverage. Can someone help me increase my test coverage or guide me in the right dirrection so I can get as close to 100% Also when I run the test I get one Test Failure : System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, StageName, CloseDate]: [Name, StageName, CloseDate]

 

 Below is my Test Class:

@isTest 
private class TestProInterestToOpportunity {
    static testMethod void myTest() {
        List<Customer_Visited__c> lstOpp = new List<Customer_Visited__c>();
        ApexPages.StandardSetController sc = new ApexPages.StandardSetController(lstOpp);
        ProInterestToOpportunity  myCls = new ProInterestToOpportunity (sc); //Create object reference of 'LeadConvert'
        
        Account objAcc= new Account(Name='Test',Phone='0841545441',Fax='029295789');
        insert objAcc;
        
        Customer_Visit_Report__c objCusRpt = new Customer_Visit_Report__c();
        objCusRpt.Name = 'test';
        insert objCusRpt;
                        
        Customer_Visited__c objCusv = new Customer_Visited__c(Customer_Visit_ReportId__c=objCusRpt.Id, AccountId__c= objAcc.Id);
        insert objCusv;
        ApexPages.CurrentPage().getParameters().put('Id',objCusv.Id);
        
        Product2 objProduct = new product2(name='unittest');
        insert objProduct;
        
        Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1];
        insert new PricebookEntry(pricebook2id = stdPb.id, product2id = objProduct.id,unitprice=1.0, isActive=true);
        // Next, it creates a new pricebook with an entry for this product
        
        Pricebook2 PB = new pricebook2(name='unittest');
        insert PB;
        
        PricebookEntry PE = new PricebookEntry(pricebook2id=PB.id, product2id=objProduct.id,unitprice=1.0, isActive=true);
        insert PE;
        
        Product_of_Interest__c objLineItem = new Product_of_Interest__c();
        objLineItem = new Product_of_Interest__c();
        objLineItem.Customer_VisitedID__c = objCusv.Id;
        objLineItem.Customer_Visit_ReportID__c = objCusv.Customer_Visit_ReportID__c;
        objLineItem.ProductId__c = objProduct.Id;
        objLineItem.PriceBookEntryId__c = PE.Id;
        objLineItem.Name = objProduct.Name;
        objLineItem.Status__c = 'Not Started';
        insert objLineItem;
        
            Opportunity objOpp = new Opportunity();
            objOpp.Name = objCusv.AccountName__c + '-';      
            objOpp.AccountID = objCusv.AccountID__c;
            objOpp.CloseDate = System.Today();
            objOpp.StageName = 'Prospecting';
            insert objOpp;
              
        //ProInterestToOpportunity.CusvWrapper lw = myCls.objCusvW;
       
        myCls.objOpp = myCls.getFillOpportunity();  
        PageReference objPageRef = myCls.ConvertCurrentOpp();
        objPageRef = myCls.Cancel();    
        myCls.lstCusv= myCls.getlstCusv();    
    }
}

 Below is my Apex Class:

 

public class ProInterestToOpportunity {
    public ProInterestToOpportunity(ApexPages.StandardController controller) {
    }
    public ProInterestToOpportunity(ApexPages.StandardSetController controller) {
    controller.setPageSize(10);
    }    
    public boolean IsConverted {get;set;}
    Id oppId;   
    
    public string OpportunityStage {get;set;}

    public List<Customer_Visited__c> lstCusv;
    
    public Opportunity objOpp {get{if (objOpp == null) objOpp = new Opportunity(); return objOpp;} set;}
       
    public List<SelectOption> getStageName()
    {   
        OpportunityStage ls = [select MASTERLABEL from OpportunityStage where IsWon = False];
        OpportunityStage = ls.MASTERLABEL;
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption(ls.MASTERLABEL,ls.MASTERLABEL));
        return options;
    }
    
   public List<Customer_Visited__c> getlstCusv() {
        if (lstCusv== null){
        return [select AccountName__c,AccountId__c,Customer_Visit_ReportID__c,LastModifiedDate from Customer_Visited__c where id =: ApexPages.currentPage().getParameters().get('id') and AccountId__c != null];
       }
       else
        return lstCusv;
    
    }
     public Opportunity getFillOpportunity()
    {   
        if(lstCusv != null){
            for (Customer_Visited__c lc: lstCusv)
            {
                objOpp.Name = lc.AccountName__c + '-';   
                objOpp.StageName = OpportunityStage;  
                objOpp.CloseDate= System.Today(); 
                objOpp.AccountId = lc.AccountID__c;
            }
        }
        return objOpp;
    }
    public PageReference ConvertCurrentOpp() {
      
    List<Opportunity> lstOpp = [select Id,Name,AccountId,CloseDate,StageName from Opportunity where Name=:objOpp.Name];
     if (lstOpp.size() > 0)
        {
            lstOpp[0].Name =objOpp.Name; 
            lstOpp[0].StageName = objOpp.StageName;
            lstOpp[0].CloseDate = objOpp.CloseDate;     
            lstOpp[0].AccountId = objOpp.AccountId;
            
            update lstOpp[0];
            oppId = lstOpp[0].Id;
            }else{
            insert objOpp;
            oppId = objOpp.Id;
            } 
        if (lstCusv.size() > 0)
        {            
            lstCusv[0].IsConverted__c = true;
            update lstCusv;
        }
        
        //Move all leadlineitems to opportunitylineitems
        
        List<Product_of_Interest__c> lstProIn = [select id,Average_Price__c,Description__c,ProductID__c,Material_Packing_Group__c,Opportunity__c,PriceBookEntryID__c from Product_of_Interest__c where Customer_VisitedID__c = :ApexPages.currentPage().getParameters().get('Id')];
        if (lstProIn.size() > 0 && objOpp.Id != null)
        {
            List<OpportunityLineItem > lstOppTeam = new List<OpportunityLineItem >();
            for(Product_of_Interest__c l:lstProIn)
            {
                if(l.Opportunity__c != null || l.Opportunity__c != 0){
                
                OpportunityLineItem LineItem = new OpportunityLineItem();
                LineItem.OpportunityId = objOpp.Id;
                LineItem.Description = l.Description__c;
                LineItem.Material_Packing_Group__c = l.Material_Packing_Group__c;
                LineItem.PricebookEntryId = l.PriceBookEntryID__c;
                LineItem.Quantity = l.Opportunity__c;
                LineItem.UnitPrice = l.Average_Price__c;
                LineItem.TotalPrice *= l.Opportunity__c;
                lstOppTeam.add(LineItem);    
                }            
            }
            insert lstOppTeam;            
        }
       
        PageReference returnPage = new PageReference('/' + oppId);
        returnPage.setRedirect(true);
        return returnPage;
    }
    
    public PageReference Cancel() {
        PageReference returnPage = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
        returnPage.setRedirect(true);
        return returnPage;
    }
 

 Below is my Visual Force Page:

<apex:page id="CustomerToOpportunity" standardController="Customer_Visited__c" extensions="ProInterestToOpportunity">
<apex:form id="CloseOpportunitiesForm">
        <apex:pageBlock id="pageBlock" mode="edit">
        
            <apex:pageBlockButtons >
                <apex:commandbutton action="{!ConvertCurrentOpp}" value="Closed" />
                <apex:commandbutton action="{!Cancel}" value="Cancel" />
            </apex:pageBlockButtons>
            
            <apex:pageBlockSection id="OpportunityNew" collapsible="false" title="New Opportunities" columns="1">
            
                 <apex:pageblockSectionItem id="OppBlockSectionItem">
                    Opportunity Name
                    <apex:outputPanel >
                        <div class="requiredInput">
                        <div class="requiredBlock"></div>                        
                    <apex:inputField id="txtOpportunity" value="{!FillOpportunity.Name}" required="true" />                    
                    </div>
                    </apex:outputPanel>                    
                </apex:pageblockSectionItem>
                <apex:pageblockSectionItem id="StageBlockSectionItem">
                    Stage
                    <apex:selectList value="OpportunityStage">
                    <apex:inputField id="Stage" value="{!StageName}" required="true" />                    
                    </apex:selectList>                                    
                </apex:pageblockSectionItem>
                 
               </apex:pageBlockSection>
        </apex:pageBlock>
 </apex:form>
</apex:page>

 

 

 

trigger OrgOnAccount on Organization_Structure__c (after insert, after update) {

 for(Organization_Structure__c Org:Trigger.New){
 
    Organization_Structure__c[] OSC = [Select Id,Sales_Organization_Des__c,
    Sales_Person_Name__c,AccountID__c From Organization_Structure__c Where Id=:Org.id];
    
    for(Organization_Structure__c O:OSC ){
    Account[] Acc =[Select Id,Chlor_Alkali_Domestic__c,Chlor_Alkali_Exports__c,
    Epoxy_Domestic__c,Epoxy_Exports__c,Peroxide_Domestic__c,Peroxide_Exports__c,
    Phosphates_Domestic__c,Phosphates_Exports__c,Sulphites_Domestic__c,Sulphites_Exports__c,
    Sale_Chlor_Alkali_Domestic__c,Sale_Chlor_Alkali_Exports__c,Sale_Epoxy_Domestic__c,
    Sale_Epoxy_Exports__c,Sale_Peroxide_Domestic__c,Sale_Peroxide_Exports__c,Sale_Phosphates_Domestic__c,
    Sale_Phosphates_Exports__c,Sale_Sulphites_Domestic__c,Sale_Sulphites_Exports__c 
    From Account Where Id=:O.AccountId__c];
    
        for(Account A:Acc){
        
        if(O.Sales_Organization_Des__c=='Epoxy Domestic'){
        A.Epoxy_Domestic__c = true;
        A.Sale_Epoxy_Domestic__c = O.Sales_Person_Name__c; 
        }
         if(O.Sales_Organization_Des__c=='Epoxy Exports'){
        A.Epoxy_Exports__c = true;
        A.Sale_Epoxy_Exports__c = O.Sales_Person_Name__c; 
        }        
        if(O.Sales_Organization_Des__c=='Chlor-Alkali Domestic'){
        A.Chlor_Alkali_Domestic__c = true;
        A.Sale_Chlor_Alkali_Domestic__c = O.Sales_Person_Name__c; 
        }
         if(O.Sales_Organization_Des__c=='Chlor-Alkali Exports'){
        A.Chlor_Alkali_Exports__c = true;
        A.Sale_Chlor_Alkali_Exports__c = O.Sales_Person_Name__c; 
        }
        if(O.Sales_Organization_Des__c=='Phosphates Domestic'){
        A.Phosphates_Domestic__c = true;
        A.Sale_Phosphates_Domestic__c = O.Sales_Person_Name__c; 
        }
         if(O.Sales_Organization_Des__c=='Phosphates Exports'){
        A.Phosphates_Exports__c = true;
        A.Sale_Phosphates_Exports__c = O.Sales_Person_Name__c; 
        }
        if(O.Sales_Organization_Des__c=='Peroxide Domestic'){
        A.Peroxide_Domestic__c = true;
        A.Sale_Peroxide_Domestic__c = O.Sales_Person_Name__c; 
        }
         if(O.Sales_Organization_Des__c=='Peroxide Exports'){
        A.Peroxide_Exports__c = true;
        A.Sale_Peroxide_Exports__c = O.Sales_Person_Name__c; 
        }
        if(O.Sales_Organization_Des__c=='Sulphites Domestic'){
        A.Sulphites_Domestic__c = true;
        A.Sale_Sulphites_Domestic__c = O.Sales_Person_Name__c; 
        }
         if(O.Sales_Organization_Des__c=='Sulphites Exports'){
        A.Sulphites_Exports__c = true;
        A.Sale_Sulphites_Exports__c = O.Sales_Person_Name__c; 
        }
        update Acc;
    }
    
    }
    
 }

}

 

Only 50% I want more :mansad:

 

 

 

@isTest
private class TestOrgOnAccount {

    static testMethod void myUnitTest() {
        Account obj = new Account();
        obj.name = 'test';
        obj.Country__c = 'Afghanistan ';
        obj.Zone__c = 'PZ';
        insert obj;
        
        Organization_Structure__c Org = new Organization_Structure__c();
            Org.Sales_Person_Name__c = 'test';
            Org.Sales_Organization_Des__c = 'Epoxy Domestic';
            Org.name = 'test';
         insert Org;
        
        if(Org.Sales_Organization_Des__c == 'Epoxy Domestic'){
        Account obj2 = new Account(Id=obj.id,name = 'test',Country__c = 'Afghanistan',
        Zone__c = 'PZ',Epoxy_Domestic__c = true,Sale_Epoxy_Domestic__c = Org.Sales_Person_Name__c);
        
        update obj2;
        }
    }
}

 

Thak for heolp

 

 

public class LastOfferPrice {
    public ID OpportunityID{get; set;}

List<OpportunityLineItem> Item;
    public List<OpportunityLineItem> getItem(){
        if(Item==null){
        Item = [Select Id,PricebookEntry.Product2.Name,PricebookEntry.Product2Id,ListPrice,UnitPrice,Opportunity.CloseDate,LastModifiedDate From OpportunityLineItem Where OpportunityId=:OpportunityID]; 
       }
        return Item;
    }

List<Product2> Pro;
    public List<Product2> getPro(){
    for(integer i=0;i<Item.size();i++){
       
        Pro = [Select Id,Name From Product2 where Id=:Item[i].PricebookEntry.Product2Id]; 
         
       }
        return Pro;
    }
}

 Why my loop not working;

 

My Item has 7 records and my product  1,354 record but is return only 1 record

 

 

 

Error Generated request::Unable to send request server.
System error numbe: 12057
ExceptionCode : 5103

 

I'm try to chang my  Server URL

 

https://www.salesforce.com/services/Soap/c/6.0

 

https://www.salesforce.com/services/Soap/c/12.0

 

https://www.salesforce.com/services/Soap/c/13.0

 

https://www.salesforce.com/services/Soap/c/16.0

 

what was I'm mistake

 

I'm sure My Usernam password and token not worng

 

public class LastOfferPrice {
	public ID OpportunityID{get; set;}
	
	
	List<OpportunityLineItem> Item;
	public List<OpportunityLineItem> getItem(){
        if(Item==null){
        Item = [Select Id,PricebookEntry.Product2.Name,ListPrice,UnitPrice,Opportunity.CloseDate,LastModifiedDate From OpportunityLineItem Where OpportunityId=:OpportunityID ORDER BY PricebookEntry.Product2.Name DESC]; 
       }
        return Item;
    }

    List<OpportunityLineItem> LastOffer;
    
    public List<OpportunityLineItem> getLastOffer(){
    	
        for(OpportunityLineItem I:Item){
        	
	      LastOffer = [Select Id,distinct (PricebookEntry.Product2.Name,ListPrice),UnitPrice,LastModifiedDate 
	      From OpportunityLineItem Where LastModifiedDate <= YESTERDAY and PricebookEntry.Product2.Name=:I.PricebookEntry.Product2.Name ORDER BY ID DESC ]; 

	       }
	      return LastOffer;
    }

}

 Is error How to query not duplicate name of product

 

How I can get Id from list view when Icheck box and click Add to call List  I  have vf page but don't know how I can get ID from list

Contact List View

 

i'm posting the code of my apex Trigger. can somebody guide me how to write test class for the following.

 

trigger AccountUpdate on Account (before insert, before update) {
    map<string,string> SODesToName = new map<string,string> {
    'TEC Dom. Sales Org' => 'TECD',
    'TEC Exp. Sales Org' => 'TECE',
    'TOC Dom. Sales Org' => 'TOCD',
    'TOC Exp. Sales Org' => 'TOCE',
    'TPCC Dom. Sales Org'=>'TPCD ',
    'TPCC Exp. Sales Org'=>'TPCE ',
    'TPL Dom. Sales Org'=>'TPLD ',
    'TPL Exp. Sales Org'=>'TPLE ',
    'TSC Dom. Sales Org'=>'TSCD',
     'TSC Exp. Sales Org'=>'TSCE '
  };
    map<string,string> SONameToDes = new map<string,string>();
        for(string key:SODesToName.keySet())
        SONameToDes.put(SODesToName.get(key),key);
        
    map<string,string> DCDesToName = new map<string,string> {
    'TOC-Direct to Cust'=>'DC',
    'TPC-Direct Sales'=>'DI',
    'TEC-Direct Sales'=>'DS',
    'TSC-Direct to Cust'=>'DT',
    'TPL-End Users'=>'EU',
    'Group Companies'=>'GR',
    'TSC-Indenting Agent'=>'IA',
    'TOC-Indirect to Cust'=>'IC',
    'TPC-Indirect Sales'=>'ID',
    'TSC-Indirect to Cust'=>'IN',
    'TEC-Indirect Sales'=>'IS',
    'TPL-To Dealers'=>'TD'
  };
    map<string,string> DCNameToDes = new map<string,string>();
        for(string key:DCDesToName.keySet())
        DCNameToDes.put(DCDesToName.get(key),key);
            
    for(Account Acc:trigger.new) {
    if(SODesToName.containsKey(Acc.Sales_Organization_Des__c ) && (Trigger.isInsert || Trigger.oldMap.get(Acc.id).Sales_Organization_Des__c  != Acc.Sales_Organization_Des__c ))
    Acc.Sales_Organization__c  = SODesToName.get(Acc.Sales_Organization_Des__c );

    if(SONameToDes.containsKey(Acc.Sales_Organization__c ) && (Trigger.isInsert || Trigger.oldMap.get(Acc.id).Sales_Organization__c  != Acc.Sales_Organization__c ))
    Acc.Sales_Organization_Des__c  = SONameToDes.get(Acc.Sales_Organization__c );
    
    if(DCDesToName.containsKey(Acc.Distribution_Channel_Des__c ) && (Trigger.isInsert || Trigger.oldMap.get(Acc.id).Distribution_Channel_Des__c  != Acc.Distribution_Channel_Des__c ))
    Acc.Distribution_Channel__c  = DCDesToName.get(Acc.Distribution_Channel_Des__c );

    if(DCNameToDes.containsKey(Acc.Distribution_Channel__c ) && (Trigger.isInsert || Trigger.oldMap.get(Acc.id).Distribution_Channel__c  != Acc.Distribution_Channel__c ))
    Acc.Distribution_Channel_Des__c  = DCNameToDes.get(Acc.Distribution_Channel__c );
    
    }
    

}

 

so thank.

 

 

i'm posting the code of my apex Trigger. can somebody guide me how to write test class for the following.

 

trigger AccountUpdate on Account (before insert, before update) {
    map<string,string> SODesToName = new map<string,string> {
    'TEC Dom. Sales Org' => 'TECD',
    'TEC Exp. Sales Org' => 'TECE',
    'TOC Dom. Sales Org' => 'TOCD',
    'TOC Exp. Sales Org' => 'TOCE',
    'TPCC Dom. Sales Org'=>'TPCD ',
    'TPCC Exp. Sales Org'=>'TPCE ',
    'TPL Dom. Sales Org'=>'TPLD ',
    'TPL Exp. Sales Org'=>'TPLE ',
    'TSC Dom. Sales Org'=>'TSCD',
     'TSC Exp. Sales Org'=>'TSCE '
  };
    map<string,string> SONameToDes = new map<string,string>();
        for(string key:SODesToName.keySet())
        SONameToDes.put(SODesToName.get(key),key);
        
    map<string,string> DCDesToName = new map<string,string> {
    'TOC-Direct to Cust'=>'DC',
    'TPC-Direct Sales'=>'DI',
    'TEC-Direct Sales'=>'DS',
    'TSC-Direct to Cust'=>'DT',
    'TPL-End Users'=>'EU',
    'Group Companies'=>'GR',
    'TSC-Indenting Agent'=>'IA',
    'TOC-Indirect to Cust'=>'IC',
    'TPC-Indirect Sales'=>'ID',
    'TSC-Indirect to Cust'=>'IN',
    'TEC-Indirect Sales'=>'IS',
    'TPL-To Dealers'=>'TD'
  };
    map<string,string> DCNameToDes = new map<string,string>();
        for(string key:DCDesToName.keySet())
        DCNameToDes.put(DCDesToName.get(key),key);
            
    for(Account Acc:trigger.new) {
    if(SODesToName.containsKey(Acc.Sales_Organization_Des__c ) && (Trigger.isInsert || Trigger.oldMap.get(Acc.id).Sales_Organization_Des__c  != Acc.Sales_Organization_Des__c ))
    Acc.Sales_Organization__c  = SODesToName.get(Acc.Sales_Organization_Des__c );

    if(SONameToDes.containsKey(Acc.Sales_Organization__c ) && (Trigger.isInsert || Trigger.oldMap.get(Acc.id).Sales_Organization__c  != Acc.Sales_Organization__c ))
    Acc.Sales_Organization_Des__c  = SONameToDes.get(Acc.Sales_Organization__c );
    
    if(DCDesToName.containsKey(Acc.Distribution_Channel_Des__c ) && (Trigger.isInsert || Trigger.oldMap.get(Acc.id).Distribution_Channel_Des__c  != Acc.Distribution_Channel_Des__c ))
    Acc.Distribution_Channel__c  = DCDesToName.get(Acc.Distribution_Channel_Des__c );

    if(DCNameToDes.containsKey(Acc.Distribution_Channel__c ) && (Trigger.isInsert || Trigger.oldMap.get(Acc.id).Distribution_Channel__c  != Acc.Distribution_Channel__c ))
    Acc.Distribution_Channel_Des__c  = DCNameToDes.get(Acc.Distribution_Channel__c );
    
    }
    

}

 

so thank.