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
newbie2010newbie2010 

Custom Object Trigger

I have a custom object (we will call this one external) that is being populated through a webform.  I have another custom object (we will call this one internal) that is is being created with the data entered into the webform.  The reason for this is there are two master-detail relationships on the internal custom object that would not be accessible on an external site.  These two master-detail relationships are short/controlled lists of objects so I have entered them as picklists on the external object so there is no way to select somehting other than the select group of items we want.  Once the webform has been submitted and the external custom object has been created I need to pass that data to the internal custom object and make the master-detail connections based on the values in the picklists.  I need to create a Trigger to do this and have never created one so I don't even know where to begin.  I have looked through the documentation, but there aren't any examples of this nature so I am not sure how to begin coding this.

sdetweilsdetweil

read this topic apex triggers

 

trigger is a subroutine connected to an object.custom or not.

 

setup ->develop->apex triggers ->new

 

here is a little trigger of mine on the Note object of notes and attachments. the trigger needs to reset a field on the related custom object this note is attached to.

 

trigger NoteTrigger on Note (after insert, after update) {
      map<id, problem__c> problems = new map<id, problem__c>();
      Problem__c o = null;
      
      for(Note a:[select parentid from note where parent.type = 'Problem__c' and id in :trigger.NewMap.keySet()])
      {
      
            o = new problem__c (Id = a.parentid, LastSynchronized__c = null,isAPI__c=false);
            problems.put(a.parentid, o);  // update status field to new
      
      }

      update problems.values(); 

}

 

I want this trigger to fire only AFTER the insert of a new note, or update of an existing.

 

the trigger will be passed an ARRAY of note objects, so you need to process the array of objects.

 

as someone else mentioned on another trigger topic., you don't want to do database updates INSIDE the loop of the trigger

cause this could cause more trigger events..  nasty.. 

 

so, this trigger loops thru the note objects, finds its associated related 'problem' object, and clears the special field.

it then saves that problem object (id and data) in a map used for an soql update call, and at the end of the loop issues the single update for all the affected problem records.

 

in my application, there is a trigger on the problem object too.. so I need to prevent that trigger from firing until AFTER this data is written, cause THAT trigger will try to USE this note data..

 

Sam

newbie2010newbie2010

Sam,

 

Thanks for the post and let me preference any comment I make by letting you know I am very new to coding period so this is a whole new world for me,  I am a prior electrical engineer so I do have the tech tendencies which will hopeful help.  Anyway...if i understand correctly you are changing something that is already there based on a certain condition after a note is inserted or updated, correct?  This leads me to wonder if I need to do the array of objects as I will be creating a completely new object when the other one is submitted.  You could almost say I am trying to recreate the process of converting a lead to an account, contact and opportunity, but I am just converting it to one object instead of three.  I am having a tough time getting my brain to see how to start since everything I see is updating certain fields based on certain conditions.  I just want to do a straight creating of one object from another while connecting two master-detail relationships from two picklist values and this is done everytime the webform is submitted so there is no conditional coding necessary.  Those webforms will never be updated once they have been subumitted into SF so this trigger is only when it is inserted.

sdetweilsdetweil

no problems.. my org isn't in production yet, and I am still learning as well..

 

the array point is because multiple USERS could be inserting at the same time, or you might be using some bulk load tool.

the trigger fires all the time..

 

so, it is best to code for it and not have accidental errors or lost data later..

 

creating one from another, you still have to create a new object (in memory), populate the fields from somewhere (the other object fields)

and then tell the system to 'save'  the new object.   there is no (that I am aware of) method to 'create account from contact' ( just for example purposes).. your code has to do all the work. (thats the 'straight creating' you mentioned.

 

typically triggers do conditional work.. but not always..

 

only on insert.. so you set the trigger condition to 'after insert' the form is stored, data is safe

 

Sam

newbie2010newbie2010

Sam,

 

Thanks for the reply, it actually triggered a thought this weekend. HA!  If I have any issues with the code once written I will post it here for help.

 

Thanks again!!