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
sfdclivesfdclive 

trigger help

Hi, 

Iam very new to salesforce please help me for the below senario, please its urgent.

 

I have an opportunity object which have record types opportunity a, opportunity b, when ever i choose the price book for opportunity a record type, then in product it shows the opportunity a in one custom field, for that i created a picklist field on opportunity 

 

please help how to write the trigger for it, on which object we should write the trigger

 

regards

sri

 

Best Answer chosen by Admin (Salesforce Developers) 
Samba GSamba G

Hi sfdclive,

 

I think you said product should be OpportunityLineItem. If it's, please see code below.

trigger UpdateProductRecordType on OpportunityLineItem (after insert)
{
    Set<String> oppLineItemIds = new Set<String>();
    
     for(OpportunityLineItem oppLineItem : Trigger.new)
     {
         oppLineItemIds.add(oppLineItem.Id);
     }
     List<OpportunityLineItem> oppLineItems = new List<OpportunityLineItem>();
     
     for(OpportunityLineItem oppLineItem : [select Opportunity.RecordType.Name, RecordType__c from OpportunityLineItem where Id in :oppLineItemIds])
     {
          oppLineItem.RecordType__c = oppLineItem.Opportunity.RecordType.Name;
          oppLineItems.add(oppLineItem);
     }
     if(oppLineItems.size() > 0)
     {
          update oppLineItems;
     }
}

 

Please let me know if you have any questions.

 

Thanks,

Samba

All Answers

Samba GSamba G
Trigger UpdateRecordType on Opportunity(after insert, after update)
{
     Set<String> oppIds = new Set<String>();

     for(Opportunity opp : Trigger.new)
     {
          if(opp.RecordTypeId != Trigger.oldMap.get(opp.Id).RecordTypeId)
          {
             oppIds.add(opp.Id)
          }
     }
     List<Opportunity> opps = new List<Opportunity>();
     for(Opportunity opp : [select RecordType__c, RecordType.Name from Opportunity where Id in oppIds])
     {
          opp.RecordType__c = opp.RecordType.Name;
          opps.add(opp);
     }
     if(opps.size() > 0)
     {
          update opps;
     }
}

 You need to create a RecordType__c field in the Opportunity object.

 

Thanks,

Samba

sfdclivesfdclive

Hi,

I should display the record type in product not in opportunity.

 

regards

sri

Samba GSamba G

I know. I will change my code.

 

 

Thanks,

Samba

Samba GSamba G

Hi sfdclive,

 

I think you said product should be OpportunityLineItem. If it's, please see code below.

trigger UpdateProductRecordType on OpportunityLineItem (after insert)
{
    Set<String> oppLineItemIds = new Set<String>();
    
     for(OpportunityLineItem oppLineItem : Trigger.new)
     {
         oppLineItemIds.add(oppLineItem.Id);
     }
     List<OpportunityLineItem> oppLineItems = new List<OpportunityLineItem>();
     
     for(OpportunityLineItem oppLineItem : [select Opportunity.RecordType.Name, RecordType__c from OpportunityLineItem where Id in :oppLineItemIds])
     {
          oppLineItem.RecordType__c = oppLineItem.Opportunity.RecordType.Name;
          oppLineItems.add(oppLineItem);
     }
     if(oppLineItems.size() > 0)
     {
          update oppLineItems;
     }
}

 

Please let me know if you have any questions.

 

Thanks,

Samba

This was selected as the best answer
sfdclivesfdclive

Hi samba,

Thanks alot for ur help;

 

but here my problem is i need to display the custom field in product object not in opportunity product object

 

regards 

sri