0

I have two custom objects : Obj1__c and Obj2__c.

Obj1__c fields: Name, Customer__c, Date__c, Picklist__c(Red,Blue)
Obj2__c fields: Name, Customer__c, Type__c(Type1, Type2) 

I created an apex trigger wherein a new Obj1__c record must be created every time a new Obj2__c is inserted. I have no problem with this.

Here's what I need to achieve:

Picklist__c field from Obj1__c must be inserted with this value: 'Red' if Type__c from Obj2__c is equal to 'Type1', while 'Blue' if Type__c from Obj2__c is equal to 'Type2'.

I am not so sure on how to get the selected picklist value so that I can assign it on a string variable.

Meanwhile, here's what I've done so far:

trigger MyTrigger on Obj2__c (after insert) {

    String selectedPV;
    List<Obj1__c> o1List = new List<Obj1__c>();

    for(Obj2__c o2 : Trigger.New) {

        if(o2.Id != null) { 

            Obj1__c o1 = new Obj1__c();
            o1.Customer__c = o2.Customer__c;
            o1.Date__c = Datetime.now();
            o1List.add(o1);
        }
    }

    insert o1List;
}

Updated Trigger:

trigger MyTrigger on Obj2__c (after insert) {

    List<Obj1__c> o1List = new List<Obj1__c>();

    for(Obj2__c o2 : Trigger.New) {

        Obj1__c o1 = new Obj1__c();
        String selectedPV = o2.Type__c;

        if(selectedPV == 'Type1') {
            o2.Picklist__c = 'Red';
        }

        else {
            o2.Picklist__c = 'Blue';
        } 

        if(o2.Id != null) { 


            o1.Customer__c = o2.Customer__c;
            o1.Date__c = Datetime.now();
            o1.Picklist__c = selectedPV;
            o1List.add(o1);
        }
    }

    insert o1List;
}
0

You can use this logic:

if((o2.Type__c).equalsIgnoreCase('Type1'))
{
    o1.Picklist__c = 'Red';
}
else if((o2.Type__c).equalsIgnoreCase('Type2'))
{
    o1.Picklist__c = 'Blue';
}

Update Remove o1.Picklist__c = selectedPV; from the assignment as you are assigning Red or Blue depending on the if-else logic.

4
  • Hi. Is there any way in which I can make use of my string variable? I am thinking of getting the selected Type__c value and storing it in my string variable, so that I can also insert it with other fields like this : o1.Picklist__c = selectedPV; – eagerlearner Sep 25 '16 at 16:15
  • Yes, you can. String selectedPV = o2.Type__c; – Santanu Boral Sep 25 '16 at 17:37
  • I've modified my post with the updated trigger. It saves successfully, but every time I try it on the record itself, it gives me this error: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MyTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Picklist: bad value for restricted picklist field: Type1: [Picklist__c] Trigger.MyTrigger, on this line insert o1List; – eagerlearner Sep 25 '16 at 18:40
  • "INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST" I guess the error already tells you whats wrong with it. The Picklist__c field is marked as Restricted Picklist and hence it wont accept the values other than what is defined in the Field itself (if RT exists it would be based on them). – Avidev9 Sep 26 '16 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.