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
Rajeev SatapathyRajeev Satapathy 

Trigger to insert record when quote status changes

Written a trigger ....  wanted to insert a new salesorder record when quote status changes to 'Approved' .. this code is not working 

This is inserting a blank sales order record and opportunity details are not getting populated
--------------------------------------------------------------------------------------------------------------------
trigger Create on Quote (before update) {

List <SCRB_SalesOrder__c> SOlist =  new List<SCRB_SalesOrder__c>();

For(Quote Q : trigger.new)
{
   if(Q.Status == 'Approved')
   {
   SCRB_SalesOrder__c SO = new SCRB_SalesOrder__c();
   SO.OpportunityId__c = Q.Opportunity.ID; // Data not getting populated
   SO.Amount__c = Q.Opportunity.Amount ; // Data not getting populated
   SO.StatusCode__c =  'Billed';
   SOlist.add(SO);
   }
}
insert SOlist;

}
----------------------------------------------------------------------------------------------------------------------------------
Rajeev SatapathyRajeev Satapathy
In quotes  the opportunity field name is :
 Opportunity Name                    Opportunity                        Master-Detail(Opportunity)

and in sales order custom object the opprtunity name is  :

Opportunity  OpportunityId__c  Lookup(Opportunity) 

same with the amount .. but it is not populating values 
Arunkumar.RArunkumar.R
trigger Create on Quote (after insert,after update) {

List <SCRB_SalesOrder__c> SOlist =  new List<SCRB_SalesOrder__c>();

Map<Id,Opportunity> oppMap=new Map<Id,Opportunity>([SELECT Id, Name,Amount FROM Opportunity]);


For(Quote Q : trigger.new)
{
   if(Q.Status == 'Approved')
   {
   SCRB_SalesOrder__c SO = new SCRB_SalesOrder__c();
   SO.OpportunityId__c = Q.OpportunityId;
   SO.Amount__c =oppMap.get(Q.opportunityId).Amount;
   SO.StatusCode__c =  'Billed';
   SOlist.add(SO);
   }
}
insert SOlist;

}

Key points to note:
1. Amount__c field must be Currency field.
2. You can't directly assign the amount field , use map.


If this solutions works for you then Like it and mark this as a solution..!

Thanks ...
Arunkumar.R