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
Rahavan Arumugam AlameluRahavan Arumugam Alamelu 

Help on trigger for quote update to generate case for quote line item

HI All,

Currently we havfe update trigger which genreate cases for all quote line items satisfying certain condition in prodcut description on items and the status of the quote.

We need to update it, so that the only one case generated per quote. For ex: if we have more than one quote line items satisfying the condition, then the trigger should generate only one case ( not for all line items).

We have PRoduct description and quoteline item no fields on quote line items number. If more than quote line item exists, then we should get one case generated where the case description would append each quote line item number to the product description.

Below is the current code. Can you please help

if (Trigger.isUpdate && Trigger.isAfter) 
    {
        // create ps cases 
        List<Id> acceptedQuoteIds = new List<Id>();
        for (Quote q : Trigger.New) 
        {   
            if (q.Status == 'Accepted' && Trigger.oldMap.get(q.Id).Status != 'Accepted') 
            {
                acceptedQuoteIds.add(q.Id);
            }
        }
        
        System.debug('***** accepted Quote Ids : ' +acceptedQuoteIds);
        
        if (acceptedQuoteIds.size() > 0) {
            //Fetching the assignment rules on case
            AssignmentRule AR = new AssignmentRule();
            AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];

            RecordType rt = [SELECT Id FROM RecordType WHERE developername = 'Pre_Sales_Case' limit 1];

            //Creating the DMLOptions for "Assign using active assignment rules" checkbox
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.assignmentRuleId = AR.id;
        
            List<QuoteLineItem> qlis = [SELECT Product_Description__c, QuoteId, Quote.Ship_To_Account__c, Quote.Opportunity.AccountId, Id, TotalPrice FROM QuoteLineItem where (Product_Description__c like 'Service%' or pricebookentry.productcode like 'SVCPS%' or pricebookentry.productcode like 'MHASOL%' or Product_Type__c = 'Service') and QuoteId in :acceptedQuoteIds];        
            
            System.debug('***** quote line item : ***** ' +qlis);
            
            List<Case> cases = new List<Case>();
            for (QuoteLineItem qli : qlis) 
            {                
                Case c = new Case();
                c.Subject = 'PS engagement - q: ' + Trigger.newMap.get(qli.QuoteId).QuoteNumber;
                c.PS_Quote__c = qli.QuoteId;
                c.Quote_Line_Item__c = qli.Id;
                c.Status = 'INT';
                c.INT_department__c = 'Professional Services';
                c.Severity__c = 'Sev4 – Informational';
                c.Reported_Version__c = 'N/A';
                c.Description = qli.Product_Description__c;
                // c.AccountId = Trigger.newMap.get(qli.QuoteId).Ship_To_Account__c; 
                // c.Partner_Account__c = qli.Quote.Ship_To_Account__c;
                c.Partner_Account__c = Trigger.newMap.get(qli.QuoteId).Ship_To_Account__c;               
                c.AccountId = qli.Quote.Opportunity.AccountId;                
                c.Reason = 'Professional Services';
                c.Origin = 'Web';
                c.Category__c = 'Professional Services';
                c.Type = 'PS';
                c.RecordTypeId = rt.id;
                
                c.setOptions(dmlOpts);
                //c.OwnerId = 
                cases.add(c);
                System.debug('cases to create ***** ' + cases);
            }
            insert cases;
            System.debug('case inserted succesfully ***** ' + cases);
        }     
        
Thanks
Rahavan Arumugam AlameluRahavan Arumugam Alamelu
Can anyone help me on this implementaion please