function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MayankDkPantMayankDkPant 

Trigger to Update Picklist Value

Hi,

 

My scenario:

  I have a custom object A and opportunity has a look-up relationship with this object.

  For custom Object A : I have 4 fields: F1, F2, F3, F4.(Picklist)

 In opportunity object also I have 4 fields of same type(Picklist) with same list of values.

 

Requrirement:

 

 When user saves the record for opportunity object then based on record A selectd by user via look-up relation ship, all the values for F1, F2,F3,F4 for 'A' record should be auto saved /inserted for opportunity record.

 

Can any one tell me how to achieve this using trigger as we don't have this feature using workflow.

 

Any help will be appreciated.

 

With Regards,

Mayank Pant

 

Praful GadgePraful Gadge

Hi Mayank Pant,

 

Here, is a trigger for your scenario.

 

I have written a complete trigger rather than giving you hints, As it was so much simple.

 

So, implement this and let me know.

 

/* START - Trigger */

trigger
fieldUpdateTrigger on Opportunity (before insert, before update)
{ myTriggerHandler.updateFields(Trigger.new); }

/* END - Trigger */

/* START Trigger handler */

public with sharing class myTriggerHandler
{
    public void updateFields(List<Opportunity> listNew)
    {
Set<Id> aId = new Set<Id>();
Map<Id, Opportunity> mapAIdOpp = new Map<Id, Opportunity>();
List<A> listOfa = new List<A>();
for(Opportunity opp : listNew)
      {
          aId.add(opp.A);
mapAIdOpp.put(opp.A, opp);
      }

/* Updating Fields */
for( A a : [SELECT Id, F1 FROM A__c WHERE Id IN: aId] )
{
a.F1 = mapAIdOpp.get(a.Id).F1;
a.F2 = mapAIdOpp.get(a.Id).F2;
a.F3 = mapAIdOpp.get(a.Id).F3;
a.F4 = mapAIdOpp.get(a.Id).F4;
listOfa.add(a);
}

update listOfa;
}
}
/* END Trigger handler */

 

Sincerely,

Praful G.

MayankDkPantMayankDkPant

Thanks.

 

I might not have mentioned it clearly.

 

You have done the opposite. I want that corresponding fields of  F1,F2,F3,F4 in opportunity object should be auto saved when user selects the look-up record for A. I don't wan to use apex classes.

 

Thanks again.

 

With Regards,

Mayank Pant

sravan alaparthisravan alaparthi

Dear Mayank,

 

hoping you found a solution for this based on your requirements....  

 

Workflow rules can only be used in the following cases:

 

  • When you want to update a field
  • Assign a task
  • Send Outbound messages
  • Send email

As you clearly stated we cant use workflow rules in this scenario,so the only solution as of my knowledge is using triggers where obviously we have to write Apex code.

 

please do let us know of the solution you came up with which might help us in future......