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
chrismartchrismart 

Whats wrong with this simple Apex Trigger?

Hi,

 

I am relatively new to Apex, and have tried but it appears not very well to create a simple trigger. I simply have 2 custom contact fields (one plain text and one a Lookup field). If the plain text matches a string of text I just need to populate the lookup field with a corresponding ID of a custom object. I have tried all sorts using code samples from the boards, etc. but simply this is what I am trying to achieve:

 

trigger myTrigger on Contact (after insert, after update){
if(Contact.Affiliate__c = '123')
Contact.Affiliate_Source__c = 'a0BQ0000003TAAo';
}

 

I will update contacts using Lead conversion and also mass import using the Apex data loader, so I presume any solution will work on each contact upsert.

 

I know there are more complex functions that include looking up the ID, etc. but I am just trying to get this working in it's most basic form - and would appreciate any help. I am sure it's really simple :)

MickleMickle

You shouldn't need the dotnotation of Contact.Afiilicate__c, since you are already in the realm of the Contact object. Dotnotation is used to traverse relationships. 

 

Secondly, to test if two things are equal you want to use  == intead of just =

 

Third, when the if statement extends more than one line, you need to tell it what's included with { }:

 

Try the following code, it should work.

 

trigger myTrigger on Contact (after insert, after update){
if(Affiliate__c == '123'){
	Affiliate_Source__c = 'a0BQ0000003TAAo';
	}
}

 

 

chrismartchrismart

Hi,

 

I really appreciate you taking the time to reply...

 

I have tried the suggested code, but get the error:

 

Error: Compile Error: Variable does not exist: Affiliate_Source__c at line 3 column 5

 

Affiliate_Source__c is a lookup field for the Affiliate custom object that I am trying to associate the contact with.

 

Thanks again to anyone who can help!

sdetweilsdetweil

triggers are passed an array of affected objects.. not just one at a time, so you need to process the array

 

this is a before update trigger

 for(integer i=0; i< Trigger.size; i++ )
        {
            object_type changedObjectNew = Trigger.new[i];

       }

 

 

or another loop way

this is an after insert trigger

      for(Comment__c a:[select Related_Problem__c, isApi__c, creator__c, createdById from Comment__c where id in :trigger.NewMap.keySet()])      
      {

          if(a.isApi__c==false)
          {

          }

     }

 

so, yours might be

 

trigger myTrigger on Contact (after insert, after update){

 for(integer i=0; i< Trigger.size; i++ )
        {

            Contact changedObjectNew = Trigger.new[i];     

            if(changedObjectNew.Affiliate__c = '123')
                      changedObjectNew.Affiliate_Source__c = 'a0BQ0000003TAAo';

       }

}

 

Sam

Kevin SwiggumKevin Swiggum

I'll build on Sam's solution

 

The one thing that jumped out at me was that you're using After Insert, After Update. In this case, you're looking at a field on the Contact (that I'm assuming the user set) and you just need to set another field (a lookup) on the same record. If that's the case, you should be using before insert, before update. The primary advantage...you're setting the value before the record is actually saved...so you're setting the lookup field won't require an additional save (since After Insert/Update means that the record has already been saved). See this for more info

 

So here's my recommended solutions (again, building on Sam's code)

 

trigger myTrigger on Contact (before insert, before update){
     for(Contact changedObjectNew : Trigger.new) {
          if(changedObjectNew.Affiliate__c == '123')
               changedObjectNew.Affiliate_Source__c = 'a0BQ0000003TAAo';
     }
}

 Keep in mind that there are other items in this code that are going to ding you when you try to push this to production. Most specically, the hard coded ID obviously won't be the same ID you have in production....which would require you to manually change that code directly in production (which is bad). You may already have a handle on this, but I thought I'd mention it. If it's really just one ID that you need to look at, you could just store it in a custom setting or custom label. Otherwise you'd want to get at it from a query using affiliate__c (just make sure you don't put that query within your for loop....search for bulkifying triggers for more info on that)

 

Hope that helps

 

--Kevin

 

sdetweilsdetweil

thanks Kevin. I haven't used Apex enough (just becoming SF customer) to learn the loop (and other) control language capabilities.  that is SO much cleaner, and with trigger size limits, very important..

 

Sam

sdetweilsdetweil

and ChrisMart, not only is there an array of trigger.new,  but there is also a trigger.old.. so you can compare for changes and do something different..

 

I use this on one of my triggers to tell if this is the first time (field being set in New, not yet set in old).

and in another trigger I use the new field contents to tell me the data is coming from one source, but then

reset the field so it doesn't get stored set.

 

sam