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;
}