+ Start a Discussion
Emma Watson 2Emma Watson 2 

How to set the value of picklist field using Apex?

Hi All,

I have an Incident Object which hold status field(which is pickList). Initial value of status is 'OPEN' and I want to set the value to 'ASSIGN'. How to set this value using apex.

Please suggest for the same.

Thanks!
Maharajan CMaharajan C
Hi Emma,

Try the below simple one:

trigger JustUpadate on Incident__c (before Insert,before update) {
for(Incident__c l:trigger.new)
{
l.status='ASSIGN;
}
}

Can you please Let me know if it works or not and also If you face any problems!!!

If it works don't forget to mark this as a best answer!!!

Thanks,
​Raj
Emma Watson 2Emma Watson 2
CodeError
Please find the attached error.

Incident is an Object and on this it has a status field which is lookup
field. This Status is also an object when we check in object list.

I tried the above code which you have shown but it gives an error.

Please suggest for the same.

Thanks!
Maharajan CMaharajan C
Copy and paste the Lookup Record ID instead of ASSIGNED

If it is a lookup field then we have to pass the ID.

If you just want to populate all the Incident record with one status record then simply copy and paste the ID of the Record in the Trigger.

Otherwise Is there any field to compare the two object.

And also explain the requirement clearly?

The below trigger is sample one to populate the lookup field based on text between two object:
-Object 2 have a lookup field to Object 1
-we have the two same text values in both object which is Concate__c
-Based on the above field we have to get the ID of Record from Object1 to Object2.

trigger UpdateField on Object_2__c (before insert,before Update) {

Set<string> cons=new set<string>();
for(Object_2__c obj1:trigger.new)
{
if(obj1.Concate__c != null)
cons.add(obj1.Concate__c);


List<Object_1__c> obj1list=[Select Id,BDE__c,Capaingn__c,concate__c from Object_1__c where concate__c IN :  cons];

Map<String, Object_1__c> obj1concate= new Map<String,Object_1__c>();
  for (Object_1__c o : obj1list) {
    obj1concate.put(o.concate__c,o);
  }
  
Object_1__c obj2=new Object_1__c();
for(Object_2__c obj1:trigger.new)
{
if(obj1.Concate__c != null)
obj2=obj1concate.get(obj1.Concate__c);
obj1.RelatedObject_1__c=obj2.Id;

}

Can you please Let me know if it works or not and also If you face any problems!!!

If it works don't forget to mark this as a best answer!!!

Thanks,
​Raj
Emma Watson 2Emma Watson 2
Trigger UpdateError Id

Thanks for update...

I have an incident object, here there are 2 fields Queue and Staff(which
are lookup field). Initial status of field is 'OPENED' by default. Based on
the below screenshot I want to set the status field to 'ASSIGNED'.But I am
getting the below error.
 
Actually, I tried the by process builder also but I am getting the error. 

Please suggest for the same.

Thanks!