You need to sign in to do that
Don't have an account?
Rahul
Hi friends, Iam trying to create a contact when a new account is inserted or Update a contact when Account is updated.This code is working fine while creating an account, it creates contact but it throws error while updating the record.
Please find the below code.
trigger tocreatecontact on Account (after insert,after update) {
List<contact> oppList = new List<contact>();
// Add an contact for each account if it doesn't already have one.
// Iterate over accounts that are in this trigger but that don't have Contacts.
for (Account a : [SELECT Id,Name,Created_By_Email__c FROM Account
WHERE Id IN :Trigger.New AND
Id NOT IN (SELECT AccountId FROM contact)]) {
// Add a default opportunity for this account
oppList.add(new contact(lastName=a.Name + ' contact',
AccountId=a.Id,Email=a.Created_By_Email__c));
}
if (oppList.size() > 0) {
insert oppList;
}
}
Thanks in Advance.
trigger tocreatecontact on Account (after insert,after update) {
List<contact> oppList = new List<contact>();
// Add an contact for each account if it doesn't already have one.
// Iterate over accounts that are in this trigger but that don't have Contacts.
for (Account a : [SELECT Id,Name,Created_By_Email__c FROM Account
WHERE Id IN :Trigger.New AND
Id NOT IN (SELECT AccountId FROM contact)]) {
// Add a default opportunity for this account
oppList.add(new contact(lastName=a.Name + ' contact',
AccountId=a.Id,Email=a.Created_By_Email__c));
}
if (oppList.size() > 0) {
insert oppList;
}
}
Thanks in Advance.
Error:Apex trigger updateFundraiser caused an unexpected exception, contact your administrator: updateFundraiser: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: ()
Your Trigger is already active and same is getting failed. Please post the code of UpdateFundraiser trigger. You need to fix the same
trigger updateFundraiser on Account (before update) {
set<id> conIds = new set<id>();
for(account a1: trigger.new) {
if(a1.Contact_Associated__c != null) {
conIds.add(a1.Contact_Associated__c);
}
}
system.debug('conIds::'+conIds);
map<id,contact> conMap = new map<id,contact>([select id,Name, Beneficiary_Name__c,Email, MobilePhone from contact where id IN :conIds]);
system.debug('conMap::'+conMap);
for(account a1:trigger.new){
system.debug('a1.Contact_Associated__c::'+a1.Contact_Associated__c);
system.debug('!!!!!!'+trigger.oldMap.get(a1.id).Contact_Associated__c);
//if(a1.Contact_Associated__c != trigger.oldMap.get(a1.id).Contact_Associated__c) {
system.debug('herere!!!');
a1.Mobile_Number__c = conMap.get(a1.Contact_Associated__c).MobilePhone;
a1.Created_By_Email__c = conMap.get(a1.Contact_Associated__c).Email;
a1.Name = conMap.get(a1.Contact_Associated__c).Name;
system.debug('herere!!!');
system.debug('a1'+a1);
//}
}
}