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
sudha76sudha76 

Trigger on the Contact to set the checkbox to TRUE.

Trying to create a TRIGGER on CONTACT to set the checkbox called "Has_Opportunity__c" to TRUE if that Contact has any Opportunity.

 

or if that Contact is a part of the OpportunityContactRole table?

 

How can I query it? I am trying to use workbench, first to build such a query? any ideas?

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645
trigger CheckContacts on Contact (before update) {

Map<ID,Contact> cIdToContactWRelRecsMap = new Map<ID,Contact> ([select id, (select id from OpportunityContactRoles) from Contact where id IN :trigger.new]);

for (Contact c: Trigger.new)
   if (cIdToContactWRelRecsMap.get(c.id).opportunityContactRoles.size() > 0)
      c.has_opportunity__c = true; 
}

 should do the trick - the Map is initialized to contactId | Contact + all OCRs, if any; then, in the Trigger.new list, check the map to see if the size of the OCR related list is > 0.

All Answers

Ritesh AswaneyRitesh Aswaney

Hey Sudha,

A trigger on Contact would be a 'PULL'

 

trigger ContactBefore on Contact (before update){

 

Map<Id, List<Contact>> accConMap = new Map<Id, List<Contact>>{};

 

//map a map of AccountIds, List of Contacts

for(Contact con : trigger.new){

List<Contact> currCon = accConMap.get(con.AccountId);

if(currCon == null){

currCon = new List<Contact>{};

accConMap.put(con.AccountId, currCon);

}

currCon.add(con);

}

 

 

for (Account acc: [Select Id, Name, (Select Id, Name from Opportunities) from Account where Id IN :accConMap.keySet()]) 

if(acc.Opportunities != null && acc.Opportunities.size() > 0)

for(Contact con : accConMap.get(acc.Id))

con.Has_Opportunity__c = true;

 

}

 

A 'PUSH' approach would be a a trigger on Opportunity.

sudha76sudha76

I tried this code :-

 

trigger CheckContacts on Contact (before update)
{
Map<Id, List<Contact>> accConMap = new Map<Id, List<Contact>>{};

//map a map of AccountIds, List of Contacts
for(Contact con : trigger.new)
{
List<Contact> currCon = accConMap.get(con.AccountId);
    if(currCon == null)
    {
        currCon = new List<Contact>{};
        accConMap.put(con.AccountId, currCon);
    }
        currCon.add(con);
}
for (Account acc: [Select Id, Name, (Select Id, Name from Opportunities) from Account where Id IN :accIds])
{
    if(acc.Opportunities != null && acc.Opportunities.size() > 0)
    {
          for(Contact con : accConMap.get(acc.Id)
          con.HasOpportunity__c=True;
    }
}
}

 

Error message is :-

Error: Compile Error: expecting a right parentheses, found 'con.HasOpportunity__c' at line 21 column 10

 

 

Line 21 is this -   con.HasOpportunity__c=True;


what am i missing?

 

 

 

Ritesh AswaneyRitesh Aswaney

The line before was missing right paranthesis 

 

So,

 

 for(Contact con : accConMap.get(acc.Id)) //missing right paranthesis - now corrected
          con.HasOpportunity__c=True;

sudha76sudha76

now i am getting a diffrent error that accIds variable does not exist, so I had to comment that whole section:-

 

trigger CheckContacts on Contact (before update)
{
Map<Id, List<Contact>> accConMap = new Map<Id, List<Contact>>{};

//map a map of AccountIds, List of Contacts
for(Contact con : trigger.new)
{
List<Contact> currCon = accConMap.get(con.AccountId);
    if(currCon == null)
    {
        currCon = new List<Contact>{};
        accConMap.put(con.AccountId, currCon);
    }
        currCon.add(con);
}
/* for (Account acc: [Select Id, Name, (Select Id, Name from Opportunities) from Account where Id IN :accIds])
{
    if(acc.Opportunities != null && acc.Opportunities.size() > 0)
    {
          for(Contact con : accConMap.get(acc.Id))
          con.HasOpportunity__c=True;
    }
}*/
}

 

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

 

 

also, why am I referring the Account in my code, I just simply wanted to check if the contact has opportunity or not. this is getting complicated now??

 

 

Ritesh AswaneyRitesh Aswaney

My bad, 

for (Account acc: [Select Id, Name, (Select Id, Name from Opportunities) from Account where Id IN :accConMap.keySet()])

 

Opportunities are related to Accounts, unless you just want Contacts which are OCR's on an Opportunity.

This trigger is looking for Opportunities related to the Accounts which the Contact belongs to, whether or not it is an OCR itself.

sudha76sudha76

The only contacts which should be set to TRUE if they are a part of the OCR table. This means, I need only those Contacts to be set to TRUE. Right now, when I tested this trigger it is even making that checkbox TRUE even if it does not have any opportunity but the Account has the Opportunity.

 

I only want the Contact to set to TRUE if it has the Opportunity.  I tried to changed the code like this and get an error:-

 

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

trigger CheckContacts on Contact (before update)
{
Map<Id, List<Contact>> accConMap = new Map<Id, List<Contact>>{};

//map a map of AccountIds, List of Contacts
for(Contact con : trigger.new)
{
List<Contact> currCon = accConMap.get(con.AccountId);
    if(currCon == null)
    {
        currCon = new List<Contact>{};
        accConMap.put(con.AccountId, currCon);
    }
        currCon.add(con);
}
for (Account acc: [Select Id, Name from Contact where Id in (Select ContactId from OpportunityContactRole)])
{
    if(acc.Opportunities != null && acc.Opportunities.size() > 0)
    {
          for(Contact con : accConMap.get(acc.Id))
          con.HasOpportunity__c=True;
    }
}
}

 

/////////////////////////////////////

 

I think I have to remove the Account settings from the top and possibly include this in the currCon declaration.

 

please guide...thanks.

sudha76sudha76

trigger CheckContacts on Contact (before update)
{
 List<Contact> ConMap = new List<Contact>{};

//Fetch the List of Contacts
for(Contact con : trigger.new)
{
List<Contact> currCon = ConMap.get(con.Id);
    if(currCon == null)
    {
        currCon = new List<Contact>{};
        accConMap.put(con.AccountId, currCon);
    }
        currCon.add(con);
}
for (Contact currCon: [Select Id, Name, (Select ContactId from OpportunityContactRole) from Contact where Id IN :ConMap.keySet()])
{
    if(acc.Opportunities != null && acc.Opportunities.size() > 0)
    {
          for(Contact con : ConMap.get(con.Id))
          con.HasOpportunity__c=True;
    }
}
}

 

Error Message is:-

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Error: Compile Error: Didn't understand relationship 'OpportunityContactRole' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 16 column 23

 

////////////////////////////////////////////////

 

 

Please help. I simply need to point out to the Contacts table if that has Opportunity then make the checkbox TRUE.

crop1645crop1645
trigger CheckContacts on Contact (before update) {

Map<ID,Contact> cIdToContactWRelRecsMap = new Map<ID,Contact> ([select id, (select id from OpportunityContactRoles) from Contact where id IN :trigger.new]);

for (Contact c: Trigger.new)
   if (cIdToContactWRelRecsMap.get(c.id).opportunityContactRoles.size() > 0)
      c.has_opportunity__c = true; 
}

 should do the trick - the Map is initialized to contactId | Contact + all OCRs, if any; then, in the Trigger.new list, check the map to see if the size of the OCR related list is > 0.

This was selected as the best answer
sudha76sudha76

this code worked. Thank you Eric. :-)

 

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

trigger CheckContacts on Contact (before update)
{

// Map is intialized here to ContactID or Contact + all OCRs

Map<Id,Contact> cIdToContactWRelRecsMap  = new Map <Id,Contact>

([select id, (select id from OpportunityContactRoles) from Contact where id IN :trigger.new]);

for (Contact c: Trigger.new) {

    if (cIdToContactWRelRecsMap.get(c.id).opportunityContactRoles.size() > 0) c.HasOpportunity__c = true;
    
     }
     
     }

 

///////////////////////////////////////////////

 

 

I also tested it on Sandbox and it worked.

 

Now I want to build the test case for this one so I can move this to production. I will work on it now and hopefully i can get it.

 

 

thanks.