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
Shruti VishShruti Vish 

I want to update opportunityline item object based on status of opportunity i e closed Won

Here is my code Any help is appreciated
trigger Testopp on Opportunity (after insert, after update) {
    for(Opportunity opp:Trigger.new){
        if(opp.StageName=='Closed Won'){
            OpportunityLineItem op=new OpportunityLineItem();
            op.Demo5__Ready_to_close__c = true;
        }
    }
        


}
Best Answer chosen by Shruti Vish
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger Testopp on Opportunity (after insert, after update) 
{

	Set<Id> setOppId = new Set<ID>();
    for(Opportunity opp:Trigger.new)
	{
        if(opp.StageName=='Closed Won')
		{
			setOppId.add(opp.id);
        }
    }
	
	if(setOppId.size() > 0 )
	{
		Map<Id,Opportunity> mapOppWiseOLI = new Map<Id,Opportunity>( [select id ,
																	(select id,Demo5__Ready_to_close__c  from OpportunityLineItems ) 
																		from Opportunity where id in :setOppId 
																	] );

		List<OpportunityLineItem> lstOLItoUpdate = new 	List<OpportunityLineItem>();
		
		for(Opportunity opp:Trigger.new)
		{
			if(opp.StageName=='Closed Won' && mapOppWiseOLI.containsKey(opp.id) )
			{
				List<OpportunityLineItem> lstOLI = mapOppWiseOLI.get(opp.id).OpportunityLineItems;
				For(OpportunityLineItem oli : lstOLI )
				{
					oli.Demo5__Ready_to_close__c = true;
					lstOLItoUpdate.add(oli);
				}
			}
		}
		if(lstOLItoUpdate.size() > 0 )
		{
			update lstOLItoUpdate;
		}
	}
}

Let us know if this will help you
 

All Answers

Lokesh KumarLokesh Kumar
Hi Shradha,

As you have written a trigger on after update event so for this you have to explicit write a DML Insert or update to get it work.

Because in after event the auto DML Phase will not available so update your trigger like below. 

Add the line Insert op;

op.Demo5__Ready_toclose__c = true;
insert op;

let me know if this helps you
Lokesh
 
Shruti VishShruti Vish
When i tried above code i m getting below error


Apex trigger Demo5.Testopp caused an unexpected exception, contact your administrator: Demo5.Testopp: execution of BeforeInsert caused by: System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity, Quantity]: [Opportunity, Quantity]: Trigger.Demo5.Testopp: line 10, column 1
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger Testopp on Opportunity (after insert, after update) 
{

	Set<Id> setOppId = new Set<ID>();
    for(Opportunity opp:Trigger.new)
	{
        if(opp.StageName=='Closed Won')
		{
			setOppId.add(opp.id);
        }
    }
	
	if(setOppId.size() > 0 )
	{
		Map<Id,Opportunity> mapOppWiseOLI = new Map<Id,Opportunity>( [select id ,
																	(select id,Demo5__Ready_to_close__c  from OpportunityLineItems ) 
																		from Opportunity where id in :setOppId 
																	] );

		List<OpportunityLineItem> lstOLItoUpdate = new 	List<OpportunityLineItem>();
		
		for(Opportunity opp:Trigger.new)
		{
			if(opp.StageName=='Closed Won' && mapOppWiseOLI.containsKey(opp.id) )
			{
				List<OpportunityLineItem> lstOLI = mapOppWiseOLI.get(opp.id).OpportunityLineItems;
				For(OpportunityLineItem oli : lstOLI )
				{
					oli.Demo5__Ready_to_close__c = true;
					lstOLItoUpdate.add(oli);
				}
			}
		}
		if(lstOLItoUpdate.size() > 0 )
		{
			update lstOLItoUpdate;
		}
	}
}

Let us know if this will help you
 
This was selected as the best answer
Shruti VishShruti Vish
Thank you for your valueable time It worked
tulasiram chtulasiram ch
Amith i used your code like below , but i am getting error : Initial term of field expression must be a concrete SObject: Boolean
I wrote a trigger for updating contact Phone, Title based on Account Ownership == 'Private' (picklist) and Phone, AccountNumber
if(Account Ownership == 'Private')
contact Phone = account phone
contact   Title = account  AccountNumber ...But i am getting error Can anyone explain me the Solution. Just Learning and Practicing based on someones scenarios ...Thank you

Code: trigger updateFieldsOnContact on Account (after insert, after update)
{
    set<id> setAccid = new set<id>();
    for(Account acc:trigger.new){
        if(acc.Ownership =='Private' && acc.AccountNumber !=  Null && acc.Phone !=Null){
            setAccid.add(acc.Id);
        }
    }
    if(setAccid.size()>0)
            {
                map<id, account> mapAccCons = new map<id, account>([select id, (select id, Phone, Title from Contacts) from Account where id in :setAccid]);
               list<Contact> cons = new List<Contact>();
                for(account acc:trigger.new)
                        {
                            if(acc.Ownership == 'Private' && mapAccCons.containsKey(acc.Id).Contacts)
                                {
                                List<Contact> newUpdatingCons = mapAccCons.get(acc.Id).Contacts;
                                for(Contact cont : newUpdatingCons){
                                    cont.Phone = acc.Phone;
                                    cont.Title = acc.AccountNumber;
                                    cons.add(cont);
                                    
                                }
                                
                         }
                   
               }
                if(cons.size()>0){
                    update cons;
                }
        
    }
}