• Deepu 8.ax1468
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hi all,

 

I am new to writing triggers and I am having a little trouble writing the test class code for the trigger. The trigger is suppose to fire when several Oppportunity fields met a certain criteria. Below is the criteria:

  • Product Line = Non-Standard
  • Products = YES
  • Stage = Propose
  • Opportunity Record Type is not "Active Client : Renewal", "Active Client : Services Only", or "New Client : Services Only"

Now I was not sure how to modifiy the trigger code to look at the Opportunity Record Type field (kept getting an error message saying something about SObject), so instead I created a formula field that puts the Opportunity Record Type field into text (called it Oppty Record Type). The trigger works great when I manually test it out but when I wrote the test class code I get an error message saying

 Error: Compile Error: Field is not writeable: Opportunity.Oppty_Record_Type__c at line 12 column 9

 

So iam assuming that I am getting this error message because it is a formula field, maybe? Is it possible for someone to tell me how to use the Opportunity Record Type as a criteria field in the trigger code or how to modify the test class code?

 

Below is both the trigger and the test class

 

Trigger

trigger OpportunitySubmitForApproval on Opportunity (after update) {
 
    list<Opportunity>updateOpps = new list<Opportunity>();
    
    for (Opportunity opp : trigger.new) {
 
        if (opp.StageName == 'Propose' && opp.Products__c == 'YES' && opp.Product_Line__c == 'Non-Standard' &&
        (opp.Oppty_Record_Type__c != 'Active_Client_Renewal' && opp.Oppty_Record_Type__c != 'Active_Client_Services_Only' && opp.Oppty_Record_Type__c != 'New_Client_Services_Only')) {

            // create the new approval request to submit
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(opp.id);
            // submit the approval request for processing
            Approval.ProcessResult result = Approval.process(req);
            // display if the request was successful
            System.debug('Submitted for approval successfully: '+result.isSuccess());
 
        }
     }
}

 Test Class

@isTest
private class TestOpportunitySubmitForApproval {
 
    static testMethod void testApprovalSuccess() {
 
        Opportunity opp = new Opportunity();
        opp.Name = 'Non-Standard Approval test';
        opp.StageName = 'Demonstrate';
        opp.Product_Line__c = 'Non-Standard';
        opp.CloseDate = Date.today();
        opp.LeadSource = 'Other';
        opp.Oppty_Record_Type__c = 'New_Client_New_System';
        // insert the new opp
        insert opp;
        // change the probability of the opp so the trigger submits it for approval
    opp.StageName = 'Propose';
    // update the opp which should submit it for approval
    update opp;
 
        // ensure that the opp was submitted for approval
        List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :opp.id];
    System.assertEquals(processInstances.size(),1);
 
    }
 
}

 

Thank you!