• Deek
  • NEWBIE
  • 60 Points
  • Member since 2013

  • Chatter
    Feed
  • 2
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 21
    Questions
  • 78
    Replies
Hi All,

We have a custom check box field in contact as Potential Contact - "Pot_Contact__c".

The requirement is when we add or update a contact for an account and if there is an existing contact with the above check box field checked, it should throw error or otherwise continue saving the contact record.

Also there should be atleast one contact with the check box checked when creating a new or existing contact. 

Hope I am clear in my statement.

The trigger written so far is as below but for some reason its not working as expected. When trying to create a new contact or updating a existing contact I am getting both the error messages and not allowing me to save the contact.

I am sure I have made a  major mistake but cant rectify due to lack of coding expereince. Please help to fix.

trigger PotentialContactCheckbox on Contact (before insert,before update) {
   
set<id> conaccids=new set<id>();
    map<id,List<contact>> relcontactsmap=new map<id,List<contact>>();
    List<contact> cons=new List<contact>();
   
List<Account> accts=new List<Account>();
   
for(contact con:trigger.new){
        conaccids.add(con.AccountId);
    }
     accts=[select id,name from Account where id=:conaccids];
   
for(Account acc:accts){
        id accid=acc.id;
        if (Trigger.isUpdate)
        {
            cons=[select id,Pot_Contact__c from contact where AccountId=:accid  AND id not in :trigger.old];
        }
        else
        {
           cons=[select id,Pot_Contact__c from contact where AccountId=:accid];
        }
        if(cons.size()>0){
          relcontactsmap.put(accid,cons);
        }
    }
  
   
    for(contact con1:trigger.new){
        if((Trigger.isInsert) || (Trigger.isUpdate) ) {
          boolean checkCC=false;
          if(con1.Pot_Contact__c==true){
            if(relcontactsmap.containsKey(con1.AccountId)){
               con1.Pot_Contact__c.adderror('There is already a Potential Contact associated to this Account. Please uncheck and try saving the Contact.');
            }
             checkCC=true;
             break;         
          }
             if(con1.Pot_Contact__c==false){
               if(relcontactsmap.containsKey(con1.AccountId)){
                 con1.Pot_Contact__c.adderror('There has to be one Potential Contact associated to this Account. Please check the check box and try saving the Contact.');
                }
             }
            
       }
   
   
   
   
    }
Hi All,
Below is the trigger to change Contact Owner when the Account Owner changes, applicable only to 2 Account record types and for others it shouldnt change the Contact Owner.

For some reason its changing the contact owner for all account record types.

I am not able to figure out where I have committed this silly mistake if so.

Please help to resolve this.

Thanks in advance.


trigger reassignContactOwner on Account (after update) {
 
      Set<Id> accountIds = new Set<Id>();
      Map<Id, String> oldOwnerIds = new Map<Id, String>();
      Map<Id, String> newOwnerIds = new Map<Id, String>();
      List<Contact> contactsToUpdate = new List<Contact>();
    
      for (Account a : Trigger.new)
      {
         
           if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId)
           {
            oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId);
            newOwnerIds.put(a.Id, a.OwnerId);
            accountIds.add(a.Id);
           }
 
      }
       
         List<Account> accountsWithContacts = [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE
           (RecordTypeId ='012200000004bhnAAA' OR RecordTypeId ='012200000004bhsAAA') // change for these two record types only.
           AND Id in :accountIds];
          

                 for(Account a: accountsWithContacts){
                  
                   for(Contact c: a.Contacts){


                        String newOwnerId = newOwnerIds.get(a.Id);
                        String oldOwnerId = oldOwnerIds.get(a.Id);
           
                        if (c.OwnerId != oldOwnerId)
                        {
                          c.OwnerId=newOwnerId;
                          contactsToUpdate.add(c);
                        }
                    }
           
                }
      
             if (!contactsToUpdate.IsEmpty())
              update contactsToUpdate;
}
Hi All,

Need help in buiding a phone no validation in the following format:

+## # #### ####

For e.g +65 7 1234 5678 with the spaces as shown

I have this regex but somehow its not working as expected. 

Please help.

NOT(REGEX(Phone, "\\+[0-9]{2}\s([0-9]{1}\s[0-9]{4}\s([0-9]{4})")))
Hi All,

We have a custom check box field in contact "Potential_Contact__c".

The requirement is when we add or update a contact for an account and if there is an existing contact with the above check box field checked, it should throw error or otherwise continue saving the contact record.

Below is the apex written so far which was shared by one of the group member.

Issue: I am getting the below custom error message during update operation while updating the contact which has the checked box ticked and the only contact associated to this account.

Please advise where I am doing wrong.



trigger CampaignContactCheckbox on Contact (before insert,before update) {
    set<id> accids=new set<id>();
    map<id,List<contact>> relcontactsmap=new map<id,List<contact>>();
    List<contact> cons=new List<contact>();
    List<Account> accts=new List<Account>();
    for(contact con:trigger.new){
        accids.add(con.AccountId);
    }
accts=[select id,name from Account where id=:accids];  
    for(Account accts1:accts){
        id accid=accts1.id;
        cons=[select id from contact where Accountid=:accid AND Potential_Contact__c=true];
        if(cons.size()>0){
          relcontactsmap.put(accid,cons);
        }
    }
  
    for(contact con1:trigger.new){
        if(Trigger.isInsert) {
         if(con1.Potential_Contact__c==true){
            if(relcontactsmap.containsKey(con1.AccountId)){
               con1.Potential_Contact__c.adderror('There is already a Potential Contact associated to this Account.');
            } 
         }
       }
   
    else if(Trigger.isUpdate) {
      if(con1.Potential_Contact__c==true){
               if(relcontactsmap.containsKey(con1.AccountId)){
                 con1.Potential_Contact__c.adderror('There is already a Potential Contact associated to this Account.');
                } 
            }

    }


}
}
Hi All,

We have a custom check box field in contact "Potential_Contact__c". The requirement is when we add or update a contact for an account and if there is an existing contact with the above check box field checked, it should throw error or otherwise continue saving the contact record.


Below is the apex error written so far. I am getting lot of errors while creating or saving any contact record.

Could you please help me as I am pretty new to Apex.

trigger contactcheckbox on Contact ( before insert,after update) {

        Map<Id, List<Contact>> AcctContactList = new Map<Id, List<Contact>>();
        Set<Id> AcctIds = new Set<Id>(); 

        List<Account> AcctList = new List<Account>();
        List<Contact> ConList = new List<Contact>();
   
    if(trigger.isInsert || trigger.isUPdate) {
        for(Contact Con : trigger.New) {
            AcctIds.add(Con.AccountId);    
        } 
    }
   
   ConList = [SELECT Id, AccountId FROM Contact WHERE Potential_Contact__c=true and AccountId IN : AcctIds];
   
    for(Contact Con : ConList) {
        if(!ConList== Null){
            con.adderror('there is an existing record');
        }
  
}
}
Hi All,

Need help to fine tune the below code as intermittently its throwing error  - System.LimitException: Too many query rows: 50001

This code updates the current date and time on a custom date/time field in Account when the Event Status (custom field) is set to 'Completed' for an Account/Oppty.

I am not getting a clue how to avoid this error, so requesting for a quick solution to refine this code if any.

Thanks in Advance!


trigger updatelastactivitydateinaccount on Event (after insert,after update) {
Set<Id> accIds = new Set<Id> ();
List<Account> lstAcc;


for(Event e: Trigger.new) {
if (e.AccountId != NULL &&  e.Currencyisocode == 'USD') {
    if(e.Activity_Status__c == 'Completed') {
       accIds.add(e.AccountId);
}
   
   
    lstAcc = [Select Last_Activity_Dt__c from Account where  Currencyisocode = 'USD' AND ID IN: accIds];
  
for(Event e: Trigger.new) {
   if(Trigger.isInsert) {
      if(e.Activity_Status__c == 'Completed') {
         for(Account a: lstAcc) {
            if(a.Id == e.AccountId) {
              if(e.StartDateTime>a.Last_Activity_Dt__c){
              a.Last_Activity_Dt__c= e.StartDateTime;
              }
            } 
         }
        
     }
  }

        else if(Trigger.isUpdate) {
          if(e.Activity_Status__c == 'Completed' && Trigger.oldMap.get(e.Id).Activity_Status__c != 'Completed') {
            for(Account a: lstAcc) {
                  if(a.Id == e.AccountId) {
                    if(e.StartDateTime>a.Last_Activity_Dt__c){
                    a.Last_Activity_Dt__c= e.StartDateTime;
                    }
                   }
            }
         }
      }
    }
update lstAcc;
}

The requirement is to stop updating contact owner when the account owner is changed based on below 2 criterias

1.if the account_rating__c='High Rater'