0

Visualforce Page Code:

<apex:page showHeader="true" sidebar="true" standardController="Property__c" extensions="PropertyObjectEnrichmentExtension">
    <apex:includeScript value="/support/console/30.0/integration.js"/>
    <apex:includeScript value="https://d24v6ufiv8bzar.cloudfront.net/ScriptLibrary/jquery-latest.pack.js"/>
    <apex:includeScript value="{!$Resource.ValidToSendJS}"/>
    
    <apex:sectionHeader title="Property {!Record.Property_ID__c}" />
    <apex:outputPanel id="messages">
    </apex:outputPanel>
    
    <apex:form id="mainform">        
        <apex:outputPanel rendered="{!NOT(EditMode)}">
            <apex:pageBlock title="Full Address of the Property" id="Auto-Address">
                <apex:outputText value="{!Record.Address_Full__c}" id="full-address"/>
            </apex:pageBlock>
            
            <apex:pageBlock title="Find Existing Similar Properties by Eircode" id="dup-prop-eircode">
                
                <apex:pageMessages />
                <apex:pageBlockButtons location="bottom">
                    <apex:commandButton action="{!findSimilarPropertiesByEircode}" value="Find Properties by Eircode" reRender="dup-prop-eircode, messages"/>
                    <apex:commandButton action="{!mergeProperties}" value="Merege With Selected Property" immediate="true" reRender="mainform, messages"/>
                </apex:pageBlockButtons>
                <apex:pageBlockTable value="{!wrapProps}" var="p" id="theTable1" frame="border">
                    <apex:column headerValue="Select" >
                        <apex:inputCheckbox value="{!p.selected}"/>
                        <apex:outputText value="{!p.selected}"/>
                    </apex:column> 
                    <apex:column headerValue="MPRN" value="{!p.prop.MPRN__c}"/>
                    <apex:column headerValue="GPRN" value="{!p.prop.GPRN__c}"/>
                    <apex:column headerValue="Full Address" value="{!p.prop.Address_Full__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:form>
</apex:page>

Apex Extension Code:

public class PropertyObjectEnrichmentExtension {
    public Property__c Record {get; set;}
    public class wraperProperty {
        public Property__c prop {get; set;}
        public Boolean selected {get; set;}
        
        public wraperProperty(Property__c property) {
            this.prop = property;
            selected = false;
        }
    }
    public List<wraperProperty> wrapProps {get; set;}
    public List<Property__c> similarProperties {get; set;}
    public Boolean EditMode { get; private set; }
    private Id prop_id;
    
    public PropertyObjectEnrichmentExtension(ApexPages.StandardController controller)
    {
        this.prop_id = controller.getId();
        system.debug('Property Id in controller: '+prop_id);
        if(wrapProps==null){
            System.debug('Running Constructor!!');
            wrapProps = new List<wraperProperty>();
        }
        EditMode = false;
    }
    public void findSimilarPropertiesByEircode(){
        if(Record.Postcode__c.length()==7){
            List<Property__c> props = new List<Property__c>();
            props = [Select Id, MPRN__c, GPRN__c, Address_Full__c,Postcode__c
                     From Property__c Where Postcode__c =: Record.Postcode__c
                     AND Id!=: Record.Id];
            
            for(Property__c pro : props){
                wrapProps.add(new wraperProperty(pro));
            }
        }else{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'No Eircode added by the letting agent!'));
        }
        
        if(wrapProps.size()<1){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning, 'No similar property found!'));
        }
    }
    public PageReference mergeProperties(){
        system.debug('All records in table: '+wrapProps);
        Property__c selectedProp = new Property__c();
        Boolean found = False;
        
        for(wraperProperty wrapProp : wrapProps){
            if(wrapProp.selected == True){
                selectedProp = wrapProp.prop;
                found = True;
                break;
            }
        }
        if(!found){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning, 'No property selected for merge!'));
        }
        else{
            Savepoint sp = Database.setSavepoint();
            //Reparent the property Service of new EasyRent property to existing Switching property
            try{
                List<PropertyService__c> listPropServ = [Select Id,Property__c From PropertyService__c Where Property__c =:Record.Id];
                for(PropertyService__c propServ : listPropServ){
                    propServ.Property__c = selectedProp.Id;
                }
                update listPropServ;
                String fieldsForMerge = 'MyHomePropId__c,Account__c,OwnerId,Bathrooms__c,Bedrooms__c,BER_No__c,BER_Rating__c,BuildingType__c,County_ie__c,GPRN__c,House_No__c,Address_1__c,Address_2__c,Postcode__c,Town__c,Landlord__c,MPRN__c';
                List<String> fields = fieldsForMerge.split(',');
                String existingPropertyQuery ='Select Id,'+fieldsForMerge+'From Property__c where Id=:'+selectedProp.Id;
                String newEasyRentPropertyQuery ='Select Id,'+fieldsForMerge+'From Property__c where Id=:'+Record.Id;
                Property__c existingProperty = database.query(existingPropertyQuery);
                Property__c newEasyRentProperty = database.query(newEasyRentPropertyQuery);
                for(String field: fields){
                    if(newEasyRentProperty.get(field)!=null){
                        if(existingProperty.get(field)!=newEasyRentProperty.get(field)){
                            existingProperty.put(field,newEasyRentProperty.get(field));
                        } 
                    }
                }
                update existingProperty;
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Property Merged!'));
            }catch(exception e){
                Database.rollback(sp);
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Property Merge Failed! '+e.getMessage()));
            }
        }
        return null;
    }
}

Visualforce page is rendering as expected: Before row selection

But on row selection the selected attribute of wraper record is not changing: after row selection

Due to this my mergeProperties() function is not working. Any help would be appreciated.

Thanks

Your Answer

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

Browse other questions tagged or ask your own question.