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
sml9099sml9099 

Need Help: Trigger to update opportunity Product when field on custom object is updated

Hey Guys, 

 

I have an OPPORTUNITY LINE ITEM object. On this, I have a lookup field to SCHEDULE_HEADER__c object. Name of look up field on Opportunity line item is SCHEDULE_HEADER__c. we have one field on schedule_header__c object called SCH_ACTUAL_UNITS__C. we want whatever value is present in sch_actual_units__c, it should be copied to TOTALPRICE field on opportunity line item object. I have written a code , But i am getting this error. Please help.

 

Compile Error: Invalid foreign key relationship: Schedule_Header__c.OpportunityLineItem__c at line 9 column 23

 

trigger OppStatus on Schedule_Header__c (after insert, after update) {


List<ID> schID = New List<ID> ();


for(Schedule_Header__c sch : Trigger.new){
if(sch.Sch_Actuals_Total__c != NULL){
schID.add(sch.OpportunityLineItem__c.Id);
}
}

List<OpportunityLineItem> OppLineUpdateList = [SELECT Id,TotalPrice FROM OpportunityLineItem WHERE id in: schID];
for(integer i = 0; i < OppLineUpdateList.size(); i++){
OppLineUpdateList[i].TotalPrice = schID;
}

update OppLineUpdateList;
}

 

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

In that case you have made wrong trigger.

 

Try to use following trigger code.

 

Apex Trigger:

trigger OppStatus on Schedule_Header__c (after insert, after update) {
	List<OpportunityLineItem> lstOppLiniitemUpdate = new List<OpportunityLineItem>();
	List<Schedule_Header__c> lstScHeader = [select id,(select id from Opportunity_Product__r) from Schedule_Header__c where id in: trigger.newmap.keyset()];
	
	for(Schedule_Header__c sch : lstScHeader){
		if(sch.Sch_Actuals_Total__c != NULL){
			for(OpportunityLineItem oli: sch.Opportunity_Product__r){				
				oli.TotalPrice = sch.SCH_ACTUAL_UNITS__C;
				lstOppLiniitemUpdate.add(oli);				
			}
		}
	}
	if(lstOppLiniitemUpdate.size() > 0){
		update lstOppLiniitemUpdate;
	}	
}

 

All Answers

hitesh90hitesh90

Hi,

 

 Try to use following trigger. you have made small mistake at Line No. 9.

 

Apex Trigger:

trigger OppStatus on Schedule_Header__c (after insert, after update) {
	List<ID> schID = New List<ID> ();
	for(Schedule_Header__c sch : Trigger.new){
		if(sch.Sch_Actuals_Total__c != NULL){
			schID.add(sch.OpportunityLineItem__c);
		}
	}
	List<OpportunityLineItem> OppLineUpdateList = [SELECT Id,TotalPrice FROM OpportunityLineItem WHERE id in: schID];
	for(integer i = 0; i < OppLineUpdateList.size(); i++){
		OppLineUpdateList[i].TotalPrice = schID;
	}
	update OppLineUpdateList;
}

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

sml9099sml9099

Hi Hitesh, 

 

i am getting this error

 

Error: Compile Error: Invalid field OpportunityLineItem__c for SObject Schedule_Header__c at line 5 column 23

 

 

hitesh90hitesh90

what is the API name of OpportunityLineitem Lookup field in Schedule_Header__c object?

sml9099sml9099

Look up field in on opportunityLineItem and Its's looking up to Schedule_Header__c object. API name of look up field on OpportunityLineItem object is Schedule_Header__c

hitesh90hitesh90

In that case you have made wrong trigger.

 

Try to use following trigger code.

 

Apex Trigger:

trigger OppStatus on Schedule_Header__c (after insert, after update) {
	List<OpportunityLineItem> lstOppLiniitemUpdate = new List<OpportunityLineItem>();
	List<Schedule_Header__c> lstScHeader = [select id,(select id from Opportunity_Product__r) from Schedule_Header__c where id in: trigger.newmap.keyset()];
	
	for(Schedule_Header__c sch : lstScHeader){
		if(sch.Sch_Actuals_Total__c != NULL){
			for(OpportunityLineItem oli: sch.Opportunity_Product__r){				
				oli.TotalPrice = sch.SCH_ACTUAL_UNITS__C;
				lstOppLiniitemUpdate.add(oli);				
			}
		}
	}
	if(lstOppLiniitemUpdate.size() > 0){
		update lstOppLiniitemUpdate;
	}	
}

 

This was selected as the best answer
sml9099sml9099

Hitesh, 

 

It worked. Thanks for your pat reply and correcting the code. I really appreciate this.

 

Thanks

sml9099sml9099

Hi Hitesh, 

 

I just realized that It is not updating the totalprice value. FYI, TotalPrice is a standard field on OpportunityLine Item. Trigger is not giving any error but it is also not workong. Can you please help ?

 

Regards

hitesh90hitesh90

Try to use following trigger code.

 

Apex Trigger:

trigger OppStatus on Schedule_Header__c (after insert, after update) {
	List<OpportunityLineItem> lstOppLiniitemUpdate = new List<OpportunityLineItem>();
	List<Schedule_Header__c> lstScHeader = [select id,(select id from Opportunity_Product__r) from Schedule_Header__c where id in: trigger.newmap.keyset()];
	
	for(Schedule_Header__c sch : lstScHeader){
		if(sch.Sch_Actuals_Total__c != NULL){
			for(OpportunityLineItem oli: sch.Opportunity_Product__r){				
				oli.TotalPrice += sch.SCH_ACTUAL_UNITS__C;
				lstOppLiniitemUpdate.add(oli);				
			}
		}
	}
	if(lstOppLiniitemUpdate.size() > 0){
		update lstOppLiniitemUpdate;
	}	
}

 

sml9099sml9099

Hi Hitesh, 

 

Thanks for your reply. It is still not updating . 

hitesh90hitesh90

Try to use below trigger code.

 

trigger OppStatus on Schedule_Header__c (after insert, after update) {
	List<OpportunityLineItem> lstOppLiniitemUpdate = new List<OpportunityLineItem>();
	List<Schedule_Header__c> lstScHeader = [select id,SCH_ACTUAL_UNITS__C,(select id,TotalPrice from Opportunity_Product__r) from Schedule_Header__c where id in: trigger.newmap.keyset()];
	
	for(Schedule_Header__c sch : lstScHeader){
		if(sch.Sch_Actuals_Total__c != NULL){
			for(OpportunityLineItem oli: sch.Opportunity_Product__r){				
				oli.TotalPrice += sch.SCH_ACTUAL_UNITS__C;
				lstOppLiniitemUpdate.add(oli);				
			}
		}
	}
	if(lstOppLiniitemUpdate.size() > 0){
		update lstOppLiniitemUpdate;
	}	
}