You need to sign in to do that
Don't have an account?
sfdc 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)
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)
thanks !. Yes we can achieve it by validation rule, how to achieve it by using code..
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..
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
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
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 ?
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