+ Start a Discussion
sfdc freshersfdc fresher 

triggers logic needed

hi team, 
Could you please provide the logic for below :
Need to throw an error while creating a contact when the account has its phone filed blank.
(provide the trigger logic)
Raj VakatiRaj Vakati
No need of the trigger for this one .. simple validation is enough  .. please refer this image for validation rule 

User-added image
sfdc freshersfdc fresher
Hi Raj,
thanks !. Yes we can achieve it by validation rule, how to achieve it by using code..
Raj VakatiRaj Vakati
Try this code
 
Trigger ShowContacts on Contact (before insert , before update) {
    
    Set<Id> accountIds = new Set<Id>();
    List<Account> accountsToUpdate = new List<Account>();

    for (Contact c : Trigger.new) {
        accountIds.add(c.accountId);
    }

    Map<id, Account> accountMap = new Map<id, Account>([select id, Phone from Account where id IN :accountIds]);

    for (Contact c : Trigger.new) {
        if (c.accountId != null && accountMap.get(c.accountId).Phone == null) {
			c.addError('There is no Phone Number on the Account ');
		}
    }
}

 
sfdc freshersfdc fresher
hi Raj,
Map<id, Account> accountMap = new Map<id, Account>([select id, Phone from Account where id IN:accountIds]);

will it works?....we are assigning list of records to Set right?

i just added like below.
Map<id, Account> accountMap = new Map<id, Account>();
List<Account> act=[select id, Phone from Account where id IN:accountIds];
for(Account a:act)
{
accountMap.put(a.id,a.Phone);
}

could you please correct me if am wrong..
Raj VakatiRaj Vakati
This will work 

Map<id, Account> accountMap = new Map<id, Account>([select id, Phone from Account where id IN:accountIds]);


Because you have one account with same id and while inserting the contact you can able to select only one contact .. 

The code works fine 
sfdc freshersfdc fresher
hi Raji,
i am getting an erro at below line
Map<id, Account> accountMap = new Map<id, Account>([select id, Phone from Account where id IN:accountIds]);

Invalid initializer type List<Account> found for Map<Id,String>: expected a Map with the same key and value types, or a valid SObject List
Raj VakatiRaj Vakati
This line is correct .. give me complete code
 
Map<id, Account> accountMap = new Map<id, Account>([select id, Phone from Account where id IN:accountIds]);

 
Raj VakatiRaj Vakati
Trigger ShowContacts on Contact (before insert , before update) {
    
    Set<Id> accountIds = new Set<Id>();
    List<Account> accountsToUpdate = new List<Account>();
    
    for (Contact c : Trigger.new) {
        accountIds.add(c.accountId);
    }
    
    //Map<id, Account> accountMap = new Map<id, Account>([select id, Phone from Account where id IN :accountIds]);
    Map<id, Account> accountMap = new Map<id, Account>([select id, Phone from Account where id IN:accountIds]);

    for (Contact c : Trigger.new) {
        if (c.accountId != null && accountMap.get(c.accountId).Phone == null) {
            c.addError('There is no Phone Number on the Account ');
        }
    }
}

 
sfdc freshersfdc fresher
hi Raj,
Sorry I writting below thats i got an error 
Map<id, String> accountMap = new Map<id, String >([select id, Phone from Account where id IN:accountIds]);

yeah your code is working fine .
Could you please explain me this line:
Map<id, Account> accountMap = new Map<id, Account>([select id, Phone from Account where id IN:accountIds]);

when we are assign that list of records to Map.........what are the values of Id and Account from that query line
it is like
Id is Account Id
Account :: is phone ?
 
Ajay K DubediAjay K Dubedi
Hi,

Below code can fulfill your requirements. Hope this will work for you.

trigger ContactTriggerWithMap on Contact (before insert) {
    // Here taking the Set because we don't need to maintain duplicate
    Set<Id> accIdSet = new Set<Id>();
    
    for(Contact con: Trigger.new) {
        if(con.AccountId != null) {
            accIdSet.add(con.AccountId);
        }
    }
    
    if(!accIdSet.isEmpty()) {
        Map<Id, Account> accMap = new Map<Id, Account>([Select Id, Name,Phone, Type from Account where Id IN: accIdSet]);
        
        for(Contact con: Trigger.new) {
            if(con.AccountId != null && accMap.get(con.AccountId).Phone == null) {
               con.addError('There is no Phone Number on the Account');
            }
        }
    }
}

Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi