You need to sign in to do that
Don't have an account?

New to Apex Class: Please Help!
Guys:
I am very very green when it comes to Apex Classes/Triggers. I have developed the following code in my sandbox and tested it out and it works when I do tests on the objects:
trigger FiberQualification on GPON_Address__c (after insert, after update)
{list<Household_Address__c> gpon = [select id,name, fiber_video_eligible__c, property_owner__c, fiber_internet_eligible__c, complex_location__c from Household_Address__c where name=:Trigger.new[0].name];
if(gpon.size()>0)
{
gpon[0].fiber_video_eligible__c = Trigger.new[0].fiber_video_eligible__c;
gpon[0].fiber_internet_eligible__c = Trigger.new[0].fiber_internet_eligible__c;
gpon[0].complex_location__c = Trigger.new[0].complex_location1__c;
gpon[0].property_owner__c = Trigger.new[0].property_owner1__c;
update gpon;
}}
The code works great when I play around with the objects and does what I need it to do. Now I just need to get it into the Production Org. I was told to use Force.com IDE, which I am in, but now it seems I have to write a test class for this to get to 75% and I admit I have no idea how to do so.
If someone can use the above code and give me a very brief explanation on what code to add to meet the test requirements and to deploy I would appreciate it.
I have tried reading the tutorials but have thus far had no luck.
Thanks
I have tested this out in my instance and works fine. The scenario i followed is when a gpon address object record is created or updated, then all the household address object fields with the sane name as gpon address object record name are updated with the new values.
Note:Change the field datatypes according to your object field datatypes
trigger FiberQualification on GPON_Address__c (after update, after insert) {
public String gponname;
//Change the datatype according to your object
public boolean fvideo;
public boolean finternet;
public String owner;
public String loc;
for(GPON_Address__c gp : Trigger.New){
gponname = gp.Name;
fvideo=gp.fiber_video_eligible__c;
finternet=gp.fiber_internet_eligible__c;
owner=gp.complex_location__c;
loc=gp.property_owner__c;
}
list<Household_Address__c> gpon = [select id,name, fiber_video_eligible__c, property_owner__c, fiber_internet_eligible__c, complex_location__c from Household_Address__c where name=:gponname];
List<Household_Address__c> ha = new List<Household_Address__c>{};
for(Household_Address__c h : gpon){
h.fiber_video_eligible__c = fvideo;
h.fiber_internet_eligible__c = finternet;
h.complex_location__c = loc;
h.property_owner__c = owner;
ha.add(h);
}
update ha;
}
Please let me know if you face any difficulty
All Answers
I did not understand the object on which you are writing a trigger. Anyways try to create the records as follows and proceed accordingly. Is there a relation between GPON_Address__c and Household_Address__c? If so could you please elaborate on that.
To write the test class start as follows:
//Apex class
@isTest
private class testTriggerFiberQualification{
public static testMethod void testTrigger(){
GPON_Address__c gp = new GPON_Address__c();
//insert all the fields in this object
insert gp;
Household_Address__c ha = new Household_Address__c();
//insert all the fields
insert ha;
list<Household_Address__c> gpon = [select id,name, fiber_video_eligible__c, property_owner__c, fiber_internet_eligible__c, complex_location__c from Household_Address__c where name=:gp.Id];
if(gpon.size()>0){
//then update on the fields
update ha;
}
}
Let me know if you face any difficulty.
Thanks for the code. I put it in as a new Apex Class in Force.com IDE and it is still giving me the error Test Coverage of Current Apex Trigger is 0%, at least 1% test coverage is required.
To answer your earlier question, the objects are "linked". When a GPON_Address__c record is inserted, I need to have four fields on Household_Address__C updated with information from the GPON_Address__C record. Any advice you can give on a better way to do this would be appreciated.
OK got it what kind of relationship do you have between the two objects?
And in regards to the previous code i havent completed the whole code have you inserted the records?
I have not inserted the records.
The link between the two is nothing in SF, basically Household_Address__C is an object that has about 1 million records. GPON_Address__C is a subset of those records. The Name field in the two different objects can be the same.
Not all Household_Address__C records have a matching GPON_Address__C record, but all GPON_Address__C records have a Household_Address__C record that has the same name.
Ok Try this code. Populate the fields that are marked in red color with some junk values.
public class testFiberQualification{
static testMethod void testTriggerFiberQualification(){
GPON_Address__c gp = new GPON_Address__c();
gp.Name='Test';
gp.fiber_video_eligible__c='';
gp.property_owner__c='';
gp.fiber_internet_eligible__c='';
gp.complex_location__c='';
insert gp;
Household_Address__c ha = new Household_Address__c();
ha.Name='Test';
ha.fiber_video_eligible__c='';
ha.property_owner__c='';
ha.fiber_internet_eligible__c='';
ha.complex_location__c='';
insert ha;
List<Household_Address__c> gpon = [select id,name, fiber_, property_owner__c, fiber_internet_eligible__c, complex_location__c from Household_Address__c where Name=:gp.Name];
if(gpon.size()>0) {
gpon[0].fiber_video_eligible__c = gp.fiber_video_eligible__c;
gpon[0].fiber_internet_eligible__c = gp.fiber_internet_eligible__c;
gpon[0].complex_location__c = gp.complex_location1__c;
gpon[0].property_owner__c = gp.property_owner1__c;
update gpon;
}
}
}
Please let me know if you face any difficulty.
Thank you for the code. I may be doing this wrong, but in Force.com IDE, I created a new Force.com project, did a new Apex Class, copied the code into that, added the junk numbers, and did the test run but it still had the 0% error message. I added the Apex Trigger to the project and tried to deploy, and it did deploy, however in my Production org, the only thing showing on the trigger is:
trigger FiberQualification on GPON_Address__c (after insert, after update) {
}
I am sure I am missing a step here. Your help thus far is appreciated, I hope I can get this up and running tonight.
Thoughts?
OK, I was able to get this into my Sandbox to run the test, and only got 28%. Here are the lines not tested:
Suggestions?
Ok try to insert more household address records with the same GPON address name and then see. May be it might be the problem with the List
Thank you again for all of your help. The code was in the production org this morning and working.
Now, one last issue that I need help with if you can:
I will be batch inserting and uploading records for the trigger to run and seems that my code is not set up correctly for that. Any ideas on how to correct?
Thanks!
In this case you should use Trigger.new instead of Trigger.New[0]
trigger FiberQualification on GPON_Address__c (after insert, after update) {
list<Household_Address__c> gpon = [select id,name, fiber_video_eligible__c, property_owner__c, fiber_internet_eligible__c, complex_location__c from Household_Address__c where name=:Trigger.new.name or name IN :Trigger.newMap.keySet() ];
//Here you will have to use a for loop to update the four fields on the Household Address records that have the same name like GPON Address
if(gpon.size()>0)
{
for(Integer i =0;i<gpon.size();i++){
gpon[i].fiber_video_eligible__c = Trigger.new[i].fiber_video_eligible__c;
gpon[i].fiber_internet_eligible__c = Trigger.new[i].fiber_internet_eligible__c;
gpon[i].complex_location__c = Trigger.new[i].complex_location1__c;
gpon[i].property_owner__c = Trigger.new[i].property_owner1__c;
}
update gpon;
}
// Instead of the above loop you can use the followin loop and see
List<Household_Address__c> ha = new List<Household_Address__c>{};
for(Household_Address__c h : gpon){
h.fiber_video_eligible__c = Trigger.new.fiber_video_eligible__c;
h.fiber_internet_eligible__c = Trigger.new.fiber_internet_eligible__c;
h.complex_location__c = Trigger.new.complex_location__c;
h.property_owner__c = Trigger.new.property_owner__c;
ha.add(h);
}
update ha;
}
Let me know if you face any difficulty.
Thanks again. I am getting the following error:
Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<GPON_Address__c> at line 2 column 182
Suggestions?
Here is how the trigger is in:
trigger FiberQualification on GPON_Address__c (after insert, after update) {
list<Household_Address__c> gpon = [select id,name, fiber_video_eligible__c, property_owner__c, fiber_internet_eligible__c, complex_location__c from Household_Address__c where name=:Trigger.new.name or name IN :Trigger.newMap.keySet() ];
if(gpon.size()>0)
{
for(Integer i =0;i<gpon.size();i++){
gpon[i].fiber_video_eligible__c = Trigger.new[i].fiber_video_eligible__c;
gpon[i].fiber_internet_eligible__c = Trigger.new[i].fiber_internet_eligible__c;
gpon[i].complex_location__c = Trigger.new[i].complex_location1__c;
gpon[i].property_owner__c = Trigger.new[i].property_owner1__c;
}
update gpon;
}
}
Your eroor might be the highlighted part use anyone condition
trigger FiberQualification on GPON_Address__c (after insert, after update) {
list<Household_Address__c> gpon = [select id,name, fiber_video_eligible__c, property_owner__c, fiber_internet_eligible__c, complex_location__c from Household_Address__c where name=:Trigger.new.name or name IN :Trigger.newMap.keySet() ];
if(gpon.size()>0)
{
for(Integer i =0;i<gpon.size();i++){
gpon[i].fiber_video_eligible__c = Trigger.new[i].fiber_video_eligible__c;
gpon[i].fiber_internet_eligible__c = Trigger.new[i].fiber_internet_eligible__c;
gpon[i].complex_location__c = Trigger.new[i].complex_location1__c;
gpon[i].property_owner__c = Trigger.new[i].property_owner1__c;
}
update gpon;
}
}
Still getting the same error. I am sure this is something small I am missing given my releative lack of experience with triggers.
trigger FiberQualification on GPON_Address__c (after update, after insert) {
list<Household_Address__c> gpon = [select id,name, fiber_video_eligible__c, property_owner__c, fiber_internet_eligible__c, complex_location__c from Household_Address__c where name=:Trigger.new.name];
if(gpon.size()>0)
{
for(integer i=0;i<gpon.size();i++){
gpon[i].fiber_video_eligible__c = Trigger.new[i].fiber_video_eligible__c;
gpon[i].fiber_internet_eligible__c = Trigger.new[i].fiber_internet_eligible__c;
gpon[i].complex_location__c = Trigger.new[i].complex_location1__c;
gpon[i].property_owner__c = Trigger.new[i].property_owner1__c;
update gpon;
}
}
}
Thoughts?
I have tested this out in my instance and works fine. The scenario i followed is when a gpon address object record is created or updated, then all the household address object fields with the sane name as gpon address object record name are updated with the new values.
Note:Change the field datatypes according to your object field datatypes
trigger FiberQualification on GPON_Address__c (after update, after insert) {
public String gponname;
//Change the datatype according to your object
public boolean fvideo;
public boolean finternet;
public String owner;
public String loc;
for(GPON_Address__c gp : Trigger.New){
gponname = gp.Name;
fvideo=gp.fiber_video_eligible__c;
finternet=gp.fiber_internet_eligible__c;
owner=gp.complex_location__c;
loc=gp.property_owner__c;
}
list<Household_Address__c> gpon = [select id,name, fiber_video_eligible__c, property_owner__c, fiber_internet_eligible__c, complex_location__c from Household_Address__c where name=:gponname];
List<Household_Address__c> ha = new List<Household_Address__c>{};
for(Household_Address__c h : gpon){
h.fiber_video_eligible__c = fvideo;
h.fiber_internet_eligible__c = finternet;
h.complex_location__c = loc;
h.property_owner__c = owner;
ha.add(h);
}
update ha;
}
Please let me know if you face any difficulty
I truely appreciate your help. I took the trigger, plugged it into my Sandbox, saved it, went to create a new GPON_Address__C record to test it out, and I got this:
Please disregard previous question, I found the issue. The Loc and Owner where switched at the bottom. I beleive it is now working in the Sandbox. Going to try to deploy to production and see what happens. Thank you again for all your help.
There you go. Got the error. In my code the fields on GPON Address are normal text fields. You have look up fields. This is causing the error.
Can you give me your schema of the GPON Address object so that I can modify the code accordingly
The trigger is working and is in the production org, however when I did a bulk update through the Apex Data Loader, batch size 200, it did fire the trigger onto all of the Household_Address__C records. Is this query set for batch updates or do I need to do something to change so this will work?
When you did bulk update it will fire trigger on the household address records that has the same name as corresponding the gpon address record. So did your trigger update the records correclty according to your senarios in this case?
No it did not update the Household_Address__C records.
could you please mention all the fields in both the objects and the look up object's fields also to get the clear picture of your scenario and an example how you want your trigger to work.