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
BylithiumBylithium 

Clarifiication on Trigger

This trigger is on Quote and I am stuck on one part I am not able to use IsClosed() on quote status.

 

trigger CloseAllQuotes on Quote (after update) {

system.debug('Close All the Quotes on the Oppurtunity after one Quote is closed');

ID Oppid = Trigger.new[0].OpportunityId;
List <Quote> newQt = new List <Quote> ();

for(Quote Qt: newQT) {
    if((){ ----> I need to chk if previously this was not approved
    Qt = [select id,status,OpportunityId from Quote where OpportunityId=:Oppid]; 
    Qt.Status = 'Approved';
    Update Qt;
           
        }     
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
Going South.ax738Going South.ax738

Oh, i thought you wanted to check on setting approved only on current record status change.
You can use the following logic with some modifications.
Remember, your trigger need to run for bulk updates too.


trigger CloseAllQuotes on Quote (after insert,after update)
{
     List <Quote> newQt = new List <Quote> ();
    Set<ID> ct=new set<ID>();

 for(Quote c:trigger.new)
   {
    if (c.status == 'Approved')
    {
        ct.add(c.OpportunityId);                 
        }   
    }

    for(Quote con :[select id,status from Quote where status != 'Approved' and OpportunityId in :ct]){
            con.status = 'Approved';
            newQt.add(con);
    }   
     update newQt;
}

All Answers

Going South.ax738Going South.ax738

You could use

 

if ( Trigger.oldMap.get(Qt.Status) != 'Approved')

 

 -----------------

 

The standard options for quote are only:

  • —None—
  • Draft
  • Needs Review
  • In Review
  • Approved
  • Rejected
  • Presented
  • Accepted
  • Denied
BylithiumBylithium

I tried it but it throws error...please look into this

 

trigger CloseAllQuotes on Quote (after update) {

system.debug('Close All the Quotes on the Oppurtunity after one Quote is closed');

ID Oppid = Trigger.new[0].OpportunityId;
List <Quote> newQt = new List <Quote> ();

for(Quote Qt: newQT) {
    if (Trigger.oldMap.get(Qt.Status) != 'Approved'){
    Qt = [select id,status,OpportunityId from Quote where OpportunityId=:Oppid];
    Qt.Status = 'Approved';
    Update Qt;
          
        }    
    }
}

Going South.ax738Going South.ax738
use if (Trigger.oldMap.get(Qt.id).status != 'Approved'){
BylithiumBylithium

thanks for that it accepted but the trigger is not updating status of other quotes to 'Approved'

 

please look at this and let me know if I am missing something

 

trigger CloseAllQuotes on Quote (after insert,after update) {

system.debug('Close All the Quotes on the Oppurtunity after one Quote is closed');

ID Oppid = Trigger.new[0].OpportunityId;
List <Quote> newQt = new List <Quote> ();

for(Quote Qt: newQt) {
    Qt = [select id,status,OpportunityId from Quote where OpportunityId=:Oppid];

    if (Trigger.newMap.get(Qt.id).status == 'Approved'){
    Qt.Status = 'Approved';
    Update Qt;
         
        }   
    }
}

Going South.ax738Going South.ax738

Oh, i thought you wanted to check on setting approved only on current record status change.
You can use the following logic with some modifications.
Remember, your trigger need to run for bulk updates too.


trigger CloseAllQuotes on Quote (after insert,after update)
{
     List <Quote> newQt = new List <Quote> ();
    Set<ID> ct=new set<ID>();

 for(Quote c:trigger.new)
   {
    if (c.status == 'Approved')
    {
        ct.add(c.OpportunityId);                 
        }   
    }

    for(Quote con :[select id,status from Quote where status != 'Approved' and OpportunityId in :ct]){
            con.status = 'Approved';
            newQt.add(con);
    }   
     update newQt;
}

This was selected as the best answer
BylithiumBylithium

thanks a lot ..it worked

pmozz01pmozz01

I am trying to run a trigger where all of the work orders related to an opportunity must have a work order stage status of Job Completed or Work Order Closed before the opportunity is updated to Job Completed.  The trigger works in that if any of the work orders are changed to Job Completed or Work Order Closed, the opportunity gets updated to Job Completed, but I want all of the work orders closed; otherwise, don't update the opportunity.  Can you please take a look and see what I am missing?

 

Thanks!

 

Trigger CheckOpportunityWorkorders on Work_Order__c (after insert, after update) {

    List<Opportunity> opportunitiesToUpdate = new List<Opportunity>();
	
    Set<Id> oppIds = new Set<Id>();
	 if(Trigger.isInsert || Trigger.isUpdate)
    //Store Opportunity Id for each Work_Order__c
    for(Work_Order__c workOrder : Trigger.new)
    {
       if (workOrder.stage__c == 'Work Order Closed - Quickbooks' ||  workOrder.stage__c == 'Job Completed' ) 
       oppIds.add(workOrder.Opportunity__c);
    }

	if(oppIds.size() > 0)
	{
		//Retrieve all the Opportunities and related Work Orders regardless the status
		List<Opportunity> opportunities = new List<Opportunity> ([SELECT Id, StageName, (SELECT Id, Opportunity__c, Stage__c FROM Work_Orders__r) FROM Opportunity WHERE Id IN :oppIds]);

		for (Opportunity opportunity : opportunities) {

			Boolean allCompleted = true;
			for(Work_Order__c workOrder:opportunity.Work_Orders__r)
			{	if(workOrder.stage__c != 'Work Order Closed - Quickbooks' && workOrder.stage__c != 'Job Completed')
				{					
					allCompleted = false;
					break;
				}
			}
			
			if(allCompleted = true) 
			{
				opportunity.StageName = 'Job Complete';
				opportunitiesToUpdate.add(opportunity);
			}
		}
	}
	
	if(opportunitiesToUpdate.size() > 0) 
	update opportunitiesToUpdate;
	
}

 

 

Going South.ax738Going South.ax738

your problem description is not clear.

When do you want to update oppurtunities?
is it only when work_order is closed?

If so, your first condition fails on how you pick your list of oppurtunities to be added to the list.

pmozz01pmozz01

Thanks for your response.  I want to update the Opportunity Stage when all of the work orders that relate to that opportunity either are marked Job Completed, Work Order Closed - Quickbooks.  What I am trying to do in the trigger is when a work order is updated to either Job Completed, or Work Order Closed, create a list of opportunities affected.  Check that list of opportunities to see if all the related work orders have one of the two closed values, if all related work orders do have a closed value, then mark the opportunity completed. 

 

The trigger is working so far as updating the opportunity field, but it closes the opportunity if any work order is marked Job Completed or Work Order Closed.  I cannot figure out how to check the list of all opportunities to see that all work orders related are closed.  I appreciate any clarification you may provide.

Going South.ax738Going South.ax738

the above given code would not compile well and you might be running old compiled code?

You should have .....if(allCompleted == true)....  instead of ....if(allCompleted = true) ......

Other than that, code seems ok.