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
BKWBKW 

Using Case field history as part of an "if" statement in a trigger.

Hello,

I am new to working with Apex and I have what I suspect is in an easy question. I have a trigger that I have written to create a record on a custom object on Insert or Update of case with a specific topic. The issue I am having (as you would guess) is that it creating a new record everytime the case is updated.  I only want it to fire on update if the record is being saved with the "Price" topic when it wasn't previously. Anyone have a good idea for that?  I think I am close if I could get that answer.

 

 trigger CreatePriceRequestFromCase on Case (after Insert, after update){List<Request__c> Req =
new List<request__c>();
 
   String  RequestTypeId = Functions.getRecordTypeId( 'Price', 'Request__c' );

 if (trigger.isInsert){
       for (Case newCase: Trigger.New) {        
         if (newCase.Sub_Topic__c == 'Price){
                                    {Req.add(new Request__c(     
                 RecordTypeId = RequestTypeId,
                 Price__c = newCase.Price__c,
                 Case_Number__c = newCase.CaseNumber,
                 Status__c = 'New'));
                         insert Req;}
               

                  }
         }
         }else if (trigger.isUpdate){
                   for (Case newCase2: Trigger.New) {        
                   if (newCase2.Sub_Topic__c == 'Price){
                       {Req.add(new Request__c(     
                         RecordTypeId = RequestTypeId,
                         Price__c = newCase2.Price__c,
                         Case_Number__c = newCase2.CaseNumber,
                         Status__c = 'New'));
                                            
                            insert Req;}
                  }
                }
                }
               
                }

Best Answer chosen by Admin (Salesforce Developers) 
imranrazaimranraza

Hi,

       Change your code with this:-

 trigger CreatePriceRequestFromCase on Case (after Insert, after update){List<Request__c> Req =
new List<request__c>();
 
   String  RequestTypeId = Functions.getRecordTypeId( 'Price', 'Request__c' );

 if (trigger.isInsert){
       for (Case newCase: Trigger.New) {        
         if (newCase.Sub_Topic__c == 'Price){
                                    {Req.add(new Request__c(     
                 RecordTypeId = RequestTypeId,
                 Price__c = newCase.Price__c,
                 Case_Number__c = newCase.CaseNumber,
                 Status__c = 'New'));
                         insert Req;}
               

                  }
         }
         }else if (trigger.isUpdate){

                    Case oldCase;
                   for (Case newCase2: Trigger.New) {        

                        oldCase = Trigger.Oldmap.get(newCase2.Id);
                    if (newCase2.Sub_Topic__c == 'Price' && oldCase.Sub_Topic__c != 'Price'){
                       {Req.add(new Request__c(     
                         RecordTypeId = RequestTypeId,
                         Price__c = newCase2.Price__c,
                         Case_Number__c = newCase2.CaseNumber,
                         Status__c = 'New'));
                                            
                            insert Req;}
                  }
                }
                }
               
                }

All Answers

imranrazaimranraza

Hi,

       Change your code with this:-

 trigger CreatePriceRequestFromCase on Case (after Insert, after update){List<Request__c> Req =
new List<request__c>();
 
   String  RequestTypeId = Functions.getRecordTypeId( 'Price', 'Request__c' );

 if (trigger.isInsert){
       for (Case newCase: Trigger.New) {        
         if (newCase.Sub_Topic__c == 'Price){
                                    {Req.add(new Request__c(     
                 RecordTypeId = RequestTypeId,
                 Price__c = newCase.Price__c,
                 Case_Number__c = newCase.CaseNumber,
                 Status__c = 'New'));
                         insert Req;}
               

                  }
         }
         }else if (trigger.isUpdate){

                    Case oldCase;
                   for (Case newCase2: Trigger.New) {        

                        oldCase = Trigger.Oldmap.get(newCase2.Id);
                    if (newCase2.Sub_Topic__c == 'Price' && oldCase.Sub_Topic__c != 'Price'){
                       {Req.add(new Request__c(     
                         RecordTypeId = RequestTypeId,
                         Price__c = newCase2.Price__c,
                         Case_Number__c = newCase2.CaseNumber,
                         Status__c = 'New'));
                                            
                            insert Req;}
                  }
                }
                }
               
                }

This was selected as the best answer
BKWBKW

Thanks Imran, that worked like a charm!