function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RahulRahul 

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.
Amit Chaudhary 8Amit Chaudhary 8
What error you are getting at time of update and what you want to do on update event ? which all field you want to update ?
RahulRahul
Hi Amit, currently when Iam creating a new Account, a contact is created  with Name and Email from the Account. but later when Iam updating the email Id in contact or in account or updating any field.Getting the Following Error :

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: ()





 
Amit Chaudhary 8Amit Chaudhary 8
Look like you are getting error in some other trigger "updateFundraiser " not this one.
RahulRahul
HI Amit but i dont want to de activate UpdateFundraiser, because it is implemented based on some functionality. Any suggestions?
Amit Chaudhary 8Amit Chaudhary 8
Hi Sumit,
Your Trigger is already active and same is getting failed. Please post the code of UpdateFundraiser trigger. You need to fix the same
RahulRahul
Hi Amit, Please find the code for UpdateFundraiser.There is a Lookup Relationship of Account with contact. The relationship is named as Contact Associated. When ever an account is updated with the different contact details through contact associated look up , the updated contact mobile phone , Email and Name is updated in the Account as well.This is a mandatory functionality that we require too. Thanks

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);   
        //}
    }
   
    

}