Reputation: 71
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;
}
}
Upvotes: 0
Views: 2073
Reputation: 1
Here is the code for inserting an Account,
Account acc = new Account( Name='Test' ) ;
Insert acc;
Link to apex guide for the same
Upvotes: 0
Reputation: 1
You need to create a test class which will
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%.
Upvotes: 0