function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Philip BerryPhilip Berry 

Matching Buying Preferences with Properties to create Potential Buyers

I have a use case that I am creating, and I would like to ask the community on some guidance on the best way to go about creating it.
We have contacts / Accounts with a related  custom object "Preferences" - Preferences are buy preferences for purchasing a property (example - Property Type, Min Price, Size, etc.)
 
We have a custom object "Properties" and when they are put on the market, they become an Opportunity.
 
I am looking for a way to match Contacts / Accounts whom have Preferences that fit the criteria of the Property(Opportunity)
 
So, ideally when you click the opportunity record, there is a tab that has a list of Contact/ Account's preferences that match the criteria of the opportunity and would be labeled as potentials buyers
 
 
Any advice on how to set up this matching?
Best Answer chosen by Philip Berry
Hemant_SoniHemant_Soni
Hi Philip,
 Please update below code with comments and and work on UI and let me know if you have any issue.
<aura:component controller="DispalyPotentialBuyersController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name="recordId" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="listOfBuyers" type="LIST"/>
    <div>
        <div>
            <table class="slds-table slds-table_bordered slds-table_cell-buffer">
                <thead>
                    <tr class="slds-text-title_caps">
                        <th scope="col">
                            <div class="slds-truncate" title="Opportunity Name">Field 1</div>
                        </th>
                        <th scope="col">
                            <div class="slds-truncate" title="Account Name">Field 2</div>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <aura:iteration items="{!v.listOfBuyers}" var="lob">
                        <tr>
                            <td data-label="Name">
                                <div class="slds-truncate" title="{!lob.Subject}">{!lob.Subject}</div>
                            </td>
                            <td data-label="Name">
                                <div class="slds-truncate" title="{!lob.Subject}">{!lob.Subject}</div>
                            </td>
                        </tr>
                    </aura:iteration>
                </tbody>
            </table>
        </div>
    </div>
</aura:component>



({
	doInit : function(component, event, helper) {
        helper.getData(component);
	}
})


({
    getData : function(component) {
        var action = component.get("c.fetchRecords");
        var id = component.get("v.recordId");
        action.setParams({
            "recordIds": id
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (component.isValid() && state === 'SUCCESS') {
                var result = response.getReturnValue();
                component.set('v.listOfBuyers',result);
            } else if (component.isValid() && state === 'ERROR') {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log('Error message: ' + errors[0].message);
                    }
                } else {
                    console.log('Unknown error');
                }
            }
            
        });
        
        $A.enqueueAction(action);
    }
})


public class DispalyPotentialBuyersController {

    @auraEnabled
    public static list<sObject> fetchRecords(String recordIds){
        list<sObject> lstRecordList = new list<sObject>();
        set<Id> setAccount = new set<id>();
        String soql = prepareQueryStr('Account');// Object Name Of Accounts/Contact Related List
        soql += ' Where hemant1993__Opportunity__c =: recordIds';// Replace hemant1993__Opportunity__c Field with Opportunity Reference Field
        for(Account oAccoout : database.query(prepareQueryStr('Account'))){// Object Name Of Accounts/Contact Related List
            setAccount.add(oAccoout.Id);
        }
        soql = prepareQueryStr('Case'); // Object Name Of  Preference
        soql +=  ' Where AccountId IN : setAccount'; //ReplaceAccountId Field with Preference Reference Field
        for(Case oCase : database.query(soql)){
            lstRecordList.add(oCase);
        }
        return lstRecordList;
    }
    
    public static String prepareQueryStr(String objName) {
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        Schema.sObjectType objType = globalDescription.get(objName);
        Schema.DescribeSObjectResult r1 = objType.getDescribe(); 
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  
        
        String queryStr = 'SELECT ';
        for(Schema.SObjectField field : mapFieldList.values()){  
            Schema.DescribeFieldResult fieldResult = field.getDescribe(); 
            if(fieldResult.isAccessible()){
                if(fieldResult.getName() != 'Id' && fieldResult.getName() != 'id' && fieldResult.getName() != 'ID'){ 
                    queryStr += fieldResult.getName() + ', ';
                }
            }  
        }
        
        queryStr += ' Id FROM ' + objName + ' ';
        return queryStr;
    }
}
If This Code helps you please mark is solved and let me know.

Thanks
 

All Answers

Hemant_SoniHemant_Soni
Hey Philip,

According to my understand we can grab Property(Opportunity) id in lighning component and than we can create a lightning component which will showing your matching accounts/ contacts based on opportunity id.

If you are agree with this then i can provide you code of this which probably very easy.

Thanks,
Hemant
Philip BerryPhilip Berry
Hello Hermant,
Yes, this is accurate. Only minor change is that it will be showing the matching Preferences from Accounts/Contacts.

Any help would be much appreciated!
Thank you,
Philip
Hemant_SoniHemant_Soni
Correct me if i am wrong 
You need a custom component in which you need to show preferences of all Accounts /contacts which is in the related list of opportunity.

Thanks
Philip BerryPhilip Berry
Yes! That is correct.
Hemant_SoniHemant_Soni
Hi Philip,
 Please update below code with comments and and work on UI and let me know if you have any issue.
<aura:component controller="DispalyPotentialBuyersController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name="recordId" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="listOfBuyers" type="LIST"/>
    <div>
        <div>
            <table class="slds-table slds-table_bordered slds-table_cell-buffer">
                <thead>
                    <tr class="slds-text-title_caps">
                        <th scope="col">
                            <div class="slds-truncate" title="Opportunity Name">Field 1</div>
                        </th>
                        <th scope="col">
                            <div class="slds-truncate" title="Account Name">Field 2</div>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <aura:iteration items="{!v.listOfBuyers}" var="lob">
                        <tr>
                            <td data-label="Name">
                                <div class="slds-truncate" title="{!lob.Subject}">{!lob.Subject}</div>
                            </td>
                            <td data-label="Name">
                                <div class="slds-truncate" title="{!lob.Subject}">{!lob.Subject}</div>
                            </td>
                        </tr>
                    </aura:iteration>
                </tbody>
            </table>
        </div>
    </div>
</aura:component>



({
	doInit : function(component, event, helper) {
        helper.getData(component);
	}
})


({
    getData : function(component) {
        var action = component.get("c.fetchRecords");
        var id = component.get("v.recordId");
        action.setParams({
            "recordIds": id
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (component.isValid() && state === 'SUCCESS') {
                var result = response.getReturnValue();
                component.set('v.listOfBuyers',result);
            } else if (component.isValid() && state === 'ERROR') {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log('Error message: ' + errors[0].message);
                    }
                } else {
                    console.log('Unknown error');
                }
            }
            
        });
        
        $A.enqueueAction(action);
    }
})


public class DispalyPotentialBuyersController {

    @auraEnabled
    public static list<sObject> fetchRecords(String recordIds){
        list<sObject> lstRecordList = new list<sObject>();
        set<Id> setAccount = new set<id>();
        String soql = prepareQueryStr('Account');// Object Name Of Accounts/Contact Related List
        soql += ' Where hemant1993__Opportunity__c =: recordIds';// Replace hemant1993__Opportunity__c Field with Opportunity Reference Field
        for(Account oAccoout : database.query(prepareQueryStr('Account'))){// Object Name Of Accounts/Contact Related List
            setAccount.add(oAccoout.Id);
        }
        soql = prepareQueryStr('Case'); // Object Name Of  Preference
        soql +=  ' Where AccountId IN : setAccount'; //ReplaceAccountId Field with Preference Reference Field
        for(Case oCase : database.query(soql)){
            lstRecordList.add(oCase);
        }
        return lstRecordList;
    }
    
    public static String prepareQueryStr(String objName) {
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        Schema.sObjectType objType = globalDescription.get(objName);
        Schema.DescribeSObjectResult r1 = objType.getDescribe(); 
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  
        
        String queryStr = 'SELECT ';
        for(Schema.SObjectField field : mapFieldList.values()){  
            Schema.DescribeFieldResult fieldResult = field.getDescribe(); 
            if(fieldResult.isAccessible()){
                if(fieldResult.getName() != 'Id' && fieldResult.getName() != 'id' && fieldResult.getName() != 'ID'){ 
                    queryStr += fieldResult.getName() + ', ';
                }
            }  
        }
        
        queryStr += ' Id FROM ' + objName + ' ';
        return queryStr;
    }
}
If This Code helps you please mark is solved and let me know.

Thanks
 
This was selected as the best answer
Philip BerryPhilip Berry
Thank you so much for taking the time to do this. I am goingt to input my objects/fields and see if I can get it to work. I will let you know if I have any questions. Again, thank you for your help!
Philip BerryPhilip Berry
Hi @Hemant_Soni 
I am sorry to bug you. However, I am having issues getting this code to work. I am just verifying I am coping it in the correct location.  my coments on location are in the code below:
//Component
<aura:component controller="DispalyPotentialBuyersController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name="recordId" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="listOfBuyers" type="LIST"/>
    <div>
        <div>
            <table class="slds-table slds-table_bordered slds-table_cell-buffer">
                <thead>
                    <tr class="slds-text-title_caps">
                        <th scope="col">
                            <div class="slds-truncate" title="Opportunity Name">Field 1</div>
                        </th>
                        <th scope="col">
                            <div class="slds-truncate" title="Account Name">Field 2</div>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <aura:iteration items="{!v.listOfBuyers}" var="lob">
                        <tr>
                            <td data-label="Name">
                                <div class="slds-truncate" title="{!lob.Subject}">{!lob.Subject}</div>
                            </td>
                            <td data-label="Name">
                                <div class="slds-truncate" title="{!lob.Subject}">{!lob.Subject}</div>
                            </td>
                        </tr>
                    </aura:iteration>
                </tbody>
            </table>
        </div>
    </div>
</aura:component>

//Controller

({
	doInit : function(component, event, helper) {
        helper.getData(component);
	}
})

//Helper
({
    getData : function(component) {
        var action = component.get("c.fetchRecords");
        var id = component.get("v.recordId");
        action.setParams({
            "recordIds": id
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (component.isValid() && state === 'SUCCESS') {
                var result = response.getReturnValue();
                component.set('v.listOfBuyers',result);
            } else if (component.isValid() && state === 'ERROR') {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log('Error message: ' + errors[0].message);
                    }
                } else {
                    console.log('Unknown error');
                }
            }
            
        });
        
        $A.enqueueAction(action);
    }
})

//Renderer
public class DispalyPotentialBuyersController {

    @auraEnabled
    public static list<sObject> fetchRecords(String recordIds){
        list<sObject> lstRecordList = new list<sObject>();
        set<Id> setAccount = new set<id>();
        String soql = prepareQueryStr('Account');// Object Name Of Accounts/Contact Related List
        soql += ' Where hemant1993__Opportunity__c =: recordIds';// Replace hemant1993__Opportunity__c Field with Opportunity Reference Field
        for(Account oAccoout : database.query(prepareQueryStr('Account'))){// Object Name Of Accounts/Contact Related List
            setAccount.add(oAccoout.Id);
        }
        soql = prepareQueryStr('Case'); // Object Name Of  Preference
        soql +=  ' Where AccountId IN : setAccount'; //ReplaceAccountId Field with Preference Reference Field
        for(Case oCase : database.query(soql)){
            lstRecordList.add(oCase);
        }
        return lstRecordList;
    }
    
//Not Sure?
    public static String prepareQueryStr(String objName) {
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        Schema.sObjectType objType = globalDescription.get(objName);
        Schema.DescribeSObjectResult r1 = objType.getDescribe(); 
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  
        
        String queryStr = 'SELECT ';
        for(Schema.SObjectField field : mapFieldList.values()){  
            Schema.DescribeFieldResult fieldResult = field.getDescribe(); 
            if(fieldResult.isAccessible()){
                if(fieldResult.getName() != 'Id' && fieldResult.getName() != 'id' && fieldResult.getName() != 'ID'){ 
                    queryStr += fieldResult.getName() + ', ';
                }
            }  
        }
        
        queryStr += ' Id FROM ' + objName + ' ';
        return queryStr;
    }
}

I am sorry, I am new to components. Can you advise. Thank you so much for your time

 
Hemant_SoniHemant_Soni
Hi Philip,

This is complete code and it gose in apex controller not in render.
//Apex Controller
public class DispalyPotentialBuyersController {

    @auraEnabled
    public static list<sObject> fetchRecords(String recordIds){
        list<sObject> lstRecordList = new list<sObject>();
        set<Id> setAccount = new set<id>();
        String soql = prepareQueryStr('Account');// Object Name Of Accounts/Contact Related List
        soql += ' Where hemant1993__Opportunity__c =: recordIds';// Replace hemant1993__Opportunity__c Field with Opportunity Reference Field
        for(Account oAccoout : database.query(prepareQueryStr('Account'))){// Object Name Of Accounts/Contact Related List
            setAccount.add(oAccoout.Id);
        }
        soql = prepareQueryStr('Case'); // Object Name Of  Preference
        soql +=  ' Where AccountId IN : setAccount'; //ReplaceAccountId Field with Preference Reference Field
        for(Case oCase : database.query(soql)){
            lstRecordList.add(oCase);
        }
        return lstRecordList;
    }
    public static String prepareQueryStr(String objName) {
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        Schema.sObjectType objType = globalDescription.get(objName);
        Schema.DescribeSObjectResult r1 = objType.getDescribe(); 
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  
        
        String queryStr = 'SELECT ';
        for(Schema.SObjectField field : mapFieldList.values()){  
            Schema.DescribeFieldResult fieldResult = field.getDescribe(); 
            if(fieldResult.isAccessible()){
                if(fieldResult.getName() != 'Id' && fieldResult.getName() != 'id' && fieldResult.getName() != 'ID'){ 
                    queryStr += fieldResult.getName() + ', ';
                }
            }  
        }
        
        queryStr += ' Id FROM ' + objName + ' ';
        return queryStr;
    }
}

 
Hemant_SoniHemant_Soni
Hi Philip,

If Your Problem has been resolved then please mark is solved.

Thanks,
Hemant
Philip BerryPhilip Berry
Hi Hermant,
Thank you so much for your time and efforts. I will for sure mark this as solved and give you credit once I get it working. I am still having issues pulling in the fields to match.
Example:
Opportunities:
Record Example: Opportunity A:
Property Size- 1500 sqft
Property Price - $300,000
Property Location- Richmond, VA

Match to Contact Preference
Record Example: Preference BB:
Min size- 1,000 SqFt
Max Size- 3,000 SqFt
Min Price- $200,000
Max Price- $400,000
Property Area- Richmond

Opportunity Page- Match opportunity (Property) with Contacts who have preferences matching this criteria. When I click on a tab on Opportunity A: Preference BB should show up because it's buying preferences match the property criteria.w

I am still understanding the code. But as an example, this is what I am trying to accomplish
 
Hemant_SoniHemant_Soni
Hi Philip,
 if you dont have any problem then we can connect on skype so we can discussed over there and i have sufficient access then i will made change in code infont of you.
I am available every day after 9:00 pm IST.
My Skype Id : sonihemant26

Thanks,
Hemant