+ Start a Discussion
Ashique Khan 7Ashique Khan 7 

Relate Event to Opportunity based on Contact Role

Trying to relate events for contact roles to the opportunity. I'm getting an 'Error: Compile Error: unexpected token: WHERE at line 3 column 122'. Here's the code:
 
trigger ChangeRelatedTo on Event (before insert) {

List<OpportunityContactRole> contactsToCheck = [SELECT ContactId, OpportunityId, CreatedDate  FROM OpportunityContactRole WHERE IsDeleted = FALSE ORDER BY CreatedDate DESC];



 for( Event e : Trigger.new){

      if (e.WhoId != null){

             for ( OpportunityContactRole c: contactsToCheck ){

                 if (e.WhoId.equals(c.ContactId) ){

                 String oId = c.OpportunityId;

                     List<Opportunity> opportunity;

                     opportunity o = new opportunity();

                     o = [SELECT IsClosed, Id From Opportunity WHERE Id = :c.OpportunityId];

                     if (!o.IsClosed){

                             e.WhatId = c.OpportunityId;

                             break;

                             }

                 }

             }

      }

  }
}

 
Best Answer chosen by Ashique Khan 7
Dushyant SonwarDushyant Sonwar

Hi Ashique,

Try to open the code in notepad++ , as it is your code is having garbage characters like '[][]'

SELECT ContactId, OpportunityId, CreatedDate[][] FROM OpportunityContactRole WHERE IsDeleted = FALSE  ORDER BY CreatedDate DESC

Remove those characters and then again try to save, it should work fine.
Hope this helps. :)

All Answers

Ashique Khan 7Ashique Khan 7
Thanks Dushyant. I'm getting another error now :( *Error: Compile Error: No such column 'CreatedDate ' on entity 'OpportunityContactRole'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 3 column 59* I thought CreatedDate was a system field.
Dushyant SonwarDushyant Sonwar

Hi Ashique,

Try to open the code in notepad++ , as it is your code is having garbage characters like '[][]'

SELECT ContactId, OpportunityId, CreatedDate[][] FROM OpportunityContactRole WHERE IsDeleted = FALSE  ORDER BY CreatedDate DESC

Remove those characters and then again try to save, it should work fine.
Hope this helps. :)

This was selected as the best answer
Ashique Khan 7Ashique Khan 7
Thanks Dushyant! That worked :) 

I'm new to this trigger / test class word - how would I write a test class for this? Are there apex class generators I can use?
Dushyant SonwarDushyant Sonwar
Hi Ashique ,

I don't think that there is apex class generators that can create testclass automatically. You need to create the test class manually.
You can learn writing testclasses using trailhead .
https://trailhead.salesforce.com/modules/apex_testing/units/apex_testing_intro

Also you can check out below links , This will help you
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_test.htm
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

Thanks,
Dushyant