• Mayur Naidu 17
  • NEWBIE
  • 25 Points
  • Member since 2019

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 5
    Replies
I have written a trigger where if the user attempts to make a 2nd related contact on the account object a Primary Contact too, an error is thrown.  I am trying to add syntax to my trigger so if the user adds a related contact to an Account that currently has 0 related contacts, this new contact will automatically become the primary contact.  This is the code I have so far:
Trigger PrimaryContactTrigger on Contact (Before Insert, Before Update) {

    List<Id> actIds = New List<Id>();
    List<Contact> comingCons = New List<Contact>();
    List<Id> conIds = New List<Id>();

    If (Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate)) {
        For (Contact Con : Trigger.New) {
            If (Con.IsPrimary__c == TRUE) {
                actIds.add(Con.AccountId);
                conIds.add(Con.Id);
                comingCons.add(Con);
            }
            }
        }
        List<Account> allRelatedAccounts = [
                Select Id, (
                        Select Id, IsPrimary__c
                        FROM Contacts
                        WHERE IsPrimary__c = TRUE
                        AND Id != :conIds
                )
                FROM Account
                WHERE Id = :actIds
        ];
        For (Contact EveryCon : comingCons) {
            For (Account EveryAccount : allRelatedAccounts) {
                If (EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0) {
                    EveryCon.addError('THERE is already a primary contact for this account');
                }
            }
        }
    }

 
Hello, I wanted to know if we use a Database.update() method in an Apex class for an sObject, does this update invoke 'before update' Apex trigger on that sObject?

Thanks for you help.
    if (clmMap.size() > 0){
        for(Clm_Presentation_vod__c clm : clmMap.values()){
            for(Integer i = 1; i < clmMap.size(); i++){
                Integer version1 = Integer.valueOf(clmMap.get(clm[i].Version_vod__c));
                Integer version2 = Integer.valueOf(clmMap.get(clm[i-1].Version_vod__c));
                   String docId1 = clmMap.get(clm[i].Vault_Doc_Id_vod__c);
                String docId2 = clmMap.get(clm[i-1].Vault_Doc_Id_vod__c);
                if (docId1 == docId2){
                    if (version1 > Version2){
                        Trigger.newMap.get(clm[i].Show_Offline_abv__c = True);
                        Trigger.newMap.get(clm[i-1].Show_Offline_abv__c = false);
                    }else {
                        Trigger.newMap.get(clm[i].Show_Offline_abv__c) = false;
                        Trigger.newMap.get(clm[i-1].Show_Offline_abv__c = True);
                    }           
                }
            }
       }
    }
Hello, I wanted to know if we use a Database.update() method in an Apex class for an sObject, does this update invoke 'before update' Apex trigger on that sObject?

Thanks for you help.
    if (clmMap.size() > 0){
        for(Clm_Presentation_vod__c clm : clmMap.values()){
            for(Integer i = 1; i < clmMap.size(); i++){
                Integer version1 = Integer.valueOf(clmMap.get(clm[i].Version_vod__c));
                Integer version2 = Integer.valueOf(clmMap.get(clm[i-1].Version_vod__c));
                   String docId1 = clmMap.get(clm[i].Vault_Doc_Id_vod__c);
                String docId2 = clmMap.get(clm[i-1].Vault_Doc_Id_vod__c);
                if (docId1 == docId2){
                    if (version1 > Version2){
                        Trigger.newMap.get(clm[i].Show_Offline_abv__c = True);
                        Trigger.newMap.get(clm[i-1].Show_Offline_abv__c = false);
                    }else {
                        Trigger.newMap.get(clm[i].Show_Offline_abv__c) = false;
                        Trigger.newMap.get(clm[i-1].Show_Offline_abv__c = True);
                    }           
                }
            }
       }
    }
I have written a trigger where if the user attempts to make a 2nd related contact on the account object a Primary Contact too, an error is thrown.  I am trying to add syntax to my trigger so if the user adds a related contact to an Account that currently has 0 related contacts, this new contact will automatically become the primary contact.  This is the code I have so far:
Trigger PrimaryContactTrigger on Contact (Before Insert, Before Update) {

    List<Id> actIds = New List<Id>();
    List<Contact> comingCons = New List<Contact>();
    List<Id> conIds = New List<Id>();

    If (Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate)) {
        For (Contact Con : Trigger.New) {
            If (Con.IsPrimary__c == TRUE) {
                actIds.add(Con.AccountId);
                conIds.add(Con.Id);
                comingCons.add(Con);
            }
            }
        }
        List<Account> allRelatedAccounts = [
                Select Id, (
                        Select Id, IsPrimary__c
                        FROM Contacts
                        WHERE IsPrimary__c = TRUE
                        AND Id != :conIds
                )
                FROM Account
                WHERE Id = :actIds
        ];
        For (Contact EveryCon : comingCons) {
            For (Account EveryAccount : allRelatedAccounts) {
                If (EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0) {
                    EveryCon.addError('THERE is already a primary contact for this account');
                }
            }
        }
    }