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 

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);
        }



lakslaks
Hi,

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.
Vinit_KumarVinit_Kumar
Try below code :-

@isTest
public class ContractGeneratorTest{
    static testMethod void validateContractAttachment(){
        opportunity opp= new Opportunity();
        opp.Name  = 'Test Opp';
	opp.CloseDate = system.today();
        Opp.StageName = 'Generate Contract';
        Opp.Contract_Attached__c = 'FALSE';
        
        insert opp;
        
        List<Attachment> att=[SELECT id from Attachment WHERE parentId =: opp.id]; 
        
        System.assertEquals(1, att.size()); 
    }
}


Kim WojnoKim Wojno
I had tried soemthing like that before but I can't place a Boolean under a String prior to "insert opp"
Vinit_KumarVinit_Kumar
ok got it,that was a typo,try this one :-
@isTest
public class ContractGeneratorTest{
    static testMethod void validateContractAttachment(){
        opportunity opp= new Opportunity();
        opp.Name  = 'Test Opp';
	opp.CloseDate = system.today();
        Opp.StageName = 'Generate Contract';
        Opp.Contract_Attached__c = false;
        
        insert opp;
        
        List<Attachment> att=[SELECT id from Attachment WHERE parentId =: opp.id]; 
        
        System.assertEquals(1, att.size()); 
    }
}


Kim WojnoKim Wojno
Ahh I see, why does it generate a result when it is just false as opposed to "FALSE"? I am assuming it is the difference it how it is recognized and as you can see I am very new to apex code. 
ShikibuShikibu
You wrote:

WHERE attach.Id = opp.id

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:

WHERE attach.Id = :opp.id

The colon before "opp.id" indicates that "opp" is an apex variable, not an soql keyword, object name, or field name.