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
Felipe FernandesFelipe Fernandes 

workflow rule - field update

i have a formula field that just make a operation like field 1 + field 2 and I wanna update other field with the result of this field...

Can i do that with workflows?

 

I’m trying to create a roll up summary of a field on 1 object, that is created by a Formula, that looks up to another object... I know the field won't appear and i'm thinking if i can do that with workflow rules...

 

Can someone help me with this?

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
jhurstjhurst

Felipe,

 

I am not sure I follow.  You have two fields on Product.  These Fields are displaying on OpportunityProduct.  You want to roll the combination of these fields to Opportunity.  Does that sound correct?

 

If that is the case, then what you would have to do would be to have an Apex Trigger on the OpportunityLineItem object (which is the API name for Opportunity Products).  The trigger would pull the data from the Product based on the OpportunityLineItem you are creating.

 

In the below, I have 2 fields on Product (Number1__c and Number2__c) and I have 1 field on my OpportuntiyLineItem (ProductCombo__c):

 

 

trigger productComboUpdate on OpportunityLineItem (after insert) {
    //get the Product Numbers and other fields from the OpportunityLineItem and build a list of OpportunityLineItems to update
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    for(OpportunityLineItem oli : [Select Id, PricebookEntry.Product2.Number1__c, PricebookEntry.Product2.Number2__c from OpportunityLineItem where Id in : trigger.new]) {
        oli.ProductCombo__c = oli.PricebookEntry.Product2.Number1__c + oli.PricebookEntry.Product2.Number2__c; //Update the OpportunityLineItem
        oliList.add(oli);
    }
    update oliList; //Update the OpportunityLineItems with the ProductCombo__c field
}

 You would then have the Opportunity Roll-Up field run off of the cobmo field.

 

Hope this helps.

Jay

 

All Answers

jhurstjhurst

Felipe,

 

You can do that with Workflow Field Updates.

 

For instance, suppose I have Field1__c, Field2__c, and ComboFiled__c on Contacts.  I would:

 

1. Create a new Field Update (Setup | App Setup | Create | Workflow & Approvals | Field Updates)

2. Select "Contact" as the object and "ComboField__c" as the field

3. Choose to set the value with a formula:

 

Field1__c + Field2__c

 

4. Click Save

5. Create a new Workflow Rule with your requirements and choose the actions to be the Field Update you created

 

Remember that the action will only happen when your workflow fires, which will be on a save to the record, so this will not retroactively fill out the field values in your existing records.

 

Hope this helps.

 

Jay

Felipe FernandesFelipe Fernandes

Jay,

 

I think workflow rules won't work for me...

Explaining better what i have to do: I have 2 fields that show the value of a product in the object "Product", I have a field in the object "Opportunity Products" that do Field 1 + Field 2, and I need a roll up summary field in the object "Opportunity" to the field in opp products. I can't do the roll up summary because I get 2 field in Products.

With workflow rules, the field will be updated from an action and i believe i need the field already updated, even for values that have been imported before. Tell me if i'm wrong...

 

Any suggestion?

 

Thanks!!

jhurstjhurst

Felipe,

 

I am not sure I follow.  You have two fields on Product.  These Fields are displaying on OpportunityProduct.  You want to roll the combination of these fields to Opportunity.  Does that sound correct?

 

If that is the case, then what you would have to do would be to have an Apex Trigger on the OpportunityLineItem object (which is the API name for Opportunity Products).  The trigger would pull the data from the Product based on the OpportunityLineItem you are creating.

 

In the below, I have 2 fields on Product (Number1__c and Number2__c) and I have 1 field on my OpportuntiyLineItem (ProductCombo__c):

 

 

trigger productComboUpdate on OpportunityLineItem (after insert) {
    //get the Product Numbers and other fields from the OpportunityLineItem and build a list of OpportunityLineItems to update
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    for(OpportunityLineItem oli : [Select Id, PricebookEntry.Product2.Number1__c, PricebookEntry.Product2.Number2__c from OpportunityLineItem where Id in : trigger.new]) {
        oli.ProductCombo__c = oli.PricebookEntry.Product2.Number1__c + oli.PricebookEntry.Product2.Number2__c; //Update the OpportunityLineItem
        oliList.add(oli);
    }
    update oliList; //Update the OpportunityLineItems with the ProductCombo__c field
}

 You would then have the Opportunity Roll-Up field run off of the cobmo field.

 

Hope this helps.

Jay

 

This was selected as the best answer
Felipe FernandesFelipe Fernandes

Jay,

 

Thanks a lot! The code worked for me.

 

How can I test this trigger? I tried to deploy the trigger but I have to do a test...

 

 

thanks...

Felipe.

 

 

Felipe FernandesFelipe Fernandes

Some help?

jhurstjhurst

You would have to create an Apex Test Class which will simulate the actions that will fire the trigger.  So you would:

 

1. Create a dummy Opportunity

2. Start the test 

3. Insert an Opportunity Line Item attached to a Product

4. Assert that the combo field is what you expect

5. Stop the test

 

You can find out about test in the documentation -

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_testing_best_practices.htm?SearchType=Stem

Felipe FernandesFelipe Fernandes

How can i fix this error?

 

 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id; others must specify product id): [PricebookEntryId, unknown]

 

My code:

 

 

 

 

@isTest
private class testMyOpportunityLineItemTrigger {
  
    public static testMethod void testOLITrigger(){
        Opportunity opp = new Opportunity();
        opp.name = 'test opp';
        opp.StageName = 'test';
        opp.LeadSource = 'Google';
        opp.CloseDate = date.today();
                                
        insert opp;

        
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = opp.id;
        oli.Quantity = 1;       
        insert oli;
        

        System.assertequals(oli.valor_sub__c,oli.ProductCombo__c);
    }
}

 

Thanks!

 

jhurstjhurst

There are three things:

 

1. OpportunityLineItems are connected to a PricebookEntry.  This is an object that links the Pricebook2 and the Product2 and determines which product you are actually adding to the Opportunity.

2. OpportunityLineItems have a required field called TotalPrice (the other option you have it to use the default price)

3. You have to query for the ProductCombo__c field after the update.  The Apex object is not updated with the result of the insert.

 

The below worked for my setup:

 

@isTest
private class testMyOpportunityLineItemTrigger {
  
    public static testMethod void testOLITrigger(){
        Opportunity opp = new Opportunity();
        opp.name = 'test opp';
        opp.StageName = 'test';
        opp.LeadSource = 'Google';
        opp.CloseDate = date.today();
                                
        insert opp;

        //Look for the PricebookEntry based on a Product2 Name and a Pricebook2 Name
        PricebookEntry pbe = [select id from PricebookEntry where Product2.Name = 'GenWatt Gasoline 300kW' and Pricebook2.Name = 'Standard Price Book'];
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = opp.id;
        oli.Quantity = 1;       
        oli.PricebookEntryId = pbe.id; //Attache the PricebookEntry to the OLI
        oli.TotalPrice = 2000; //Set a TotalPrice
        insert oli;
        
        //Query for the value of ProductCombo__c
        OpportunityLineItem oliAfterUpdate = [select id, ProductCombo__c from OpportunityLineItem where id = :oli.id];

        System.assertequals(15,oliAfterUpdate.ProductCombo__c);
    }
}

 

You can find what fields are required and optional on the OpportunityLineItem in the documentation - 

http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content/sforce_api_objects_opportunitylineitem.htm?SearchType=Stem

 

Hope this helps.

 

Jay