2

I have a custom page with standard controller and extension. I have a page block table with inputField to display the account lookup. When the page is submitted with the wrong Account value in account lookup text box, it throws the following validation error message: "No records found". My problem is, this message shows for all account lookups in the page block table even though others have correct Account values.

Page:

    <apex:pageblock id="pb" title="Convert {!Site__c.name} ">
        <apex:pageMessages ></apex:pageMessages>
        <apex:pageBlockButtons >
            <apex:commandbutton value="Create Opportunity " action="{!Convert}"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>
        </apex:pageBlockButtons>
        <!-- Site Ref No: -->
        <apex:pageBlockSection title="Convert Site" columns="2">
            <apex:outputField value="{!Site__c.Site_Ref_No__c}"/>
            <apex:outputField value="{!Site__c.Name}" />
            <apex:outputField value="{!Site__c.Site_Address__c}"/>
            <apex:outputField value="{!Site__c.Project_City__c}"/>
            <apex:outputField value="{!Site__c.State__c}"/>
            <apex:outputField value="{!Site__c.Post_code__c}"/>
            <apex:outputField value="{!Site__c.Account__c}" rendered="true"/>
            <apex:outputField value="{!Site__c.Completion_Date__c}" rendered="true"/>

        </apex:pageBlockSection>

        <apex:pageblock id="pb1">
            <apex:pageBlockTable id="panelgrid" value="{!lstOppRecords}" var="e1">
                <apex:column >
                    <apex:facet name="header">Opportunity</apex:facet>
                    <apex:selectList value="{!e1.oppType}" size="1" tabindex="1">
                        <apex:selectOptions value="{!items}"/>
                    </apex:selectList>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">Account</apex:facet>
                    <apex:inputfield id="AccountId" value="{!e1.opp.AccountId}" >
                        <apex:actionSupport event="onblur" action="{!SalesPersonPopulate}" rerender="panelgrid" >
                           <apex:param name="recIndex" value="{!e1.recIndex}" assignTo="{!selectedRowIndex}"/>
                        </apex:actionSupport>
                    </apex:inputfield>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">Sales Person</apex:facet>
                    <apex:inputfield id="SalesPerson" value="{!e1.opp.ownerid}" required="false"/>
                </apex:column>

                <apex:column >
                    <apex:facet name="header">Close Date</apex:facet>
                    <apex:inputfield value="{!e1.opp.CloseDate}" required="false"/>
                </apex:column>

                <apex:column >
                    <apex:facet name="header">Stage</apex:facet>
                    <apex:inputfield value="{!e1.opp.StageName}" required="false"/>
                </apex:column>
            </apex:pageBlockTable> 
        </apex:pageBlock>

    </apex:pageblock>
</apex:form>

Extension:

public class Conversion_site
{

private Site__c siteObj;
public String Sales_pop {get; set;}
private List<Opportunity> lstOpp = new List<Opportunity>();
public List<opportunityRecords> lstOppRecords {get; set;}
public Map<Integer, opportunityRecords> mapOppRecords {get; set;}
public Integer selectedRowIndex {get; set;}
public Integer rowCount {get; set;}
private Integer intialCount = 10;
private Integer maxCount {get; set;}
public List<String> lstOppName {get; set;}
private Map<Integer, String> mapOppName {get; set;}
private Map<Integer, String> mappedOppName = new Map<Integer, String>();
public String dateTimeValue { get; set; }

/*Opportunity records*/
public class opportunityRecords implements Comparable
{
    //Holds record Index
    public Integer recIndex {get; set;}
    public String oppType {get; set;}
    public Opportunity opp {get; set;}

    public opportunityRecords(Integer index)
    {
        recIndex = index;
        opp = new Opportunity();
    }

    // Implement the compareTo() method
    public Integer compareTo(Object compareTo) {
        opportunityRecords oppRec = (opportunityRecords)compareTo;
        if (recIndex == oppRec.recIndex) return 0;
        if (recIndex > oppRec.recIndex) return 1;
        return -1;        
    }
}

////save the records by adding the elements in the inner class list to lstAcct,return to the same page
public PageReference Convert()
{
    system.debug('>>>Inside convert');
    PageReference pr = new PageReference('/' + siteObj.Id);
    Integer totalRows = lstOppRecords.size();
    lstOpp.clear();
    for(opportunityRecords oppRec : lstOppRecords)
    {
        oppRec.opp.Name = siteObj.Name+'-'+oppRec.oppType;
        oppRec.opp.Site_Ref_No__c = siteObj.Site_Ref_No__c;
        if(oppRec.opp.StageName == 'No Contact Made')
            oppRec.opp.Probability= 0;
        else if(oppRec.opp.StageName == 'Contact Made')
            oppRec.opp.Probability= 10;
        else if(oppRec.opp.StageName == 'Taking Off')
            oppRec.opp.Probability= 25;
        else if(oppRec.opp.StageName == 'Quotation with Customer ')
            oppRec.opp.Probability= 50;
        else if(oppRec.opp.StageName == 'Negotiation/Review')
            oppRec.opp.Probability= 75;
        else if(oppRec.opp.StageName == 'Closed Won')
            oppRec.opp.Probability= 100;
        else if(oppRec.opp.StageName == 'Closed Lost')
            oppRec.opp.Probability= 0;

        if (oppRec.opp.AccountId != null && oppRec.opp.CloseDate != null)
        {
            if (oppRec.opp.OwnerId == null)
                oppRec.opp.OwnerId = UserInfo.getUserId();
            system.debug('>>>Check already available:');
            system.debug(oppRec.opp);
            lstOpp.add(oppRec.opp);
            mapOppRecords.remove(oppRec.recIndex);
            rowCount = rowCount - 1;
        }
    }
    insert lstOpp;
    if (lstOpp.size() != totalRows)
    {
        clearAndPopulateList();
        String successMsg = '';
        if(lstOpp.size() > 0)
        {
            if(lstOpp.size() == 1)
                successMsg = 'Successfully Created ' + lstOpp.size() + ' Opportunity';
            else if(lstOpp.size() > 1)
                successMsg = 'Successfully Created ' + lstOpp.size() + ' Opportunities';
            ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,successMsg));   
        }
        String errorMsg = 'Please enter Account and Close Date for below Opportunities. Otherwise click cancel to go back to Site Leads page.';

        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO,errorMsg));

        pr = null;
    }
    else
    {
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'All Opportunities created successfully.')); 
        pr.setRedirect(True);
    }
    return pr;
}

//add one more row
public void Add()
{   
    if (rowCount < maxCount)
    {
        rowCount = rowCount+1;
        addMore();
        clearAndPopulateMap();
    }
    else
    {
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Maximum row that can be added is ' + maxCount));
        //return null;
    }
}

/*Begin addMore*/
public void addMore()
{
    //call to the iner class constructor
    Set<Integer> setName = mapOppName.keySet();
    List<Integer> lstName = new List<Integer>();
    lstName.addAll(setName);
    mappedOppName.put(lstName[0], mapOppName.get(lstName[0]));
    mapOppName.remove(lstName[0]);
    opportunityRecords oppRecord = new opportunityRecords(rowCount);
    oppRecord.oppType = mappedOppName.get(lstName[0]);
    Id accId = siteObj.Account__c;
    system.debug('accId' + accId);
    if (accId != null)
    {
        oppRecord.opp.AccountId = accId;
        Id ownerid = getSalesPerson(oppRecord.opp.AccountId);
        if (ownerid != null)
            oppRecord.opp.OwnerId = ownerid;
    }
    oppRecord.opp.CloseDate=date.today()+7;
    oppRecord.opp.StageName = 'No Contact Made';
    //add the record to the inner class list
    lstOppRecords.add(oppRecord);
}


private void clearAndPopulateMap()
{
    if(!lstOppRecords.isEmpty())
    {
        mapOppRecords.clear();
        for(opportunityRecords oppRec : lstOppRecords)
        {
            mapOppRecords.put(oppRec.recIndex, oppRec);
        }
        lstOppRecords.sort();
    }
}

private void clearAndPopulateList()
{
    if(!lstOppRecords.isEmpty())
    {
        lstOppRecords.clear();
        lstOppRecords = mapOppRecords.values();
        lstOppRecords.sort();
    }
}

private void AddOpportunity(Integer rc)
{
    for(Integer i = 0; i < rc; i++) 
    {
        rowCount += i;
        addMore();
    }
    clearAndPopulateMap();
}

//Sales Person Populate from Account
public Id getSalesPerson(Id accountid)
{
    Id salesPersonId = [select EXO_Sales_Rep__r.id, EXO_Sales_Rep__r.Name 
                    from Account 
                    where id=:accountid 
                    LIMIT 1].EXO_Sales_Rep__r.Id;
    return salesPersonId;
}

//OnChange SalesPerson Populate
public void SalesPersonPopulate()
{
    system.debug('>>>ListOpprec start ' + lstOppRecords);
    opportunityRecords opRec = mapOppRecords.get(selectedRowIndex);
    if (opRec.opp.AccountId != null)
    {
        Id ownerid = getSalesPerson(opRec.opp.AccountId);
        opRec.opp.OwnerId = ownerid;
        mapOppRecords.put(selectedRowIndex, opRec);
        clearAndPopulateList();
    }
    system.debug('>>>ListOpprec start ' + lstOppRecords);
}

private void opportunityList()
{
    lstOppName = new List<String>();
    mapOppName = new Map<Integer, String>();
    lstOppName.add('Floor Systems');
    lstOppName.add('Footings & Concrete');
    lstOppName.add('Frames');
    lstOppName.add('Roof');
    lstOppName.add('External Cladding');
    lstOppName.add('Plasterer');
    lstOppName.add('Fix out');
    lstOppName.add('Renderer');
    lstOppName.add('Retaining Walls / Landscaping');
    lstOppName.add('Hire');
    maxCount = lstOppName.size();
    for(Integer i = 0; i < maxCount; i++)
    {
        mapOppName.put(i, lstOppName[i]);
    }
}
public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>();
    for(String name : lstOppName)
    {
        options.add(new SelectOption(name,name));
    }
    return options;
}

/*Constructor*/
public Conversion_site(ApexPages.StandardController stdController)
{  dateTimeValue = System.Now().format('dd/MM/yyyy');//GMT 
    siteObj = (Site__c)stdController.getRecord();
    lstOppRecords = new List<opportunityRecords>();
    mapOppRecords = new Map<Integer, opportunityRecords>();
    opportunityList();
    rowCount = 0;
    AddOpportunity(intialCount);
    selectedRowIndex = 0;

}/*End Constructor*/

}

If it shows a validation error message, I assume the Account lookup must not working. I'd have included a screenshot, but I don't have the reputation to post one. Based on the code I've posted, how do I fix this problem?

0

I think the error occurs because you have several parameters recIndex with same name on the page. Try the following steps to improve the code:

  • Create only one action function instead of using several actionSupports or wrap the account inputField with actionRegion tag
  • Use PageReference instead of void method

Formatted code example:

<apex:actionFunction name="doAccount" action="{!SalesPersonPopulate}" reRender="panelgrid">
    <apex:param name="recIndex" value="" assignTo="{!selectedRowIndex}"/>
</apex:actionFunction >

....    

<apex:column>
    <apex:facet name="header">Account</apex:facet>
    <apex:inputfield id="AccountId" value="{!e1.opp.AccountId}" onblur="doAccount('{!e1.recIndex}')"/>
</apex:column>


public PageReference SalesPersonPopulate()
{
    // Your logic 
    ....
    return null;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.