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
SFDC n12SFDC n12 

Trigger Help needed

Hi,

I need help on the following requirement,


1) I am having a custom field called as "Dealer Principal__c" in my custom object called as "Account Exceptions__c"

2) There is a lookup on my custom object called as Account__c which is a lookup to the account object



1) if the account is a nornmal account (PDN Record type) , the contact(name) associated with that account with title "Dealer Principal" is populated to that field


2) If the account is a group account


the group account might have many child accounts , where it will check the contacts of all th e child accounts with the title "Dealer principal" and update the name correspondingly



so my case should work for both the scenarios

First scenario my trigger is working fine , just need help on second one


MY TRIGGER :

trigger updateaccountexceptions on AccountExceptions__c(after insert) {
    map < id, string > dealercontacts = new map < id, string > ();
    set < id > accids = new set < id > ();

    for (AccountExceptions__c acex: trigger.new) {
        accids.add(acex.account__c);
    }

    List < contact > cons = new List < contact > ([select Id, accountid, firstname, lastname from contact where title = 'Dealer Principal'
        and accountid = : accids
    ]);

    for (Contact con: cons) {
        string conname = con.firstname + ' ' + con.lastname;
        dealercontacts.put(con.accountid, conname);
    }

    list < AccountExceptions__c > acexlisttoupdate = new list < AccountExceptions__c > ();

    for (AccountExceptions__c acex: trigger.new) {
        AccountExceptions__c accex1 = new AccountExceptions__c();
        if (dealercontacts.containskey(acex.account__c)) {
            accex1.id = acex.id;
            accex1.Dealer_Principal_s__c = dealercontacts.get(acex.account__c);
            acexlisttoupdate.add(accex1);
        }
    }

    update acexlisttoupdate;

}


Thanks in Advance
SamuelDeRyckeSamuelDeRycke
What kind of help do you need ? What's stopping you from extending the code with extra functionality ?