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
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12 

Send Email Notification Error

Hi,

I tried the send Email notification for some one, when the pricebookentry price changed include standard price also. But i got error

Please refer the following code. what my mistake
trigger
=================
trigger PricebookEntry on Pricebook2(before insert, before update){
PricebookTriggerHandler handler = new PricebookTriggerHandler();

     if(trigger.isBefore && trigger.IsInsert){
       handler.OnBeforeInsert(trigger.newmap, Trigger.OldMap);
    } 
      
    if(trigger.isBefore && trigger.IsUpdate){
      handler.OnBeforeUpdate(trigger.newmap, Trigger.OldMap);
    } 
    
}

Trigger Controller
========================
Public class PricebookTriggerHandler{

    Public void OnBeforeInsert(map<id,Pricebook2> PrdNewMap,map<id,Pricebook2> PrdOldMap){
    
    }
    Public void OnBeforeUpdate(map<id,Pricebook2> PrdNewMap,map<id,Pricebook2> PrdOldMap){
        EmailNotification(PrdNewMap,PrdOldMap);
    }

   Public void EmailNotification(map<id,Pricebook2> PrdNewMap,map<id,Pricebook2> PrdOldMap){
    Set<Id> PriceId = new Set<Id>();
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    
    for(pricebook2 price:PrdNewMap.values()){
        
     pricebookEntry Stdprice = [SELECT Id, Unitprice, UseStandardPrice FROM PricebookEntry WHERE Id = :priceId];

      Messaging.reserveSingleEmailCapacity(1);
       
       Messaging.SingleEmailMessage PriceNotificationmail = new Messaging.SingleEmailMessage(); 
     //  PriceNotificationmail.setToAddresses('sundhar.mks@gmail.com');
       PriceNotificationmail.setReplyTo('Sundhar.mks@gmail.com');
       PriceNotificationmail.setSenderDisplayName('Salesforce Support'); 
       
       
       String oldUnitprice = PrdOldMap.get(price.id).Unitprice;

        if (price.Unitprice != oldUnitprice) {
            PriceNotificationmail.setSubject(' Pricebook price updation : ' + 'Changed to ' + price.Unitprice + '. Pricebook Id:' + price.Id);
           // CaseNotificationmail.setPlainTextBody('Your pricebook : ' + ' has been updated.'+'To view your pricebook <a href=<a target="_blank" href="https://cs16.salesforce.com/'+price.Id'">https://cs16.salesforce.com/'+price.Id);</a>
  
        } 
     // Messaging.sendEmail(new Messaging.SingleEmailMessage[] { PriceNotificationmail});
      mails.add(PriceNotificationmail);
      }
     Messaging.sendEmail(mails);
    }

}
Himanshu ParasharHimanshu Parashar
Hi Sundar,

What is the error message you are getting.

Thanks,
Himanshu
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12
Hi Himanshu,

 String oldUnitprice = PrdOldMap.get(price.id).Unitprice; --> this line i got the below error  

Error: Compile Error: Invalid field Unitprice for SObject Pricebook2 at line 24 column 54
Himanshu ParasharHimanshu Parashar
Hi Sundar,

It is because UnitPrice is field of PricebookEntry object not the Pricebook2 object and at line 24 you have map of Pricebook2. you are already have data in Stdprice object at line 18 so you should do 
 
String oldUnitprice = String.valueof(Stdprice.Unitprice);


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.


 
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12
Hi Himanshu,

Your right i have corrected, i got another one error from highlighted line


Integer OldUnitprice = Integer.valueof(Stdprice.Unitprice);   
         if (Stdprice.Unitprice != OldUnitprice) {
            PriceNotificationmail.setSubject(' Pricebook price updation : ' + 'Changed to ' + Stdprice.Unitprice + '. Pricebook Id:' + Stdprice.Id);
            PriceNotificationmail.setPlainTextBody('Your pricebook : ' + ' has been updated.'+'To view your pricebook <a href=<a target="_blank" href="https://cs16.salesforce.com/'+price.Id'">https://cs16.salesforce.com/'+price.Id');</a>   -->Error: Compile Error: line breaks not allowed in string literals at line 26 column -1
            
        } 
Himanshu ParasharHimanshu Parashar
This error is because wrong string concatenation you are missing + after  price.Id, Please make sure you are joining string in correct way.