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
torotoro 

Validation rule on Opportunity for Opportunity product data

I need to setup validation rule on close date in opportunity.

 

What I'm trying to do is alert user if they try to change closed date after ship date in opportunity product. (ship date is custom field in opportunity product)

 

I could not find a way to compare ship date in opportunity product to close date.

 

Also we can have more than one product for an opportunity.

 

Does anyone can tell me how to do this?

Best Answer chosen by Admin (Salesforce Developers) 
torotoro

Ah, I got it working.

 

I need to add "list <OpportunityLineItem>" .

 

All Answers

Starz26Starz26

I believe you will have to write a trigger that:

 

Gets the ship date for the specific product in Oportunity product

 

then compares the value of the close date to that ship date

 

if the close date is > ship date then adderror('Your message here)

 

this will prevent the entry of a close date > ship date.

 

 

torotoro

This is my very first trigger.

 

I would appreciated if you (or somebody) could help me how to write the trigger code...

 

This is what I started, but does not look like it's working.

 

Thanks.

 

trigger closeDate on Opportunity (before update) {
for (Opportunity o:Trigger.new){
OpportunityLineItem opl = new OpportunityLineItem;
opl=[select Ship_Date__C from OpportunityLineItem with OpportunityID =:o.ID];
for (OpportunityLineItem opl){
if (o.CloseDate >= opl.ship_Date__c){
adderror(Close Date has to be on or before ship date. Please change ship date in line item);
}
}
}

 

Starz26Starz26

Try this:

 

trigger closeDate on Opportunity (before update) {


for (Opportunity o:Trigger.new){

   OpportunityLineItem[] lOPL = new OpportunityLineItem[]{};
   lOPL=[select Ship_Date__C FROM OpportunityLineItem WHERE OpportunityID =: o.ID];

for (OpportunityLineItem oOPL : lOPL){

   if (o.CloseDate >= oOPL.Ship_Date__c){
         o.adderror('Close Date has to be on or before ship date. Please  change ship date in line item');
   }
}
}
}

 

 

This IS NOT Bulkified and WILL Not work with bulk Opportunity updates (Think unable to update Opp via dataloader or mass update)

 

Also, this will check ALL Opp Line Items and not check just the ones you are looking for if there could be multiple cship dates and different products.

torotoro

Thank you for your post.

 

When I try it, I got error, " Error: Compile Error: unexpected token: ';' at line 6 column 53".

 

 

torotoro

Ah, I got it working.

 

I need to add "list <OpportunityLineItem>" .

 

This was selected as the best answer