+ Start a Discussion
PaulJSPaulJS 

Custom object field update trigger

Hi - I am trying to work on my first trigger and could use some guidance.

I have a custom object (object__c) into which records are being bulk uploaded/inserted.  There is a particular field on this object (appt__c) that includes a code (sometimes abbreviated text, sometimes alphanumeric, etc.) that corresponds to an appointment type.  

I am trying to update a second field on the record (text__c) with a more "readable" text-version of the appointment type.  For example, if "ov" is uploaded into the appt__c field, I want to update the text__c field with "office visit."  There usually will be, however, multiple codes that correspond with the same text version.  For example, all of the following might equal "office visit": "ov," "ov(15)," "office."

This seems like a relatively simple issue, but I can't seem to get to the answer.  Anyone willing to offer any suggestions?

(p.s. I am currently doing this with a field replacement workflow, but there are so many apppointment type codes that I keep running up against limits in my field update formula.) 


Best Answer chosen by PaulJS
RossGRossG
It looks to me like you are trying to update a field value on a record when another field on that record is set on record creation.  Normally this can be resolved with a formula or workflow field update in SFDC, but, it also sounds like you are hitting limits in your workflow field update.  I've had this before.

You can get around that limit with a trigger, like this:  

trigger objectTrigger on object__c (before insert) {
    for(object__c o: trigger.new)
    if(o.appt__c =='ov'
       ||
       o.appt__c == 'office visit'
       ||
       o.appt__c == 'ov(15)'
       ||
       o.appt__c == 'office'
       /*
       ||
       continue inserting possible values here
       */
      )  
    {
        o.text__c = 'Office Visit';
    }
}


All Answers

Bryn JonesBryn Jones
If(appt__c ="ov", "office visit", If(appt__c="ov(15)", "office visit", etc.....or use the OR formula which may make it a bt easier not much though.

This way you can add values that need to be changed easily.

If your willing to mess around a bit, I strongly reccomend Excel Connecter...this will save you a pile of time and you wont have limits popping up. Search on google for it...it is a very garbage website it is on and it is a bit fiddly but once running it is way way better than trying to mess around limits.

Or even better if your on Enterprise of higher is this....https://appexchange.salesforce.com/listingDetail?listingId=a0N300000016YnREAU

Hope this helps.
Please mark as answer if this worked for you.

Bryn

RossGRossG
It looks to me like you are trying to update a field value on a record when another field on that record is set on record creation.  Normally this can be resolved with a formula or workflow field update in SFDC, but, it also sounds like you are hitting limits in your workflow field update.  I've had this before.

You can get around that limit with a trigger, like this:  

trigger objectTrigger on object__c (before insert) {
    for(object__c o: trigger.new)
    if(o.appt__c =='ov'
       ||
       o.appt__c == 'office visit'
       ||
       o.appt__c == 'ov(15)'
       ||
       o.appt__c == 'office'
       /*
       ||
       continue inserting possible values here
       */
      )  
    {
        o.text__c = 'Office Visit';
    }
}


This was selected as the best answer
PaulJSPaulJS
Thanks, Ross.  Doing it like that never occurred to me, but it looks like it's going to work perfectly.

If you don't mind, I'd be interested to know if what I'd originally been trying was at all on the right track...

I created a second object (object__2) whose records consisted of two fields.  The first field was the appointment codes as they might be uploaded (appt2__c), and the second field was the text as I would ultimately want the code displayed (text2__c).  My initial attempt at the trigger broke down as follows:

1. Create a list of all the appt__c values as they were uploaded to first object (object__c).

  List<String> Appts = new List<String>();
    for (Object__c o : trigger.new){
       
        Appts.add(Object__c.appt__c);
    }

2. I then tried querying all the records of object2__c to find those appointment types whose appt2__c value was found in the list I'd previously created, and wanted to update text__c based on the value of text2__c.

  List<String> ApptList = [SELECT Text2__c
                              FROM Object2__c
                              WHERE Appt2__c IN :Appts];

3. I then attempted to update the text__c field on object__c with the corresponding text2__c value from ApptList.  It worked when there was only one record, but failed most of the time telling me my list index was out of bounds.  I think I understand why I got the error (I'm guessing my use of a list in the query is part of the problem), but am not sure how to resolve it.

I'd been trying to take this approach as it seemed it would be easier to add a new appointment type code to object2__c than it would be to modify the workflow/trigger every time I had to add one.

Thanks again for all your help.