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
Maggie Farley 14Maggie Farley 14 

Trigger Errors and Test Class Errors

Hello,

I am trying to create a trigger to populate a Contract Uploaded Date on the Opportunity. I am new to triggers and keep getting erros on both the apex trigger and apex class. Can someone please help me? 

trigger setContractAttachment on Attachment (before insert) {
    List<Opportunity> listOpp = new List<Opportunity>();
     for(Attachment att: Trigger.New){
        if(att.ParentId.getSobjectType() == Opportunity.SobjectType) {
            oppId.add(att.ParentId);
            opp.Contract_Uploaded_Date__c=Date.today();
}

//update the Opportunities
    opportunityList = [select id, has_Attachment__c from Opportunity where id is : oppId];
    if(opportunityList!=null && opportunityList.size()>0){
        for(Opportunity opp : opportunityList){
            opp.has_Attachment__c = true;
        }
        update opportunityList;
    }


Test Class

Test Class
@isTest
 
public class ContractAttachmentTest {
    Opportunity = [Select id=;0062C0000022sfT]
    
    Attachment attachTest = new Attachment (Name= 'Test');
    attachTest.body = Blob.valueOf('')
    attachTest.ParentID = b.Id;
    attachTest.OwnerID = U.Id;
    insert attachTest;
        
}
 
Best Answer chosen by Maggie Farley 14
PavanKPavanK
Trigger code

trigger setContractAttachment on Attachment (before insert) {
    List<Opportunity> opportunityList = new List<Opportunity>();
   set<Id> oppId = new set<Id>()l
     for(Attachment att: Trigger.New){
        if(att.ParentId.getSobjectType() == Opportunity.SobjectType) {
            oppId.add(att.ParentId);
       }
}

//update the Opportunities
    opportunityList = [select id, has_Attachment__c from Opportunity where id in : oppId];
    if(opportunityList!=null && opportunityList.size()>0){
        for(Opportunity opp : opportunityList){
            opp.has_Attachment__c = true;
opp.Contract_Uploaded_Date__c=Date.today();
        }
        update opportunityList;
    }
}
 
Test Class

Test Class
@isTest
 
public class ContractAttachmentTest {
    Opportunity b = new Opportunity();
 b.name=’name’;
//Populate all required opportunity fields here
Insert b;
    
    Attachment attachTest = new Attachment (Name= 'Test');
    attachTest.body = Blob.valueOf('')
    attachTest.ParentID = b.Id;

    insert attachTest;
        
}
Hi,

Please refer above trigger class.

Class is not required if you paste above code in trigger.

In test class, below line need to replace with data
//Populate all required opportunity fields here

PLease mark this answer as best if it helped.
 

All Answers

PavanKPavanK
Trigger code

trigger setContractAttachment on Attachment (before insert) {
    List<Opportunity> opportunityList = new List<Opportunity>();
   set<Id> oppId = new set<Id>()l
     for(Attachment att: Trigger.New){
        if(att.ParentId.getSobjectType() == Opportunity.SobjectType) {
            oppId.add(att.ParentId);
       }
}

//update the Opportunities
    opportunityList = [select id, has_Attachment__c from Opportunity where id in : oppId];
    if(opportunityList!=null && opportunityList.size()>0){
        for(Opportunity opp : opportunityList){
            opp.has_Attachment__c = true;
opp.Contract_Uploaded_Date__c=Date.today();
        }
        update opportunityList;
    }
}
 
Test Class

Test Class
@isTest
 
public class ContractAttachmentTest {
    Opportunity b = new Opportunity();
 b.name=’name’;
//Populate all required opportunity fields here
Insert b;
    
    Attachment attachTest = new Attachment (Name= 'Test');
    attachTest.body = Blob.valueOf('')
    attachTest.ParentID = b.Id;

    insert attachTest;
        
}
Hi,

Please refer above trigger class.

Class is not required if you paste above code in trigger.

In test class, below line need to replace with data
//Populate all required opportunity fields here

PLease mark this answer as best if it helped.
 
This was selected as the best answer
Maggie Farley 14Maggie Farley 14
You are amazing! It worked! 
Maggie Farley 14Maggie Farley 14
Hello again,

This worked in Sandbox but when I deployed it to production it did not work. Do I need test cases in Production? 
Maggie Farley 14Maggie Farley 14
Here is an example of the failure. 
User-added image