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 req

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


My req is i want to populate my custom field  "Dealer Principal__c"  on Account exception__c custom object with  the contact first name and last name of the contact  related to the account that i select if the contact title is set as "Dealer Principal" (This is done)

if the account record type is group account , the child accounts contact's name  is set which is having 
contact title as "Dealer Principal"  and not the group account contact  (need help on it)


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;

}


Help me how to get the child account contacts name for account record type is group account

Thanks in Advance
VaasuVaasu
Do you want to get contact details of account which has "group account" record type? If yes then add below condition in SOQL at line 9.

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