You need to sign in to do that
Don't have an account?
Kim Wojno
Help with a Test Class
I am creating test class for my trigger but keep getting an error on the last line where it doesn't recgonize "WHERE." Any thoughts?
@isTest public class ContractGeneratorTest{ static testMethod void validateContractAttachment(){ opportunity opp= new Opportunity(); Opp.StageName = 'Generate Contract'; Opp.Contract_Attached__c = 'FALSE'; insert opp; Attachment attach=new Attachment(); attach.Name='Contract Test Attachment'; Blob bodyBlob=Blob.valueOf('Contract Attachment Body'); attach.body = bodyBlob; attach.Id = opp.id; insert attach; List<Attachment> attach=[SELECT opportunity WHERE attach.Id = opp.id]; System.assertEquals(1, attachments.size()); } }Here is the trigger:
trigger GenerateContract on Opportunity (after update, after insert) { list<id> opportunityId = new list<id>(); for(opportunity opp: trigger.new){ if(opp.StageName=='Generate Contract' && opp.Contract_Attached__c == False) { opportunityId.add(opp.id); } } AccountTriggerController.addPDFAttach(userInfo.getSessionId(), opportunityId); }
I see that you have not specified which fields to retrieve from the Opportunity object in -
[SELECT <field1, field2 FROM> opportunity WHERE attach.Id = opp.id]
also after the WHERE clause you need to specify as attach.Id = :opp.id
Regards,
Lakshmi.
But "opp.id" refers to a variable that is in your apex execution context. This is a concept called "interpolation". In order to get the contents of your apex variable into the DML query, you need to use a colon. Thus (as Vinit_Kumar suggested), you need to write:
The colon before "opp.id" indicates that "opp" is an apex variable, not an soql keyword, object name, or field name.