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
StaceyWStaceyW 

Expression cannot be assigned at line -1 column -1

I'm quite new to triggers and I can't seem to figure out what this error is refering to. 

 

Expression cannot be assigned at line -1 column -1

 

Here is my code:

 

trigger UpsertPBE on Product2 (after insert, after update) {

sObject s = [select ID from Pricebook2 where IsStandard = TRUE];
Set<ID> ids = Trigger.newMap.keySet();
list<PricebookEntry> entries=new list<PricebookEntry>();

if(trigger.isinsert){
for (Product2 p : Trigger.new) {
entries.add(
new PricebookEntry(
Pricebook2Id=s.ID,
Product2Id=p.ID,
UnitPrice=p.Item_Cost__c,
IsActive=p.IsActive,
UseStandardPrice=FALSE)
);
}
insert entries;
}

list<PricebookEntry> pbe = [SELECT Id, UnitPrice FROM PricebookEntry WHERE Product2ID in :ids];

if(trigger.isupdate){
for (Product2 p : Trigger.new) {
if (pbe != null) {
PricebookEntry.UnitPrice=p.Item_Cost__c; }
}
}
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jiah.choudharyjiah.choudhary

@Weeball,

 

Found the issue. The line where you have 'PricebookEntry.UnitPrice=p.Item_Cost__c;'.. Please create and instance for PriceBookEntry and then use its unitprice. It lets you save.

All Answers

jiah.choudharyjiah.choudhary

@Weeball,

 

Are you unable to save this trigger? since I tried saving the same code in my org and it did save (Except for the custom field that you included).

StaceyWStaceyW

Thanks for the reply!

 

No it won't let me save. Just gives the error message - I wonder why yours lets you save but mine doesn't?

 

In the Developer Console however, I get an error message saying that the Product object isn't supported in verson 29.0. Which is very strange considering that if I remove the 'IsUpdate' code and only have it run on Insert, it works perfectly. But I need to have the PricebookEntry update when the Product is updated as well.

 

 

jiah.choudharyjiah.choudhary

@Weeball,

 

Found the issue. The line where you have 'PricebookEntry.UnitPrice=p.Item_Cost__c;'.. Please create and instance for PriceBookEntry and then use its unitprice. It lets you save.

This was selected as the best answer
StaceyWStaceyW

Thank you so much! :) That solved the problem and it is working great!

 

Here is my final code in case anyone needs something similar in the future:

 

trigger CreatePBE on Product2 (after insert, after update) {

sObject s = [select ID from Pricebook2 where IsStandard = TRUE];
Set<ID> ids = Trigger.newMap.keySet();
list<PricebookEntry> entries=new list<PricebookEntry>();

if(trigger.isinsert){
    for (Product2 p : Trigger.new) {
    entries.add(
    new PricebookEntry(
    Pricebook2Id=s.ID,
    Product2Id=p.ID, 
    UnitPrice=p.Item_Cost__c, 
    IsActive=p.IsActive, 
    UseStandardPrice=FALSE)
    );
    }
    insert entries;
    }
    
PriceBookEntry pbe = [select ID, UnitPrice FROM PricebookEntry WHERE Product2ID IN :ids AND Pricebook2ID = '01s20000000FVFV' AND IsActive = TRUE LIMIT 1];

if(trigger.isupdate){ 
  for (Product2 p : Trigger.new) {
   pbe.UnitPrice=p.Item_Cost__c;
   }    
Update pbe; 
}     
}
jiah.choudharyjiah.choudhary

Hit kudos if it solved your query