function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Matthew HamptonMatthew Hampton 

New to APEX Triggers

All:

 

Been searching all day for help, new to APEX triggers, and need to know if anyone can help:

 

I have two customer objects that I am using. I need to run a lookup to pull a field value from one custom object to another.

 

I have the field "Customer Name"  in Household Addresses. I need the value from "Customer Name"  to appear in the "Household Name" field of custom object GPON Addresses, if a certain criteria is met (if the address field is a match). I have tried a VLOOKUP but since the obejcts are not related in the API I cannot get it to work. Please let me know if more detail is needed.

 

Matthew Hampton

nagasnagas

hi mathew you can write a simple trigger to copy the value of customer name form object 1 and insert it into object2. To do this what is the common criteria where these two have unique combination. Like you mentioned address we cannot use address since address is a combination two to three fields. we can do that in different also but any other field which will be uniques between them. If you find any field like that will be unique i can give you the code rightaway

adam_purkissadam_purkiss

Assuming this will happen on insert and on update, you can try something like the following:

 

 

trigger CustomTrigger on CustObject1__c (before insert, before update)
{
    List<CustomObject2__c> c2 = new List<CustomObject2__c>;
    Set<String> compSet = new Set<String>();
			
    for(CustomObject1__c c1 : Trigger.old)
    {
        compSet.add(c1.StringFieldToCompare__c)
    }			

    // One possibility is...
    c2 = [SELECT Id, StringFieldToCompareTo__c FROM CustomObject2__c WHERE StringFieldToCompareTo__c IN :compSet limit 200];

    // or perhaps something like this...
    for(String s : compSet)
    {
        if([SELECT count() FROM CustomObject2__c WHERE StringFieldToCompareTo = :s limit 1] == 1)
        {
            // get result and add to another list.
        }
    }
    
    // Then insert/upsert the entire list like this:
    upsert entireList; 

    // key is to avoid doing DML in loops (insert, update, upsert, delete)
}

 

 

I just wrote that code out and haven't tested it - fyi.  Hope this helps!

 

Cheers,

Adam

Matthew HamptonMatthew Hampton

Thanks for the help. I have created a custom address field in both objects so that field is the common criteria in both objects.

 

The object that I want to lookup from is Household_Addresses_c and the object I want the lookup value to appear on is GPON_Addresses_c

adam_purkissadam_purkiss

I think I may have misunderstood what you're trying to do.  You have two custom objects that each have a custom address field.  Do you want to have a picklist in one that then applies the selection to another custom object instance?  If you consider each custom object to be like a database table with instances of them being rows in those custom tables, from that way of looking at this can you describe what's supposed to happen?

 

You can have as many lookup fields as you wish that create a parent/child relationship from one object(table) to another.  If this type of lookup is closer to what you need then a trigger is probably overkill.

 

Adam

Matthew HamptonMatthew Hampton

Adam:

 

Thanks for the reply.

 

In GPON_Address_c, the Name field is the same as the Name field in Household_Address_c. What I would like to do is for GPON_Address_c to pull a custom field (Customer_Name_c) from Houshold_Address_c IF there is a match on Name.

 

There are also a few other custom fields I would like to do the same with IF there is a Name (field) match. Pretty convoluted reason internally why I have it set up this way.

nagasnagas

try this code

 

 

trigger NameUpdate on Household_Address_c (after update) {
list<GPON_Address_c> gpon = [select id,name__c,household_name__c from GPON_Address__c where name__c=:Trigger.new[0].name__c];

if(gpon.size()>0)
{
gpon[0].household_name__c = Trigger.new[0].customer_name__c;

update gpon;

}
}

 uou may get some complie errors because of API names change the API names accordigly and should work for that.

 

Matthew HamptonMatthew Hampton

This code appears to be what i need...last question since I am a newbie....how do I get this trigger from my Sandbox to my org? I am using Enterprise Edition.

adam_purkissadam_purkiss

Even for beginners' I highly recommend getting the Force.com IDE for Eclipse: Force.com IDE.  From there you can simply right-click your project and select

 

Force.com | Deploy To Server

 

Otherwise you can just create a "new" trigger in production and copy the already tested code from your sandbox.

nagasnagas

Inorder to deploy this trigger to production you also need to write a test class for this trigger and make the coverage atleast 75% and then use the deployment options

 

Deployment options

 

1. Force.com IDE

 

2. setup-->Appsetup-->deploy at this page you will find the process how to deploy your code.

adam_purkissadam_purkiss

Goop point nagas.  I used to think that the 75% minimum is for class code only and the trigger requirement is "Every trigger has 'some' test coverage," not necessarily 75%.  But perhaps this means 75% average coverage over all classes and triggers, while requiring every trigger to have at least some coverage.

Matthew HamptonMatthew Hampton

Any chance I can get you to guide me through this? I need to take the Developer class but have not done so yet. Your help is much appreciated.

 

Also, it will not allow me to write the trigger in my production org.

nagasnagas

yes you are right but considering the future aspects i suggested for 75%.Its always better to maintain 100% coverage in the trigger.

adam_purkissadam_purkiss

Create a dev org to do your development (they're free - all you want) or do it in your sandbox. 

 

The best I can do is this.  First, you probably need to ease back a bit and realize you've got some serious learning time ahead of you.  If you're an experienced developer I'd give it a month of deep practice to get up to speed.  But the short answer for trigger testing is this.  Cause the trigger to be fired from a test method.  Here's an example method that tests when a record is deleted.  You need to add a record to delete, then the trigger gets fired.  The trigger may look something like the following:

 

 

trigger CampaignDelete on Campaign (before delete) 
{
    if(Trigger.isBefore && Trigger.isDelete)
    {
        for(Campaign c : Trigger.old)
	{
            // do something here
        }
    }
}

 and the test method may look something like this:

 

 

@isTest
private with sharing class CampaignDelete_Test 
{
    static testmethod void CampaignDelete_Test()
    {
        // Insert test Campaign
        Campaign c = new Campaign(Name = 'Test');
        insert c;
	
        // This causes your trigger to fire	
        delete c;
    }

    // Good luck,
    // Adam
}