0

error: Compile Error: expecting a colon, found com.opportunity__c at line 6 column 82

trigger countCompetitor on Competitor__c (after insert, after update) {
   //select id from opportunity where name = 'opp4'
   list<opportunity> opp= new list<opportunity>();
   for (competitor__c com : Trigger.new)
   {
       integer i = [select count(id) from competitor__c where opportunities.name = com.opportunity__c];
   }
}
  • The fix for that syntax error is to add a colon ... = :com.opportunity__c. – Keith C Jan 16 '16 at 9:43
  • Apart from the error,query inside a for loop should be avoided.Use Set to collect the opportunity ids using for loop and finally use it in your query. – Praveen Jan 16 '16 at 9:47
4

Your code should be:

trigger countCompetitor on Competitor__c (after insert, after update) {
  //select id from opportunity where name = 'opp4'
  list<opportunity> opp= new list<opportunity>();
  for (competitor__c com : Trigger.new)
  {
    integer i = [select count(id) from competitor__c where opportunities.name = :com.opportunity__c];
  }
}

Do not forget the colon to bind variables, so your query needs to have this part:

:com.opportunity__c

But... making a query inside the loop will work for few records but will throw Governor Limits (too many SOQL queries) when you load data. So the code should look like this:

trigger countCompetitor on Competitor__c (after insert, after update) {
    Set<Id> ids = new Set<ID>();
    for (competitor__c com : Trigger.new) {
        ids.add(com.opportunity__c);
    }
    System.debug([SELECT opportunity__c, count(id) FROM competitor__c GROUP BY opportunity__c WHERE opportunity__c in :ids]);

}

Finally, note that I have changed the search from Name to IDs, because it's a bit more efficient to search on IDs than on Strings ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.