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
kishore goud 3kishore goud 3 

Check parent field is null before inserting child record

Hi every one,
As per my requirement,I need to check whether phone field of account object is null before inserting contact.If it is null,I should not allow user to create contact.I created trigger for this scenario,but unable to achieve functionality .I am posting my trigger code,any one please help me out.

trigger CheckAcctPhoneBlank on Contact (Before insert) {
     
    for(Contact con:[Select id,account.phone,account.name from Contact where id in:trigger.new]){ 
        system.debug('Contact size**'+con);   
        system.debug('??test2??');
        system.debug('Phone details**'+con.account.phone);
        if(con.account.phone == null){
         con.addError('Contact cannot be inserted');
        
        }
        
    
    }

}
 
Best Answer chosen by kishore goud 3
sfdcMonkey.comsfdcMonkey.com
Hi Kishore,
Try following trigger, we need to check first that contact account field is not null, if it not null then we will check that account phone is not equal to null otherwise you will get System.NullPointerException in case of null account
trigger CheckAcctPhoneBlank on Contact (Before insert) {
    
    set<id> ids = new set<id>();
     for(Contact c:trigger.new){
        ids.add(c.accountid);        
     }
    
    Map<id,Account> mapofacc = new map<id,account>([select id,phone from account where id=:ids]);
    
    for(contact con:trigger.new){
        if(mapofacc.get(con.accountid) == null){
            con.addError('Contact cannot be inserted, Account is Null');
        }
        else if(mapofacc.get(con.accountid).Phone == null){
            con.addError('Contact cannot be inserted, Account Phone is Null');
        }
    }
}
Let us know if it helps you
Thanks
 

All Answers

Prashant Pandey07Prashant Pandey07
Hi Kishore,

You are not querying the account phone that is why its not checking the account phone.

Please check the below code and let me know if that solves your problem.
 
trigger CheckAcctPhoneBlank on Contact (Before insert) {
     
     set<id> ids=new set<id>();
     for(Contact c:trigger.new){
           ids.add(c.accountid);
     
     }
     
     Map<id,Account> mapofacc =new map<id,account>([select id,phone from account where id=:ids]);
     
for(contact con:trigger.new){
                if(mapofacc.get(con.accountid).phone == null){
            con.addError('Contact cannot be inserted');
            }
        }

}

--
Thanks,
Prashant
sfdcMonkey.comsfdcMonkey.com
Hi Kishore,
Try following trigger, we need to check first that contact account field is not null, if it not null then we will check that account phone is not equal to null otherwise you will get System.NullPointerException in case of null account
trigger CheckAcctPhoneBlank on Contact (Before insert) {
    
    set<id> ids = new set<id>();
     for(Contact c:trigger.new){
        ids.add(c.accountid);        
     }
    
    Map<id,Account> mapofacc = new map<id,account>([select id,phone from account where id=:ids]);
    
    for(contact con:trigger.new){
        if(mapofacc.get(con.accountid) == null){
            con.addError('Contact cannot be inserted, Account is Null');
        }
        else if(mapofacc.get(con.accountid).Phone == null){
            con.addError('Contact cannot be inserted, Account Phone is Null');
        }
    }
}
Let us know if it helps you
Thanks
 
This was selected as the best answer
kishore goud 3kishore goud 3
Thanks to both of you for giving the reply,both of the solutions solved my problem.