0
trigger ShipToAddress on Opportunity(before insert) {
  map<id,account> accounts = new map<id,account>();
  for(opportunity o:trigger.new) {
    accounts.put(o.accountid,null);
  }
  accounts.remove(null);
  accounts.putAll([select id,name,BillingStreet, BillingCity, BillingState, BillingPostalCode from account where id in :accounts.keyset()]);
  for(opportunity o:trigger.new) {
    if(accounts.containskey(o.accountid)) {


      o.Ship_To_Address__c = accounts.get(o.accountid).BillingStreet + ', ' + accounts.get(o.accountid).BillingCity + ', ' + accounts.get(o.accountid).BillingState + ', ' + accounts.get(o.accountid).BillingPostalCode;


    }
  }
}

The above is the trigger i have created in my Sandbox envorment and then moved over to my production environment. I am new to Salesforce, so i am new to creating Apex Triggers or classes to test my Apex Trigger. I am not sure where i need to go within my production environment to create a class, so i can raise my code coverage to 75%. I am also not sure how to create the class or what code i need to write to create the class, so i can then run it and get my code coverage to 75%. Please help me in showing me where i need to go within my Salesforce to create this code and what code i need to test this trigger.

I tested the trigger without having to validate in the Sandbox and it worked just fine.

I was able to deploy this code into my Prod and i run the class, but it is not validating my trigger. what am i doing wrong?

@isTest
public class ShipToAddress {
    public static void TestOne(){
        Account acc = new Account( Name='Test' )  ;
        Insert acc;

        ID acctID = acc.ID;
        //insert opp
        Opportunity opp = new Opportunity( Name='Test', AccountID = acctID, CloseDate = Date.newInstance(2016, 12, 9), StageName = 'Submitted' );
        Insert opp;
    }
}
Ezzy
  • 79
  • 1
  • 8

2 Answers2

0

You need to create a test class which will

  1. Start a test
  2. Will cover the steps so that trigger is invoked.

Example of your test class,

    @isTest
Public class myTestClass{
            static testmethid void unitTest1() {
                        //insert an account
                        //insert opp
            } 

}

After this your trigger will be covered 100%.

  • Is there a specific format for the //Insert an account and //insert opp section of the code? What would the syntax look like? Thank you. – Ezzy Mar 14 '18 at 20:05
  • Sorry, i am very new to this and it would be very helpful to get a detailed was of how this would need to look exactly, so it works and so i can just replace a few words to match what i have. thanks again. – Ezzy Mar 14 '18 at 21:06
  • can you assist me with the entire piece of code that i will need to write for this? @Aman Rawat – Ezzy Apr 13 '18 at 22:11
0

Here is the code for inserting an Account,

Account acc = new Account( Name='Test' )  ;
Insert acc;

Link to apex guide for the same

  • I was able to deploy this code into my Prod and i run the class successfully, but it is not validating my trigger. what am i doing wrong? Look up my updated code. thank you. – Ezzy Mar 15 '18 at 17:15
  • i ran the class and it ran just fine, but my trigger code is still at 0% validation. – Ezzy Mar 15 '18 at 17:24