0

I have custom fields of total_number__c and total child contacts as Total_money__c in Account object and a custom field IndividualMoney__c in Contact. My aim is to distribute total amount to individual money equally between contacts.

Error facing :

Compile Error: Variable does not exist: Total_money__c at line 22 column 42

trigger sharem on Contact(after insert, after update) {

    list < id > aid = new list < id > ();

    for (Contact cont: trigger.new) {

        aid.add(cont.accountid);

        list < Account > acc = [select id, name, total_number__c, Total_money__c from Account where id in: aid];
        list < Contact > co = [select id, name, IndividualMoney__c from contact where Accountid in: acc];
        list < Contact > updatco = new list < Contact > ();

        for (Contact upc: co) {
            upc.IndividualMoney__c = acc.Total_money__c / acc.total_number__c;
            updatco.add(upc);
        }
    }
    upsert updatco;
}
0

This is a syntax error. You are referencing fields on Account, Total_money__c and total_number__c, in a variable (acc) whose type is List<Account>.

You could fix this by extracting a single account from your list, but your trigger needs to be completely refactored to remove SOQL from the loop and bulkify the trigger. Please consider working through the entire Apex Triggers module on Trailhead and starting fresh with your queries outside the loop.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.