You need to sign in to do that
Don't have an account?
Sandile mathe
hi , I am scratching my head here. I want to link link all contacts from the related account that are flagged as key contacts to an opportunity on that account when it is created.
Here are the steps I followed using a trigger but its not happening. Please help. Maybe I am missing something
1. I created a new field of Checkbox type on Contact Object to Flag contact as Key Contact
Field Name : Key_Contact__c
type : Checkbox
Object : Contact
2. The Trigger Class to Link Contacts That Flagged as key Contact when creating an Opportunity.
------------------------------------START------------------------------------------------
trigger LinkContacts on Opportunity (before insert, before update) {
set<Id> AccountIds = new set<Id>();
for (Opportunity o: trigger.new) {
AccountIds.add(o.AccountId);
}
map<Id, Account> AccountContactMap = new map<Id, Account>();
map<Id, Contact> ContactMap = new map<Id, Contact>();
list<Account> AccConts = new list<Account>();
AccConts = [Select Id, Name, (Select Id, Name, Key_Contact__c from Contacts Where Key_Contact__c = true) from Account where Id IN : AccountIds];
for(Account acc : AccConts){
AccountContactMap.put(acc.Id, acc);
}
for (Opportunity o: trigger.new) {
Account Acc = AccountContactMap.get(o.AccountId);
if(Acc != null){
List<Contact> conts = new list<Contact>(Acc.Contacts);
map<Id, Contact> ContactMap = new map<Id, Contact>();
Contact Con = ContactMap.get(o.ContactId);
if(conts.size() > 0 ){
if(Con.Key_Contact__c = true){
for(Contact contactFlag : conts){
ContactMap.put(contactFlag.Id,con );
}
}
}
}
}
}
--------------------------------------END-------------------------------------------------
1. I created a new field of Checkbox type on Contact Object to Flag contact as Key Contact
Field Name : Key_Contact__c
type : Checkbox
Object : Contact
2. The Trigger Class to Link Contacts That Flagged as key Contact when creating an Opportunity.
------------------------------------START------------------------------------------------
trigger LinkContacts on Opportunity (before insert, before update) {
set<Id> AccountIds = new set<Id>();
for (Opportunity o: trigger.new) {
AccountIds.add(o.AccountId);
}
map<Id, Account> AccountContactMap = new map<Id, Account>();
map<Id, Contact> ContactMap = new map<Id, Contact>();
list<Account> AccConts = new list<Account>();
AccConts = [Select Id, Name, (Select Id, Name, Key_Contact__c from Contacts Where Key_Contact__c = true) from Account where Id IN : AccountIds];
for(Account acc : AccConts){
AccountContactMap.put(acc.Id, acc);
}
for (Opportunity o: trigger.new) {
Account Acc = AccountContactMap.get(o.AccountId);
if(Acc != null){
List<Contact> conts = new list<Contact>(Acc.Contacts);
map<Id, Contact> ContactMap = new map<Id, Contact>();
Contact Con = ContactMap.get(o.ContactId);
if(conts.size() > 0 ){
if(Con.Key_Contact__c = true){
for(Contact contactFlag : conts){
ContactMap.put(contactFlag.Id,con );
}
}
}
}
}
}
--------------------------------------END-------------------------------------------------
I'm unable to understand your requirement. Would you mind to explain it again with an example.
Thanks,
Ashish Singh.
Though I didn't understand your requirement, the condition check in your code for key_contact__c should be if(Con.Key_Contact__c == true){
== in place of =
Thanks