Skip to main content
Hi,

Heres the requirement that I am working on, can you look into the code that I've written and let me know what needs to be modified. Thank you.

Each Opportunity has multiple OpportunityLineItems(OLI), when all the related OLI's have status = 'QFF Submitted', a checkbox on Opportunity(Fulfillment_Instructions_Complete) should be checked to true.

Similarly when all the related OLI's have status = 'Case Created', another checkbox on Opportunity ( Fulfillment_Cases_Created ) should be checked.

trigger FulfillmentTrigger on OpportunityLineItem (after insert, after update, after delete) {

    

    for(OpportunityLineItem oppLI: Trigger.new){

        boolean B1 = true;

        boolean B2 = true;

        

       

        system.debug('******  ' + oppLI);

        List< OpportunityLineItem > oppLIo = [SELECT Id, Fulfillment_Instructions_Status__c from OpportunityLineItem where Opportunity.Id = : oppLI.OpportunityId];

        

        

        

        for(OpportunityLineItem oli: oppLIo){

          

            String test = oli.Fulfillment_Instructions_Status__c;

            if(test.contains('QFF Submitted')){

                

            }else{

            

                B1 = false;

            

            }

           

            if(test.contains('Case Created')){

           

                

                

            }

            else{

            

                B2 = false;

            

            }

            if(B1){

           oppLI.OpportunityId.Fulfillment_Instructions_Complete__c = true;

            }

            if(B2){

           oppLI.OpportunityId.Fulfillment_Cases_Created__c = true;

            }

            }

        }

    }

 
11 respuestas
  1. 11 jun 2015, 12:28
    I got what you mean, some the trigger is on OLI, and need to update field on Opportunity, right? Here is the code:

    I'm not sure what's your exactly case here, so you may need to modified a little , like what's the relation key between OLI and Opportunity...

    Let me know if you still have any questions~

    trigger FulfillmentTrigger on OpportunityLineItem (after insert, after update, after delete) 

    {

        Map<Id,OpportunityLineItem> OLIids = new Map<Id,OpportunityLineItem>();

        List<OpportunityLine> updateCheckBox = new List<Opportunity>();

        for(OpportunityLineItem oppLI: Trigger.new)

        {

            OLIids.put(oppLI.OpportunityId,oppLI);

        }

        /*What's the relation key between opportunity and OLI ?

        / You have to change something after 'where'

        / For example, if oppLI.OpportunityId= the Id of a Opptunity?

        */

        List<OpportunityLine> oppLIo = [SELECT Id, Fulfillment_Instructions_Status__c from OpportunityLineItem where ??? = : OLIids.keySet()];

        for(Opportunity oli: oppLIo )

        {        

            String test = OLIids.get(oli.OpportunityId).Fulfillment_Instructions_Status__c;

            if(test.contains('QFF Submitted'))

            {

                Opportunity o = new Opportunity(id=oli.OpportunityId); 

                o.Fulfillment_Instructions_Complete__c = true;

                updateCheckBox.add(o);

            }  

            else if(test.contains('Case Created'))

            {

                Opportunity o = new Opportunity(id=oli.OpportunityId);

                o.Fulfillment_Cases_Created__c = true;

                updateCheckBox.add(o);

            }   

        }

        

        if(updateCheckBox.size()>0)

            update updateCheckBox;

    }