-1

I'm new to developing and need help with the following code. When the status on valuation changes from active to inactive, VL__Active_Valuation__c on monitoring should be cleared out. I already got it working on shareholder but it doesn't work for monitoring. Could someone help me out on this one? Here's the code:

public static void deactivateValuation(List<VL__Valuation__c> valuations){
        
        Set<Id> shareholderIds = new set<Id>();

        for(VL__Valuation__c valuation: valuations){
            shareholderIds.add(valuation.VL__Shareholder__c);
        }

        Map<Id, VL__Shareholder__c> shareholders = new Map<Id,VL__Shareholder__c>([SELECT VL__Active_Valuation__c FROM VL__Shareholder__c WHERE Id IN: shareholderIds]);
        Map<Id, VL__Investment_Monitoring__c> monitorings = new Map<Id, VL__Investment_Monitoring__c>([SELECT VL__Active_Valuation__c FROM VL__Investment_Monitoring__c WHERE VL__Shareholder__c IN: shareholderIds]);
    
        for(VL__Shareholder__c shareholder : shareholders.values()){
            for(VL__Valuation__c valuation: valuations){
                if(shareholder.VL__Active_Valuation__c == valuation.Id){
                    shareholder.VL__Active_Valuation__c = null;
                }
            }
        }
        for(VL__Investment_Monitoring__c monitoring : monitorings.values()){
            for(VL__Valuation__c valuation: valuations){
                if(monitoring.VL__Active_Valuation__c == valuation.Id){
                    monitoring.VL__Active_Valuation__c = null;
                }
            }
        }
        update shareholders.values();
        update monitorings.values();
    }

1 Answer 1

0

You don't need all the shareholders and investment monitoring, you're only interested in those active valuations that need to be deactivated. Most of your logic isn't necessary. Here's the revised version of your code:

public static void deactivateValuation(List<VL__Valuation__c> valuations){
    VL__Shareholder__c[] shareholders = [
        SELECT Id 
        FROM VL__Shareholder__c 
        WHERE VL__Active_Valuation__c = :valuations
    ];
    VL__Investment_Monitoring__c[] monitorings = [
        SELECT Id 
        FROM VL__Investment_Monitoring__c 
        WHERE VL__Active_Valuation__c = :valuations
    ];
    for(VL__Shareholder__c shareholder: shareholders) {
        shareholder.VL__Active_Valuation__c = null;
    }
    for(VL__Investment_Monitoring__c monitoring: monitorings) {
        monitoring.VL__Active_Valuation__c = null;
    }
    update shareholders;
    update monitorings;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .