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
Asel Vazir 1Asel Vazir 1 

Trying to copy custom fields' values from related list on the same custom fields on Opportunity object

I looked for similar topics and used some best answers to modify my code, but it is still not populating the custom fields on Opportunity every time the custom fields on related list Property are entered. Please help me to resolve the issue.
Here is the class:
public with sharing class PropertyEscrowContactOnExchange {
public static void copyPropertyEscrowAddressOnExchange(List<Property__c> propList) {

    Set<String> oppIds = new Set<String>();

    for (Property__c prop : propList) {
        oppIds.add(prop.Id);
    }
    List<Opportunity> opportunities = [SELECT Id, Name, Escrow_Company__c, Escrow_Address__c, Escrow_City__c, Escrow_State__c, Escrow_Zip_Code__c
                                        FROM Opportunity WHERE Id IN : oppIds];

    Map<String, Opportunity> oppByStrings = new Map<String, Opportunity>();
    for(Opportunity o : opportunities){
        oppByStrings.put(o.Escrow_Company__c + o.Escrow_Address__c + o.Escrow_City__c + o.Escrow_State__c+ o.Escrow_Zip_Code__c, o);
    }

    for (Property__c p : propList) {
        Opportunity o = oppByStrings.get(p.Id);
        if (o == null && oppByStrings.containsKey(p.Id) && oppByStrings.get(p.Id).Properties__r.size()>0) {
            p.Escrow_Company__c = o.Escrow_Company__c;
            p.Escrow_Address__c = o.Escrow_Address__c;
            p.Escrow_City__c = o.Escrow_City__c;
            p.Escrow_State__c = o.Escrow_State__c;
            p.Escrow_Zip_Code__c = o.Escrow_Zip_Code__c;
        }
    }
}

And my trigger:
 
trigger copyPropEscrowAddressOnExchange on Property__c (after insert, after update) {
if (Trigger.isInsert || Trigger.isUpdate){
    PropertyEscrowContactOnExchange.copyPropertyEscrowAddressOnExchange(Trigger.new);
}

​​​​​​​
Danish HodaDanish Hoda
Hi there,
Please update your code in Apex class line #6, 7, 8 as below:
for (Property__c prop : propList) {
        oppIds.add(prop.Opportunity__r.Id);     //I assume Opportunity__c is the field API name on Property__c
    }