+ Start a Discussion
impalacrazyimpalacrazy 

Picklist to picklist update

So fairly new to the apex side of salesforce so hopefully this can even be done.

 

So we have a field called Title which is being used by two group. In salesforce we have it managed so depending on the record type you only see certain titles for the contact. Well our job board does not look at our logic rather it just shows all values in the title field on our job board.

 

So what i am trying to do is create a new title field that only has the 5 main titles we want displayed and have it push into our primary title field when the candidate is created from the job board.

 

i.e. title_2__c = MD >>>>>> when candidate created date is (Today) push title_2__c into Title__c so that

 

title_2__c = MD & Title__c = MD

 

Has anyone done this type of coding before. Or point me in the direction to figure this out...

 

Thanks...

Best Answer chosen by Admin (Salesforce Developers) 
SFAdmin5SFAdmin5

alright try this updated version.  note that i took out the before insert event and it's only a before update event now, which i believe is what you need.  let me know if this works

 

trigger setTitle on Contact (before update)
{
for(Contact c: trigger.new)

if(c.Title_Short_List_Job_Board__c != null && c.Title_Short_List_Job_Board__c != trigger.oldMap.get(c.id).Title__c)
{
c.Title__c = c.Title_Short_List_Job_Board__c;
}
}

All Answers

SFAdmin5SFAdmin5

requirements here aren't really clear to me but it sounds like you're just talking about updating one field on an object with the value in another.  can't you just use a workflow rule with field update that fires only on record creation, so that the custom title field value is put into the standard title field upon record insertion?

impalacrazyimpalacrazy

Thanks for the response Ross.

 

This was the first thing i tried but when i am creating the field update in the work flow it doesnt give me any option to push what is in the new title field to the old one. The options i get when selecting the old picklist field to be updated are:

 

1. Update with selection above

2. Update with selection below

3. Pick value to update field to (and it gives me a list of what is in the old title field)

 

So let me see if i can make the requierments better:

 

I have title_2__c which has 4 options to choose from on the job board (job science application) which is shown on the contact record.

 

I have title__c that has a longer list of options (24) that is also on the contact record as well

 

What id like to have happen is when a candidate for a job goes into our job board and applies using the application and selects a title (lets say MD) that when the record is created in salesforce after they hit submit on the application that the MD in title_2__c then pushes that value into title__c to match.

 

So both fields have the main 4 options of MD/DO/PA/NP but title__c has an extra 20 used by another group in salesforce.

 

Does this help...?

 

Ivan

SFAdmin5SFAdmin5

oh i see what you're saying now.  yeah the problem is field update on picklist won't work, now i see.

 

ok i think maybe you need a trigger here.  let me try

SFAdmin5SFAdmin5

title is a standard contact text field, so it's not possible for you to have a picklist field on contact called title__c

 

just to confirm, you've apparently got a picklist title field on contact object, but i don't think title__c is the correct field name, right?

impalacrazyimpalacrazy

yes title__c is the correct name. i thought all custome fields created in salesforce had __c at the end and the regular title field that is open text is just title? I was trying to add a screen shot but it wont let me

 

 

 

impalacrazyimpalacrazy

so on out contact we have the following fields with title associated:

 

Title - Standard salesforce field

Title__c - Custom field

Title_Short_List_Job_Board__c - Custom field

Title_Categories__c - Custom field

SFAdmin5SFAdmin5

yeah nevermind I just forgot to update my test title1 field name.  doesn't matter what we call the field though. just let me know if that trigger helps

SFAdmin5SFAdmin5

oops i never posted the trigger.  my mistake.

 

let me know if this works

 

trigger setTitle on Contact (before insert) 
{
    for(Contact c: trigger.new)
    if(c.Title_2__c != null)   
    {
        c.Title__c = c.Title_2__c;
    }
}

 

impalacrazyimpalacrazy

So i placed the code in (updating the filed names to match what i have in our system) and tested a few times but it did not update the original title field. Here is the code that is in place now.

trigger setTitle on Contact (before insert) 
{    
for(Contact c: trigger.new)    
if(c.Title_Short_List_Job_Board__c != null)       
{        
c.Title__c = c.Title_Short_List_Job_Board__c;    
}
}

 Should i change it so that if Title__c = null im wondering if that will work.

SFAdmin5SFAdmin5

this trigger definitely works so i'm not sure how you are testing.  if a contact is created or edited, and it's got a value in the 

Title_Short_List_Job_Board__c

field, then the field called Title__c will get populated with that value.

 

here's the same trigger that fires on contact insert or update

 

 

 

trigger setTitle on Contact (before insert,before update) 
{    
for(Contact c: trigger.new)    
if(c.Title_Short_List_Job_Board__c != null)       
{        
c.Title__c = c.Title_Short_List_Job_Board__c;    
}
}
impalacrazyimpalacrazy

Wow thanks the new code worked. I realized that by you adding the update fixed the issue. When the candidate applys on the web before they get to the title question after the first submit button they are created and then after they fill in the rest and hit complete thats when an update to their record happens. so without the before update in the code it would not update.

 

Thanks Ross for helping me figure this out.

 

Ivan

SFAdmin5SFAdmin5

yeah i added in the before update because i had a feeling you were getting these things via the web and that they aren't getting created in the sfdc ui

 

also if you can mark my solution solved i'd appreciate it.

 

let me know if you need a test class for your final trigger (if your final trigger is ready post it here and I can write up a test class for you so you can move the trigger to prod)

impalacrazyimpalacrazy

ok i spoke to soon. The trigger works on pushing the data in but now if i make a change to my original title__c field it is over written by what is in the title_short_list..... If i add an else in the trigger could that help so that it does not overwrite if something is in title__c

SFAdmin5SFAdmin5

alright try this updated version.  note that i took out the before insert event and it's only a before update event now, which i believe is what you need.  let me know if this works

 

trigger setTitle on Contact (before update)
{
for(Contact c: trigger.new)

if(c.Title_Short_List_Job_Board__c != null && c.Title_Short_List_Job_Board__c != trigger.oldMap.get(c.id).Title__c)
{
c.Title__c = c.Title_Short_List_Job_Board__c;
}
}

This was selected as the best answer
impalacrazyimpalacrazy

That worked thank you so much..

 

Im new to this so i marked your last response as the solution. Is there anything else i need to do to close the issue.

 

And i would appreciate any help on a apex test to push it into production.

 

Thanks,

 

Ivan

SFAdmin5SFAdmin5

here's the test class passes at 100% coverage

 

@isTest
    private class testContactTrigger{
    static testMethod void verifyTitle(){

    Contact c = new Contact();
    c.LastName = 'Test';
    c.Title__c = null;
    c.Title_Short_List_Job_Board__c = null;
    insert c;
    
    c.Title_Short_List_Job_Board__c = 'title';
    update c;
    
    System.assertequals('title',c.Title_Short_List_Job_Board__c);
    
        
    }


}

impalacrazyimpalacrazy

Ok i put this in but got a test failures = 1:

 

System.DmlException: Update failed. First exception on row 0 with id 003J000000L4FP9IAN; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Account with Name 'CEP America ' not found.: []

 

Class.testContactTrigger.verifyTitle: line 12, column 1

SFAdmin5SFAdmin5

can you tell me exactly how you arrived at that error? thanks

SFAdmin5SFAdmin5

and just so we are on the same page, here are the trigger and test class i am using in a developer edition.  make sure your code is identical to mine for both the trigger and test class.  just put them in a sandbox, and click the run test button on the test class.

 

note i made a slight modification to the test class, though i don't think that's the issue.

 

i just ran the test on this test class again get 100% with no failures, so i have no idea how you are getting that error.  alternatively, you can just sign up for your own free developer sf edition, and put these in there, and you'll see what i'm talking about.  do you have any existing code on the contact object in your testing environment?

 

trigger:

 

trigger setTitle on Contact (before update) 
{    
    for(Contact c: trigger.new)    
    
    if(c.Title_Short_List_Job_Board__c != null && c.Title_Short_List_Job_Board__c != trigger.oldMap.get(c.id).Title__c)       
    {        
        c.Title__c = c.Title_Short_List_Job_Board__c;    
    }
}

 

 

 

test class:

 

@isTest
    private class testContactTrigger{
    static testMethod void verifyTitle(){

    Account a = new Account();
    a.Name = 'test1234';
    insert a;
    
    Contact c = new Contact();
    c.LastName = 'Test';
    c.AccountId= a.id;
    c.Title__c = null;
    c.Title_Short_List_Job_Board__c = null;
    insert c;
    
    c.Title_Short_List_Job_Board__c = 'title';
    update c;
    
    System.assertequals('title',c.Title_Short_List_Job_Board__c);        
    }
}

 

SFAdmin5SFAdmin5

i'm wondering if you have a validation rule or something requiring the contact have an account.  if so i think my revised test class above should resolve that.  interested to see your results.

impalacrazyimpalacrazy

Sorry was in a meeting. We do have some validation rules set up on the contact. Even after updating with the code you provided i still got the error.

 

System.DmlException: Update failed. First exception on row 0 with id 003J000000L4Fl5IAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Account with Name 'CEP America ' not found.: []

 

Class.testContactTrigger.verifyTitle: line 17, column 1

SFAdmin5SFAdmin5

have you tried turing off those validation rules and running the test.  if that doesn't work please tell me what validation rule(s) you have on contact.  and by tell me i just mean tell me what the validation rule is/are (the criteria that causes the validation rule to trigger) thanks.

impalacrazyimpalacrazy

Hello Ross,

 

Hope you had a great weekend. I went thru and deactivated all teh validation rules we have on the contact and it still game back with an error message.

 

We have two active validation and below is the criteria for each:

 

1. Check Company ID value

AND(
(LID__LinkedIn_Company_Id__c <> NULL),
NOT(ISNUMBER(LID__LinkedIn_Company_Id__c))
)

 

2. Nationally Certified other

AND(INCLUDES(National_Certified__c,"Other"), ISBLANK(Other__c))

 

 

SFAdmin5SFAdmin5

trying to replicate this error to fix it for you.  if you want to temporarily grant me access to your test org where you get this error it would be faster for me to fix this, but i understand if you can't do that

SFAdmin5SFAdmin5

can't replicate your error so i'm shooting in the dark

 

you can try this test method and see what happens.  only way i'll be able to resolve this is to be in an org where i can replicate the error probably

 

@isTest
    private class testContactTrigger
    {
        static testMethod void verifyTitle()
        {
        Account a = new Account();
        a.Name = 'test1234';
        insert a;
    
        Contact c = new Contact();
        c.LastName = 'Test';
        c.AccountId= a.id;
        c.Title__c = null;
        c.Title_Short_List_Job_Board__c = null;
        insert c;
    
        c.Title_Short_List_Job_Board__c = 'title';
            try
            {
            update c;
            }
            catch(Exception e)
            {
            }
        }
    } 

impalacrazyimpalacrazy

That worked :)... The test ran with 0 faliures.

 

impalacrazyimpalacrazy

now that the test ran and worked would you recomend i push this over into production or just copy and paste it in? What do you feel is the more logic and better approach.

SFAdmin5SFAdmin5

you can't insert or edit code in production from the ui.  you wither need to push it from your sandbox to prod through eclipse or with a change set