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

2 Answers 2

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%.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
can you assist me with the entire piece of code that i will need to write for this? @Aman Rawat
0

Here is the code for inserting an Account,

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

Link to apex guide for the same

2 Comments

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.
i ran the class and it ran just fine, but my trigger code is still at 0% validation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.