public with sharing class PropertyDataController { @AuraEnabled(cacheable=true) public static List getProperties() { try { System.debug('=== FETCHING ALL PROPERTIES FROM PCRM OBJECT ==='); // Query using fields that are actually available in pcrm__Property__c String query = 'SELECT Id, Name, ' + 'pcrm__Property_Type__c, pcrm__Status__c, ' + 'pcrm__Bathrooms__c, pcrm__Bedrooms__c, pcrm__Size__c, ' + 'pcrm__Sale_Price_min__c, pcrm__Sale_Price_max__c, ' + 'pcrm__Rent_Price_min__c, pcrm__Rent_Price_max__c, ' + 'pcrm__Description_English__c, pcrm__Title_English__c, ' + 'pcrm__City_Bayut_Dubizzle__c, pcrm__Community_Propertyfinder__c, ' + 'pcrm__Furnished__c, pcrm__Floor__c, pcrm__Build_Year__c, ' + 'pcrm__Parking_Spaces__c, pcrm__Offering_Type__c, ' + 'pcrm__Unit_Number__c, pcrm__Locality_Bayut_Dubizzle__c, ' + 'pcrm__Sub_Locality_Bayut_Dubizzle__c, pcrm__Tower_Bayut_Dubizzle__c, ' + 'pcrm__Sub_Community_Propertyfinder__c, pcrm__Property_Name_Propertyfinder__c, ' + 'pcrm__City_Propertyfinder__c, ' + 'pcrm__Rent_Available_From__c, pcrm__Rent_Available_To__c, ' + 'Private_Amenities__c, ' + 'Contact__c, Contact__r.FirstName, Contact__r.LastName, Contact__r.Email, Contact__r.Phone, ' + 'Email__c, Phone__c, ' + 'CreatedBy.Name, LastModifiedBy.Name, Owner.Name, ' + 'CreatedDate, LastModifiedDate ' + 'FROM pcrm__Property__c ' + 'ORDER BY Name ASC'; List properties = Database.query(query); System.debug('=== PROPERTIES FETCHED FROM PCRM ==='); System.debug('Total properties found: ' + properties.size()); // Log first property details for debugging if (!properties.isEmpty()) { pcrm__Property__c firstProp = properties[0]; System.debug('First property details:'); System.debug('Name: ' + firstProp.Name); System.debug('Type: ' + firstProp.pcrm__Property_Type__c); System.debug('Status: ' + firstProp.pcrm__Status__c); System.debug('Bedrooms: ' + firstProp.pcrm__Bedrooms__c); System.debug('Bathrooms: ' + firstProp.pcrm__Bathrooms__c); System.debug('Size: ' + firstProp.pcrm__Size__c); System.debug('Sale Price Min: ' + firstProp.pcrm__Sale_Price_min__c); System.debug('Sale Price Max: ' + firstProp.pcrm__Sale_Price_max__c); System.debug('Rent Price Min: ' + firstProp.pcrm__Rent_Price_min__c); System.debug('Rent Price Max: ' + firstProp.pcrm__Rent_Price_max__c); System.debug('City: ' + firstProp.pcrm__City_Bayut_Dubizzle__c); System.debug('Community: ' + firstProp.pcrm__Community_Propertyfinder__c); System.debug('Description: ' + firstProp.pcrm__Description_English__c); System.debug('Title: ' + firstProp.pcrm__Title_English__c); System.debug('Furnished: ' + firstProp.pcrm__Furnished__c); System.debug('Floor: ' + firstProp.pcrm__Floor__c); System.debug('Build Year: ' + firstProp.pcrm__Build_Year__c); System.debug('Parking Spaces: ' + firstProp.pcrm__Parking_Spaces__c); System.debug('Offering Type: ' + firstProp.pcrm__Offering_Type__c); System.debug('Private Amenities: ' + firstProp.Private_Amenities__c); } return properties; } catch (Exception e) { System.debug('Error fetching properties: ' + e.getMessage()); System.debug('Stack trace: ' + e.getStackTraceString()); throw new AuraHandledException('Failed to fetch properties: ' + e.getMessage()); } } @AuraEnabled(cacheable=true) public static pcrm__Property__c getPropertyDetails(String propertyId) { try { System.debug('=== FETCHING COMPLETE PROPERTY DETAILS FROM PCRM ==='); System.debug('Property ID: ' + propertyId); // Query using fields that are actually available in pcrm__Property__c String query = 'SELECT Id, Name, ' + 'pcrm__Property_Type__c, pcrm__Status__c, ' + 'pcrm__Bathrooms__c, pcrm__Bedrooms__c, pcrm__Size__c, ' + 'pcrm__Sale_Price_min__c, pcrm__Sale_Price_max__c, ' + 'pcrm__Rent_Price_min__c, pcrm__Rent_Price_max__c, ' + 'pcrm__Description_English__c, pcrm__Title_English__c, ' + 'pcrm__City_Bayut_Dubizzle__c, pcrm__Community_Propertyfinder__c, ' + 'pcrm__Furnished__c, pcrm__Floor__c, pcrm__Build_Year__c, ' + 'pcrm__Parking_Spaces__c, pcrm__Offering_Type__c, ' + 'pcrm__Unit_Number__c, pcrm__Locality_Bayut_Dubizzle__c, ' + 'pcrm__Sub_Locality_Bayut_Dubizzle__c, pcrm__Tower_Bayut_Dubizzle__c, ' + 'pcrm__Sub_Community_Propertyfinder__c, pcrm__Property_Name_Propertyfinder__c, ' + 'pcrm__City_Propertyfinder__c, ' + 'pcrm__Rent_Available_From__c, pcrm__Rent_Available_To__c, ' + 'Private_Amenities__c, ' + 'Contact__c, Contact__r.FirstName, Contact__r.LastName, Contact__r.Email, Contact__r.Phone, ' + 'Email__c, Phone__c, ' + 'CreatedBy.Name, LastModifiedBy.Name, Owner.Name, ' + 'CreatedDate, LastModifiedDate ' + 'FROM pcrm__Property__c ' + 'WHERE Id = :propertyId'; pcrm__Property__c property = Database.query(query); if (property != null) { System.debug('=== PROPERTY DETAILS FETCHED FROM PCRM ==='); System.debug('Name: ' + property.Name); System.debug('Type: ' + property.pcrm__Property_Type__c); System.debug('Status: ' + property.pcrm__Status__c); System.debug('Bedrooms: ' + property.pcrm__Bedrooms__c); System.debug('Bathrooms: ' + property.pcrm__Bathrooms__c); System.debug('Size: ' + property.pcrm__Size__c); System.debug('Sale Price Min: ' + property.pcrm__Sale_Price_min__c); System.debug('Sale Price Max: ' + property.pcrm__Sale_Price_max__c); System.debug('Rent Price Min: ' + property.pcrm__Rent_Price_min__c); System.debug('Rent Price Max: ' + property.pcrm__Rent_Price_max__c); System.debug('City: ' + property.pcrm__City_Bayut_Dubizzle__c); System.debug('Community: ' + property.pcrm__Community_Propertyfinder__c); System.debug('Description: ' + property.pcrm__Description_English__c); System.debug('Title: ' + property.pcrm__Title_English__c); System.debug('Furnished: ' + property.pcrm__Furnished__c); System.debug('Floor: ' + property.pcrm__Floor__c); System.debug('Build Year: ' + property.pcrm__Build_Year__c); System.debug('Parking Spaces: ' + property.pcrm__Parking_Spaces__c); System.debug('Offering Type: ' + property.pcrm__Offering_Type__c); System.debug('Private Amenities: ' + property.Private_Amenities__c); } return property; } catch (Exception e) { System.debug('Error fetching property details: ' + e.getMessage()); System.debug('Stack trace: ' + e.getStackTraceString()); throw new AuraHandledException('Failed to fetch property details: ' + e.getMessage()); } } @AuraEnabled(cacheable=true) public static Integer getPropertyCount() { try { return [SELECT COUNT() FROM pcrm__Property__c]; } catch (Exception e) { System.debug('Error getting property count: ' + e.getMessage()); return 0; } } @AuraEnabled(cacheable=true) public static List> getPropertyImages(String propertyId) { try { System.debug('=== FETCHING PROPERTY IMAGES ==='); System.debug('Property ID: ' + propertyId); List> images = new List>(); // Query Image Genie records for this property List imageRecords = [ SELECT Id, Name, pcrm__Category__c, pcrm__Title__c, Public_URL__c, pcrm__Property__c FROM pcrm__Image_Genie__c WHERE pcrm__Property__c = :propertyId ORDER BY pcrm__Category__c, Name ]; System.debug('Found ' + imageRecords.size() + ' image records'); for (pcrm__Image_Genie__c img : imageRecords) { Map imageData = new Map(); imageData.put('id', img.Id); imageData.put('name', img.pcrm__Title__c); imageData.put('category', img.pcrm__Category__c); imageData.put('url', img.Public_URL__c); images.add(imageData); System.debug('Image: ' + img.pcrm__Title__c + ' - Category: ' + img.pcrm__Category__c + ' - URL: ' + img.Public_URL__c); } return images; } catch (Exception e) { System.debug('Error fetching property images: ' + e.getMessage()); System.debug('Stack trace: ' + e.getStackTraceString()); throw new AuraHandledException('Failed to fetch property images: ' + e.getMessage()); } } @AuraEnabled(cacheable=true) public static User getAgentData(String propertyId) { try { System.debug('=== FETCHING AGENT DATA FOR PROPERTY ==='); System.debug('Property ID: ' + propertyId); // First, get the property to find the related agent/owner pcrm__Property__c property = [ SELECT Id, OwnerId, CreatedById, LastModifiedById, Contact__c, Contact__r.OwnerId FROM pcrm__Property__c WHERE Id = :propertyId LIMIT 1 ]; if (property == null) { System.debug('Property not found for ID: ' + propertyId); return null; } // Try to get agent data from different sources in priority order User agentUser = null; // Priority 1: Contact's owner (if contact exists) if (property.Contact__c != null && property.Contact__r.OwnerId != null) { try { agentUser = [ SELECT Id, Name, FirstName, LastName, Email, Phone, MobilePhone, Title, Department, CompanyName, SmallPhotoUrl, FullPhotoUrl, Profile.Name, UserRole.Name FROM User WHERE Id = :property.Contact__r.OwnerId AND IsActive = true LIMIT 1 ]; System.debug('Found agent from Contact Owner: ' + agentUser?.Name); } catch (Exception e) { System.debug('Error fetching Contact Owner: ' + e.getMessage()); } } // Priority 2: Property Owner (if not found above) if (agentUser == null && property.OwnerId != null) { try { agentUser = [ SELECT Id, Name, FirstName, LastName, Email, Phone, MobilePhone, Title, Department, CompanyName, SmallPhotoUrl, FullPhotoUrl, Profile.Name, UserRole.Name FROM User WHERE Id = :property.OwnerId AND IsActive = true LIMIT 1 ]; System.debug('Found agent from Property Owner: ' + agentUser?.Name); } catch (Exception e) { System.debug('Error fetching Property Owner: ' + e.getMessage()); } } // Priority 3: Property Creator (if not found above) if (agentUser == null && property.CreatedById != null) { try { agentUser = [ SELECT Id, Name, FirstName, LastName, Email, Phone, MobilePhone, Title, Department, CompanyName, SmallPhotoUrl, FullPhotoUrl, Profile.Name, UserRole.Name FROM User WHERE Id = :property.CreatedById AND IsActive = true LIMIT 1 ]; System.debug('Found agent from Property Creator: ' + agentUser?.Name); } catch (Exception e) { System.debug('Error fetching Property Creator: ' + e.getMessage()); } } if (agentUser != null) { System.debug('=== AGENT DATA FETCHED ==='); System.debug('Agent Name: ' + agentUser.Name); System.debug('Agent Email: ' + agentUser.Email); System.debug('Agent Phone: ' + agentUser.Phone); System.debug('Agent Title: ' + agentUser.Title); System.debug('Agent Department: ' + agentUser.Department); } else { System.debug('No agent found for property: ' + propertyId); } return agentUser; } catch (Exception e) { System.debug('Error fetching agent data: ' + e.getMessage()); System.debug('Stack trace: ' + e.getStackTraceString()); throw new AuraHandledException('Failed to fetch agent data: ' + e.getMessage()); } } @AuraEnabled(cacheable=true) public static pcrm__Listing__c getListingData(String propertyId) { try { System.debug('=== FETCHING LISTING DATA BY PROPERTY ==='); System.debug('Property ID: ' + propertyId); // Query listing with related property and agent data using property ID String query = 'SELECT Id, Name, ' + 'Property__r.Id, Property__r.Name, ' + 'Listing_Agent__r.Id, Listing_Agent__r.Name, Listing_Agent__r.Email, Listing_Agent__r.Phone, Listing_Agent__r.Title, ' + 'Select_Agent__r.Id, Select_Agent__r.Name, Select_Agent__r.Email, Select_Agent__r.Phone, Select_Agent__r.Title ' + 'FROM pcrm__Listing__c ' + 'WHERE Property__c = :propertyId ' + 'ORDER BY CreatedDate DESC LIMIT 1'; List listings = Database.query(query); pcrm__Listing__c listing = listings.isEmpty() ? null : listings[0]; if (listing != null) { System.debug('=== LISTING DATA FETCHED ==='); System.debug('Listing Name: ' + listing.Name); System.debug('Property ID: ' + listing.Property__r?.Id); System.debug('Property Name: ' + listing.Property__r?.Name); System.debug('Listing Agent ID: ' + listing.Listing_Agent__r?.Id); System.debug('Listing Agent Name: ' + listing.Listing_Agent__r?.Name); System.debug('Listing Agent Email: ' + listing.Listing_Agent__r?.Email); System.debug('Listing Agent Phone: ' + listing.Listing_Agent__r?.Phone); System.debug('Select Agent ID: ' + listing.Select_Agent__r?.Id); System.debug('Select Agent Name: ' + listing.Select_Agent__r?.Name); } else { System.debug('No listing found for Property ID: ' + propertyId); } return listing; } catch (Exception e) { System.debug('Error fetching listing data: ' + e.getMessage()); System.debug('Stack trace: ' + e.getStackTraceString()); throw new AuraHandledException('Failed to fetch listing data: ' + e.getMessage()); } } }