+ Start a Discussion
Laura Babb 4Laura Babb 4 

Need help writing apex test for my trigger please!

I have done the Trailhead on Apex Unit Tests, and I just don't seem to be getting it. I wrote a trigger that will update my Opp Stage when my DocuSign Envelope Status = Completed. I need to test this so I can move it production. Your help is greatly appreciated!
Trigger updateRelatedOpportunity on dsfs__DocuSign_Status__c(before insert,before update){
    
    List<Id> oppIds = new List<Id>();
    for(dsfs__DocuSign_Status__c dc:trigger.new){
    if(dc.dsfs__Envelope_Status__c =='Completed'){	
    oppIds.add(dc.dsfs__Opportunity__c);  
    
    }
    }
    
    List<Opportunity> oppList = [select StageName from Opportunity where id in:oppIds];
    List<Opportunity> oppListnew  = new List<Opportunity>();
    
    for(Opportunity op : oppList){
    op.StageName ='SOW Received';
    oppListnew.add(op);
    }
    update oppListnew;

}
Best Answer chosen by Laura Babb 4
Navin Selvaraj23Navin Selvaraj23
Hi Laura,

In process builder, you should check isnull false before  getting any values. So, you will not get any exception on null values. If values are there, it will update the values as per the logic. Then why you are going for trigger seperately. In out of box itslef you can do this implementation.


Regards,
Navin

All Answers

Raj VakatiRaj Vakati
Use this  and add any required fields 
 
@isTest(seealldata = false)
private class updateRelatedOpportunityTest {

@isTest static void testBatch() {

Test.startTest();

Date closeDt = Date.Today();

 

Account a2 = new Account(Name ='icrm testing acc');
insert a2;

opportunity oppr = new opportunity(Name='testing DIE 4/6/2015' ,  AccountId= a2.Id,StageName = 'Prospecting', 
			   CloseDate = closeDt);
insert oppr;


dsfs__DocuSign_Status__c testData2 = new dsfs__DocuSign_Status__c();
testData2.dsfs__Envelope_Status__c ='Completed';
testData2.dsfs__Opportunity__c = oppr.Id;
insert testData2;
Test.stopTest();
}

}

 
Laura Babb 4Laura Babb 4
Thank you for noticing I was missing the Id! I am very new to Apex, so I found this trigger based on someone else’s need in the community. I don’t know why it updates before insert and before update. I was trying to solve for this via process builder, but it kept failing as it was seeing null values in the DocuSign Envelope Status field. So, just to provide some color here, we send contracts through DocuSign on the Opp object, and I need the Opp Stage to update when the DocuSign Envelope comes back ‘Completed’. Is this the best way to do that? Laura Babb | SalesForce System Analyst | Televerde 4636 E. University Dr., Phoenix, AZ USA 85034 Direct 480-771-6727 | Main 480-771-6700 Follow Us: LinkedIn | Twitter | Blog | Televerde.com [cid:image001.png@01D4B3C2.1D0A7940] [cid:image004.jpg@01D4E870.EB67B310]
Navin Selvaraj23Navin Selvaraj23
Hi Laura,

In process builder, you should check isnull false before  getting any values. So, you will not get any exception on null values. If values are there, it will update the values as per the logic. Then why you are going for trigger seperately. In out of box itslef you can do this implementation.


Regards,
Navin
This was selected as the best answer
Raj VakatiRaj Vakati
Can you share the process builder image ?? you need to check the Not null before updating in process builder 
Laura Babb 4Laura Babb 4
Thank you both for your help. I thought because the exception kept failing that I could not utilize process builder. That’s when I searched the community for a solution and found the trigger. Here is what I had in the process builder: [cid:image001.png@01D4E875.2AC26E70] Laura Babb | SalesForce System Analyst | Televerde 4636 E. University Dr., Phoenix, AZ USA 85034 Direct 480-771-6727 | Main 480-771-6700 Follow Us: LinkedIn | Twitter | Blog | Televerde.com [cid:image001.png@01D4B3C2.1D0A7940] [cid:image005.jpg@01D4E875.2AC77780]
Navin Selvaraj23Navin Selvaraj23
Not able to see the image of process builder

User-added image

only cid:image like can able to see

 
Laura Babb 4Laura Babb 4
User-added image
Laura Babb 4Laura Babb 4
I edited my process builder and it worked!