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
Kim WojnoKim Wojno 

Issue with Conditional Apex Trigger

I am looking to put in a trigger that will automatically attach a pdf contract from a VF page to the opportunity. Below is what I have (via http://jungleeforce.wordpress.com/2013/05/08/generate-a-pdf-and-attach-it-to-record-from-a-trigger-in-salesforce/) and modified. However I want to put in an if statement but I keep getting the error "Concrete SObject."  The if stament I am trying to put in:
if(opportunity.stageName == 'Sent Contract for Review' && opportunity.Contract_Attached__c == False)

Any help here? I am referencing the opportunity ID in the list but need a way to make it reference the opportunity itself with only one action being done. 

trigger GenerateContract on Opportunity (after update, after insert) {

     list<id> opportunityId = new list<id>();
     for(id opportunity: trigger.newMap.keySet()){
         opportunityId.add(opportunity);
      }
        
        AccountTriggerController.addPDFAttach(userInfo.getSessionId(), opportunityId);
Best Answer chosen by Kim Wojno
Vinit_KumarVinit_Kumar
Ohk my bad,I missed an'=' sign.Try below code :-
trigger GenerateContract on Opportunity (after update, after insert) {

     list<id> opportunityId = new list<id>();
     for(opportunity opp: trigger.new){
     if(opp.StageName=='Sent Contract for Review' && opp.Contract_Attached__c == False)
        {
         opportunityId.add(opp.id);
        } 
      }
        
        AccountTriggerController.addPDFAttach(userInfo.getSessionId(), opportunityId);
        }


All Answers

ShashForceShashForce
Hi,

You cannot use "opportuniity" as a variable for your FOR loop, as it is a reserved word for the opportunity sObject. Please select a different variable. Something like:

for(id oppId: trigger.newMap.keySet()){
    if(oppId.stageName == 'Sent Contract for Review' && oppId.Contract_Attached__c == False){
        opportunityId.add(opportunity);
    }
}

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
Kim WojnoKim Wojno
I took your advice and changed opportunity to opportunity to oppId
 but I am not getting an error that is dones't recongize (opportunity) on line 6. 
 
trigger GenerateContract on Opportunity (after update, after insert) {
     
     list<id> opportunityId = new list<id>();
     for(id oppId: trigger.newMap.keySet()){
     if(oppId.stageName == 'Sent Contract for Review' && oppId.Contract_Attached__c == False){
        opportunityId.add(opportunity);
      }
     }   
                AccountTriggerController.addPDFAttach(userInfo.getSessionId(), oppId);
      
}


Vinit_KumarVinit_Kumar
I saw the link from where you got the code,it is written on Account not on Opportunity.You need to change your Apex class to work with Opportunity object.

Once you are done with modification for class.Try below code :-

trigger GenerateContract on Opportunity (after update, after insert) {

     list<id> opportunityId = new list<id>();
     for(opportunity opp: trigger.new)){
	 if(opp.StageName='Sent Contract for Review' && opp.Contract_Attached__c == False)
	    {
         opportunityId.add(opp.id);
		} 
      }
        
        AccountTriggerController.addPDFAttach(userInfo.getSessionId(), opportunityId);

If this helps,please mark it as best answer to help others :)
Kim WojnoKim Wojno
I combed through all classes and everything is associate to Opportunity ID as is should be

I put in place the below code but am now getting "Error: Compile Error: AND operator can only be applied to Boolean expressions at line 5 column 53"
trigger GenerateContract on Opportunity (after update, after insert) {

     list<id> opportunityId = new list<id>();
     for(opportunity opp: trigger.new){
     if(opp.StageName='Sent Contract for Review' && opp.Contract_Attached__c == False)
        {
         opportunityId.add(opp.id);
        } 
      }
        
        AccountTriggerController.addPDFAttach(userInfo.getSessionId(), opportunityId);
        }

Vinit_KumarVinit_Kumar
Ohk my bad,I missed an'=' sign.Try below code :-
trigger GenerateContract on Opportunity (after update, after insert) {

     list<id> opportunityId = new list<id>();
     for(opportunity opp: trigger.new){
     if(opp.StageName=='Sent Contract for Review' && opp.Contract_Attached__c == False)
        {
         opportunityId.add(opp.id);
        } 
      }
        
        AccountTriggerController.addPDFAttach(userInfo.getSessionId(), opportunityId);
        }


This was selected as the best answer
Kim WojnoKim Wojno
You are awesome!
Vinit_KumarVinit_Kumar
Happy to help :)