• Deek
  • NEWBIE
  • 60 Points
  • Member since 2013

  • Chatter
    Feed
  • 2
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 21
    Questions
  • 78
    Replies
We are availing the facility of email-to-case in our organization. Everytime a case is created through email, a task is getting created automatically and its getting assigned to the current user.

Is there any settings to stop creating these tasks at all? If not, is there any way to get these removed automatically either immediately after creation or periodically?
  • January 20, 2016
  • Like
  • 0
Hello,
I am looking into various ways to archive email messages out of salesforce. As we all know it consumes a lot of data storage and buying additional storage is expensive. I need to know, how you all archive your email messages to get a fair idea of a future solution.

Appreciate all you inputs.
  • October 29, 2015
  • Like
  • 1
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.');
                }
             }
            
       }
   
   
   
   
    }
  • May 22, 2014
  • Like
  • 0
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;
}
  • April 28, 2014
  • Like
  • 0
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})")))
  • April 08, 2014
  • Like
  • 0
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.');
                } 
            }

    }


}
}
  • April 01, 2014
  • Like
  • 0
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');
        }
  
}
}
  • March 26, 2014
  • Like
  • 0
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;
}
  • January 29, 2014
  • Like
  • 0

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'


  • November 21, 2013
  • Like
  • 0

Hi All,

 

Below is the trigger to update contact owner when account owner changes if the field is Unchecked otherwise no contact owner change.

 

 

  • November 20, 2013
  • Like
  • 0

Hi Ya,

 

I am trying to run a upsert operation using data loader for Campaign Object but its not available to select. Its only available for Export finctionality.

 

I can see the Campaign object when I connect data loader to the sand box environment. Please advise whether this is expected behavior.

 

I have around 1K records to be updated, so is there anyway I can run a upsert operation for Campaigns.

 

Please help.

 

Regards,

Dpat

  • September 08, 2013
  • Like
  • 0

Hi All,

 

In Campaign object, there are 4 custom fields where I need to check the uniqueness as a group before its commited to the database

 

Field 1 - Custom Picklist

Field 2-Custom Picklist

Field 3-Custom Picklist

Field 4-Custom Date

 

If the above 4 fields are unique then only it will allow to save the Campaign record.

 

Please help to how it can be achievable.

 

At the moment I have thought of creating a dummy field of type TEXT and making it as unique.

Using Workflow field update I will concatenate the above 4 fields and store the values in the dummy field.

If its unique then only it will allow to save otherwise throw error as its unique.

 

Is that the right approach? Please advise how do I concatenate the 4 fields in the formula editor.

  • July 26, 2013
  • Like
  • 0

Hi Ya,

 

I need to create a Mant to Many relationship between Accounts and Campaigns. 

 

Please suggest the schema design for this approach.

 

Till now I understand, that we might need to create a junction object say AccountCampaign between Accounts and Campaigns and create master detail relation to Account and Campaign on this object respectively.

 

Is that the correct approach? Please suggest the best approach and solution.

 

Thanks

Dpat

  • July 04, 2013
  • Like
  • 0

Hi There,

 

I am creating a custom object with data type as Auto-Number with the below parameters primarily. It throws error at Starting Number field as "Error: Invalid number"

 

Display Format-->APN - {00000000}{DD}{MM}{YYYY}

Starting Number-->APN - {12345678}{12}{12}{2012}

 

Please help me to resolve this. What is the invalid number in "Starting Number"?

  • July 01, 2013
  • Like
  • 0

Hi,

 

Below is the trigger written on Accounts to send email to the old account owner whenever there is a ownership change.

 

The trigger got saved, but from UI when you change the account owner below is the error thrown at line 5 and 6.

 

Error: Apex trigger SendEmailOnOwnerChange caused an unexpected exception, contact your administrator: SendEmailOnOwnerChange: execution of AfterUpdate

caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.SendEmailOnOwnerChange: line 5,6 column 1

 

Please help me out to fix the issue.

 

trigger SendEmailOnOwnerChange on Account (after update) {

    if (trigger.old[0].OwnerId != trigger.new[0].OwnerId) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        String emailAddr = [select Email from User where Division != 'NZ' AND Id = :trigger.old[0].OwnerId].Email;
        String newOwnerName = [select Name from User where Division != 'NZ' AND Id = :trigger.new[0].OwnerId].Name;

        String[] toAddresses = new String[] {emailAddr};
        mail.setToAddresses(toAddresses);

        mail.setSubject('ALERT-Owner Changed for Account : ' + trigger.new[0].Name);

        mail.setPlainTextBody('Owner of Account: ' + trigger.new[0].Name + ' Changed to ' + newOwnerName);
        mail.setHtmlBody('Owner of Account: <b>' + trigger.new[0].Name + '</b> Changed to <b>' + newOwnerName  + '</b>');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

  • May 29, 2013
  • Like
  • 0

I have a custom date field "APN Last Activity Date" in Account. There is another custom picklist field in Activity called as "APN Activity Status" with values as planned,completed,cancelled and system close

 

My requirement is to populate APN Last ctivity Date field with the current date whenever the APN Activity Status is changed to "Completed with some other validations.

 

Below is the code:

 

trigger UpdateAccountYORDateNew on Event (before insert,before update) {
Set<Id> accIds = new Set<Id> ();
Set<Id> OppIds = new Set<Id> ();
List<Account> lstAcc,lstAcc1;
List<Opportunity> lstOpp;

 

if(lstOpp !=NULL){
For (Opportunity o :lstOpp )
{

OppIds.add(o.AccountId) ;
}
}

for(Event e: Trigger.new) {
if (e.WhatId != NULL) {

if(String.valueOf(e.WhatId).startsWith('001') && e.APN_Activity_Status__c == 'Completed') {
accIds.add(e.WhatId);

}

if((e.StartDateTime > System.today() && e.APN_Activity_Status__c== 'Completed')) {
e.addError('If APN Activity Status is "Completed", Cannot set the Start Date/Time to a Future Date.');
}
if ((e.APN_Activity_Status__c== 'Completed' && e.Description == NULL)){
e.addError('If APN Activity Status is "Completed", Description is Mandatory.');}

}
}
lstAcc = [Select APN_Last_Activity_Date__c from Account where ID IN: accIds];
lstAcc1 = [Select APN_Last_Activity_Date__c from Account where ID IN: OppIds];


for(Event e: Trigger.new) {
if(Trigger.isInsert) {
if(e.APN_Activity_Status__c == 'Completed') {
for(Account a: lstAcc) {
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}

for(Account a: lstAcc1){
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
}
}

else if(Trigger.isUpdate) {
if(e.APN_Activity_Status__c == 'Completed' && Trigger.oldMap.get(e.Id).APN_Activity_Status__c != 'Completed') {
for(Account a: lstAcc) {
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}

for(Account a: lstAcc1){
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
}
}
}
update lstAcc;
update lstAcc1;
}

 

 

The APN Date field is geting populated when I create or update a new events from accounts. The problem is the same date field doesnt populate when i create/update event from opportunity.

 

Please help me to fix this issue.

 

  • May 10, 2013
  • Like
  • 0

Hi,

 

I have a requirement to create a Case when the Oppportunity Stage is set to "Closed Won".

 

Below is the trigger that is doing this job. The problem is, its creating duplicates Case records when the Stage is set to "Closed Won".

 

Where Am i doing wrong?

 

Please advise.

 

trigger createNewCase on Opportunity (after update) {
List<Case> listcase = new List<Case>();
for(Opportunity o:trigger.new) {

Opportunity oldopp = trigger.OldMap.get(o.id);

if(o.StageName == 'Closed Won') {

//here APN_Related_Opportunity__c is lookup to Opportunity record for which new record is created

listCase.add(
new Case(APN_Related_Opportunity__c = o.id,


Subject = o.Name,
AccountId=o.AccountId,
Priority = 'Medium',
Status = 'New'));
}

}

if(listcase.size()>0) {
insert listcase;
}
}

  • April 03, 2013
  • Like
  • 0

We would like to have the ability for our Sales Reps to be able to create a new case from within an opportunity screen.  That case then would be related to that opportunity only.

 

Salesforce allows you to see related cases once you enable the relational list for cases in the layout edit mode, but does not give you a NEW CASE button anywhere in the Opportunity screen.

 

Here is what I have done so far. 

 

Under CASES > FIELDS I created a custom relationship lookup field called RELATED OPPORTUNITY | API Name: Related_Opportunity__c.  I added the new lookup field to the case layout.  It works fine from within the case screen.  I can lookup opportunities with no problem.  Under CASES > BUTTONS AND LINKS I created a new list button called NEW CASE.

 

Button Details:  

 

 

/500/e?retURL=%2F500%2Fo

&cas4_lkid={!Account.Id}

&cas4={!Account.Name}

&006M0000004yR7w_lkid={!Opportunity.Id}

&CF006M0000004yR7w={!Opportunity.Name}

&opp4={!Case.Account}

&CF00N00000007E8N3={!Case.CaseNumber}

&cas3_lkid={! NULLVALUE (Case.ContactId, Contact.Id )}

&cas3={! NULLVALUE (Case.Contact, Contact.Name )}

&cas6=Pre-Sale

&cas11=AE - Web

&retURL=/{!Opportunity.Id}

&saveURL=/{!Opportunity.Id}

 

When I hit the NEW CASE button within the opportunity screen, it takes me to new case creation screen like it should.  Some fields like Account Name gets updated with the correct info except for the RELATED OPPORTUNITY field.

 

Where am I doing wrong?  The ID of the RELATED OPPORTUNITY field is: 006M0000004yR7w

 

Please help.

 

  • April 02, 2013
  • Like
  • 1

Hi All,
My requirement is to restrict users to save EVENTS thats being created for ACCOUNTS based on the below criteria.

 

If the START DATE of the Event is greater than TODAY ,"YOR_Activity_Status__c" cannot be set to "Completed" and the DESCRIPTION field cannot be NULL.

 

Below is the Events trigger thats being written to achieve this:

 

I am getting this error message while saving:

 

Please help me to solve this and correct if I am wrong.

 

Error: Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger myTrigger caused an unexpected exception, contact your administrator: myTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.myTrigger: line 3, column 1

 

***************************************************************

trigger myeventvalidation on Event (after insert,after update) {
Set<Id> accIds = new Set<Id> ();
List<Account> lstAcc;
for(Event e: Trigger.new) {
if(e.StartDateTime > System.today() && e.YOR_Activity_Status__c == 'Completed' && e.Description ==NULL) {
e.addError('Not Possible');
}
}


lstAcc = [Select Id from Account where ID IN: accIds];

for(Event e: Trigger.new) {
if(Trigger.isInsert) {
for(Account a: lstAcc) {
if(e.StartDateTime > System.today() && e.YOR_Activity_Status__c == 'Completed' && e.Description ==NULL) {
e.addError('Not Possible');
}
}
}


else if(Trigger.isUpdate) {
if(e.StartDateTime > System.today() && e.YOR_Activity_Status__c == 'Completed' && e.Description ==NULL) {
for(Account a: lstAcc) {
if(a.Id == e.WhatId) {
e.addError('Not Possible');
}
}
}
}
}

update lstAcc;
}

***************************************************************

  • March 26, 2013
  • Like
  • 0

Hi Moderators,

 

I need to create a new field in Account object which is identical to Owner field (Field Label:Account Owner) of data type [Lookup(User)] which is a standard field in Account?

 

Could you please help me on how to achieve this?

  • March 21, 2013
  • Like
  • 0
Hello,
I am looking into various ways to archive email messages out of salesforce. As we all know it consumes a lot of data storage and buying additional storage is expensive. I need to know, how you all archive your email messages to get a fair idea of a future solution.

Appreciate all you inputs.
  • October 29, 2015
  • Like
  • 1

We would like to have the ability for our Sales Reps to be able to create a new case from within an opportunity screen.  That case then would be related to that opportunity only.

 

Salesforce allows you to see related cases once you enable the relational list for cases in the layout edit mode, but does not give you a NEW CASE button anywhere in the Opportunity screen.

 

Here is what I have done so far. 

 

Under CASES > FIELDS I created a custom relationship lookup field called RELATED OPPORTUNITY | API Name: Related_Opportunity__c.  I added the new lookup field to the case layout.  It works fine from within the case screen.  I can lookup opportunities with no problem.  Under CASES > BUTTONS AND LINKS I created a new list button called NEW CASE.

 

Button Details:  

 

 

/500/e?retURL=%2F500%2Fo

&cas4_lkid={!Account.Id}

&cas4={!Account.Name}

&006M0000004yR7w_lkid={!Opportunity.Id}

&CF006M0000004yR7w={!Opportunity.Name}

&opp4={!Case.Account}

&CF00N00000007E8N3={!Case.CaseNumber}

&cas3_lkid={! NULLVALUE (Case.ContactId, Contact.Id )}

&cas3={! NULLVALUE (Case.Contact, Contact.Name )}

&cas6=Pre-Sale

&cas11=AE - Web

&retURL=/{!Opportunity.Id}

&saveURL=/{!Opportunity.Id}

 

When I hit the NEW CASE button within the opportunity screen, it takes me to new case creation screen like it should.  Some fields like Account Name gets updated with the correct info except for the RELATED OPPORTUNITY field.

 

Where am I doing wrong?  The ID of the RELATED OPPORTUNITY field is: 006M0000004yR7w

 

Please help.

 

  • April 02, 2013
  • Like
  • 1
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.');
                }
             }
            
       }
   
   
   
   
    }
  • May 22, 2014
  • Like
  • 0
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;
}
  • April 28, 2014
  • Like
  • 0
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})")))
  • April 08, 2014
  • Like
  • 0
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.');
                } 
            }

    }


}
}
  • April 01, 2014
  • Like
  • 0
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');
        }
  
}
}
  • March 26, 2014
  • Like
  • 0
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;
}
  • January 29, 2014
  • Like
  • 0

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'


  • November 21, 2013
  • Like
  • 0