Trigger scenario 3 in Salesforce:
Using Trigger to populate the account type on the contact record
trigger triggername on contact(before update,before insert){
if(trigger.isbefore && (trigger.isupdate|| trigger.isinsert){
Set<Id> accountIds = new Set<Id>();
for(contact ct: Trigger.new){
accountIds.add(ct.id);
}
if(!accids.isempty()){
map<id,account> mapacc = new map<id,account>([select id,name ,type from account where id IN: accountids]);
for(contact con : trigger.new){
if(con.accountid != null){
con.acctype__c = mapacc.get(con.accountid).type;
}
}
}
if(!accids.isempty()){
map<id,account> mapacc = new map<id,account>([select id,name ,type from account where id IN: accountids]);
for(contact con : trigger.new){
if(con.accountid != null){
con.acctype__c = mapacc.get(con.accountid).type;
}
}
}
}
Trigger to update parent object value with child value and recursive trigger and updating same parent object not child object:
Note:below trigger it doesn't work in this trigger.
trigger AcctoOppTrigger on Account (after update) {
list<account> lstacc1 = new list<account>();
if(trigger.isafter && trigger.isupdate){
Set<Id> accountIds = new Set<Id>();
for(Account ct: Trigger.new){
accountIds.add(ct.id);
}
list<account> lstacc = [select id,name,email__c,(select id,email, lastname, Level__c,accountid from contacts where Level__c ='primary') from account where id in : accountIds];
for(Account ct: lstacc){
for(contact con : ct.contacts){
ct.Email__c = con.Email;
lstacc1.add(ct);
}
}
// if(recusrssionPreventController.flag == true){
// recusrssionPreventController.flag = false;
if(lstacc1.size()>0)
update lstacc1;
// }
}
}
Note:
With out this below lines it should get below error:
if(recusrssionPreventController.flag == true){
recusrssionPreventController.flag = false;
if(lstacc1.size()>0)
update lstacc1;
}
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AcctoOppTrigger caused an unexpected exception, contact your administrator: AcctoOppTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0012800000exmUvAAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AcctoOppTrigger: maximum trigger depth exceeded Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv]: []: Trigger.AcctoOppTrigger: line 18, column 1
Review all error messages below to correct your data.
Apex trigger AcctoOppTrigger caused an unexpected exception, contact your administrator: AcctoOppTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0012800000exmUvAAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AcctoOppTrigger: maximum trigger depth exceeded Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv] Account trigger event AfterUpdate for [0012800000exmUv]: []: Trigger.AcctoOppTrigger: line 18, column 1
Note:
If filed value 'level__c'is 'primary' have in 2 contact records associated parent it will one more error like below:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AcctoOppTrigger caused an unexpected exception, contact your administrator: AcctoOppTrigger: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 0012800000exmUvAAI: Trigger.AcctoOppTrigger: line 18, column 1
------------------------------------------------------------------------------------------------
Review all error messages below to correct your data.
Apex trigger AcctoOppTrigger caused an unexpected exception, contact your administrator: AcctoOppTrigger: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 0012800000exmUvAAI: Trigger.AcctoOppTrigger: line 18, column 1
------------------------------------------------------------------------------------------------
trigger BestPracticesforaccountandcon on account (after insert,after update)
{
//This queries all Contacts related to the incoming Account records in a single SOQL query.
//This is also an example of how to use child relationships in SOQL
List<Account> accountsWithContacts = [select id, name,email__c, (select id, salutation, description,
firstname, lastname, email from Contacts)
from Account where Id IN :Trigger.newMap.keySet()];
List<Contact> contactsToUpdate = new List<Contact>{};
// For loop to iterate through all the queried Account records
for(Account a: accountsWithContacts){
// Use the child relationships dot syntax to access the related Contacts
for(Contact c: a.Contacts){
System.debug('Contact Id[' + c.Id + '], FirstName[' + c.firstname + '], LastName[' + c.lastname +']');
c.Description=c.salutation + ' ' + c.firstName + ' ' + c.lastname;
c.Email = a.email__c;
contactsToUpdate.add(c);
}
}
//Now outside the FOR Loop, perform a single Update DML statement.
update contactsToUpdate;
/* if(Trigger.isafter)
{
list<contact> allcontacts = new list<contact>();
if(trigger.isinsert)
{
for(integer i = 0;i<Trigger.new.size();i++)
{
contact con = new contact();
con .LastName= Trigger.new[i].name;
// con .LastName =con .LastName + 'devi';
con .Email= Trigger.new[i].Email__c;
con .Department= Trigger.new[i].AccountNumber;
con .Accountid = Trigger.new[i].id;
allcontacts.add(con);
}
insert allcontacts;
}
list<id> allAccountIDs = new list<id>();
map<id,account> allAccounts = new Map<id,account>();
if(Trigger.isupdate)
{
for(integer i = 0;i<Trigger.new.size();i++)
{
allAccountIDs.add(Trigger.new[i].id);
allAccounts.put(Trigger.new[i].id,Trigger.new[i]);
}
list<contact> listcon = [select id,LastName,Email,Department,AccountID from contact where Accountid =:allAccountIDs];
for(integer i =0;i<listcon.size();i++)
{
if(allAccounts.containskey(listcon[i].AccountID ))
{
// listcon[i].LastName =listcon[i].LastName+'devi';
listcon[i].LastName = allAccounts.get(listcon[i].AccountID ).name;
listcon[i].Email = allAccounts.get(listcon[i].AccountID ).Email__c;
listcon[i].phone = allAccounts.get(listcon[i].AccountID ).phone;
listcon[i].Department = allAccounts.get(listcon[i].AccountID ).AccountNumber;
listcon[i].Accountid = allAccounts.get(listcon[i].AccountID).id;
allcontacts.add(listcon[i]);
}
}
update allcontacts;
}
} */
}
No comments:
Post a Comment