• StaceyW
  • NEWBIE
  • 15 Points
  • Member since 2013
  • Technical Architect Consultant
  • Accenture


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 8
    Replies
I am creating a trigger which creates a Target_Sale__c which links to a Target__c. Each User has a Target per month and the Sales represents the Won Opportunity which rolls up to the target. The problem is that I need to take OpportunitySplits into account and create  Target Sale for each Opportunity Split.

I wrote the below trigger and am not getting any errors in the Developer Console but it is not saving in the browser - Here is my trigger:

trigger LinkOpportunitytoUserTarget on Opportunity (before insert, after update, after delete) {
   
    SET<String> oppyear = new SET<String>();
    SET<String> oppmonth = new SET<String>();
   
    SET<ID> oids = new SET<ID>();
    SET<ID> osids = new SET<ID>();
    SET<ID> userids = new SET<ID>();
   
    LIST<Target_Sale__c> targetsalelistdelete = new LIST<Target_Sale__c>();
    LIST<Target_Sale__c> targetsalelistinsert = new LIST<Target_Sale__c>();
    MAP<String,Target__c> targetmap = new MAP<String,Target__c>();
       
    String omaplookup;
    String osmaplookup;
   
    if(Trigger.IsInsert || Trigger.IsUpdate){
        for(Opportunity o : Trigger.new){
            if(o.Probability==100){
                oppyear.add(string.Valueof(o.CloseDate.Year()));
                oids.add(o.Id);
                oppmonth.add(string.ValueOf(o.CloseDate.Month()));
            }
        }
       
        system.debug('oppyear: '+oppyear);
        system.debug('oids: '+oids);
        system.debug('oppmonth: '+oppmonth);
    
     //Query Opportunity Splits linked to Opportunity and assign user ids to set
    for(OpportunitySplit getos : [SELECT Id, OpportunityId, SplitAmount, SplitOwnerId
                                FROM OpportunitySplit
                                WHERE OpportunityId IN :oids]){
                                    if(Opportunity.Probability=100){
                                        osids.add(getos.Id);
                                        userids.add(getos.SplitOwnerId);
                                  }
                                }
       
      system.debug('userids: '+userids);
      system.debug('osids: '+osids);
     
    //Query Target Sales already linked to Opportunity for delete
    for(Target_Sale__c getts : [SELECT Id, Name, Amount__c, Opportunity_Reference__c, Opportunity_Split_Reference__c, Opportunity_Name__c FROM Target_Sale__c WHERE Opportunity_Split_Reference__c IN :osids]){
        targetsalelistdelete.add(getts);   
    }
       
        system.debug('targetsalelistdelete: '+targetsalelistdelete);
   
        if(!targetsalelistdelete.isEmpty()){
            delete targetsalelistdelete;
        }
     
    //Query Target records for users for Month and Date of Opportunity
    for(Target__c gett : [SELECT Id, Name, Target_Amount__c, Actual_Sales__c, OwnerId, Sales_Rep__r.Id, Year__c, Month__c
                        FROM Target__c
                        WHERE Sales_Rep__r.Id IN :userids
                        AND Year__c IN :oppyear
                        AND Month__c IN :oppmonth]){
                        //add Targets to Target Map
                        targetmap.put(gett.Sales_Rep__r.Id + '_' + gett.Year__c + '_' + gett.Month__c,gett);                     
                        }
       
    system.debug('targetmap: '+targetmap);

    for(OpportunitySplit oppsplits : [SELECT Id, OpportunityId, SplitAmount, SplitOwnerId
                                FROM OpportunitySplit
                                WHERE Id IN :osids]){
            osmaplookup = oppsplits.SplitOwnerId + '_' + string.Valueof(oppsplits.Opportunity.CloseDate.Year()) + '_' + string.Valueof(oppsplits.Opportunity.CloseDate.Month()); 
            system.debug('osmaplookup: '+osmaplookup);
                            if(targetmap.get(osmaplookup) != null){
                                            targetsalelistinsert.add(new Target_Sale__c(Target__c = targetmap.get(osmaplookup).Id,
                                                                    Opportunity_Reference__c = oppsplits.OpportunityId,
                                                                    Opportunity_Split_Reference__c = oppsplits.Id,
                                                                    Amount__c = oppsplits.SplitAmount,
                                                                    Opportunity_Name__c = oppsplits.OpportunityId.Name)); 
                                            }
        }
            
        system.debug('targetsalelistinsert: '+targetsalelistinsert);
        if(!targetsalelistinsert.isEmpty()){
            insert targetsalelistinsert;
        }
    }
   
    if(Trigger.IsDelete){
        for(Opportunity o : Trigger.old){
            if(o.Probability=100){
                oids.add(o.Id);
            }
        }
       
        system.debug('oids: '+oids);
       
        for(Target_Sale__c getts : [SELECT Id, Name, Amount__c, Opportunity_Reference__c, Opportunity_Split_Reference__c, Opportunity_Name__c FROM Target_Sale__c WHERE Opportunity_Reference__c IN :oids]){
            targetsalelistdelete.add(getts);   
        }
       
        system.debug('targetsalelistdelete: '+targetsalelistdelete);
        if(!targetsalelistdelete.isEmpty()){
            delete targetsalelistdelete;
        }
    }
}

Any help would be greatly appreciated as I just can't figure out what the error is refering to!
Hi All,

This is driving me a little crazy! I have written a simple trigger which creates or updates the PriceBookEntry in the Standard Pricebook when the Product is inserted or updated. It works when doing it manually but as soon as I upsert using the Data Loader or Jitterbit, it only fires on the first record.

I can't seem to figure out why and was hoping someone may be able to take a look at the code and identify the issue.

Any help would be very much appreciated :)

Code:

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

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

for (Product2 p : Trigger.new) {ids.add(p.Id);}
   
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 AND Pricebook2ID = '01s20000000FVFV' AND IsActive = TRUE];
  
if(trigger.isupdate){
    for (Product2 p : Trigger.new) {
PriceBookEntry.UnitPrice=p.Item_Cost__c;
       }
Update pbe;
}
}

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; }
}
}
}

 

 

 

Hi all,

 

I am trying to create a trigger that will insert and/or update a PricebookEntry when the Product is inserted or updated. I have limited Apex knowledge and have put together the part that inserts the Pricebook Entry on Insert of the Product and that seems to be working correctly, however I cannot seem to get the update side of things working. 

 

Here is my code so far:

 

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

sObject s = [select ID from Pricebook2 where IsStandard = TRUE];

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 FROM PricebookEntry WHERE Product2ID =:trigger.new[0].id];

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

 

The error message I am getting is : 

Error: Compile Error: Expression cannot be assigned at line -1 column -1

 

I'm hoping someone has come accross this before and has some idea on how I can make it work.

 

Any advice would be greatly appreciated!

 

Thank you!!

 

Hi All,

 

I am a complete novice to the development side, especially Apex and am trying to create a trigger that will 'roll-up' or sum a field from a child record and I can't use standard roll ups due to limits. I have found a lot of code that can help with normal roll ups but the tricky part of what I am trying to do is that the child records of a specific 'type' can only be counted once and there is also another requirement that a field on the child record can't be empty. And that field is only populated, by other code, after the record is inserted. 

 

So basically it's a child object linked to Accounts. When a child is updated, the trigger will need to check if a child record with a specific type already exists for that account. The 'type' field on the child object  is a picklist field. If it does NOT already exist then the field on accoints called points will need to be increased by 10.  If it does already exist then nothing will happen and the record will just be updated as normal. Also, there will need to be an after delete so that when a child record is removed and it is the only one of that type, 10 points will need to be removed from the field on accounts. 

 

I am probably over complicating it but does anyone out there have a similar code I could take a look at that would give me a good starting point?

 

I am creating a trigger which creates a Target_Sale__c which links to a Target__c. Each User has a Target per month and the Sales represents the Won Opportunity which rolls up to the target. The problem is that I need to take OpportunitySplits into account and create  Target Sale for each Opportunity Split.

I wrote the below trigger and am not getting any errors in the Developer Console but it is not saving in the browser - Here is my trigger:

trigger LinkOpportunitytoUserTarget on Opportunity (before insert, after update, after delete) {
   
    SET<String> oppyear = new SET<String>();
    SET<String> oppmonth = new SET<String>();
   
    SET<ID> oids = new SET<ID>();
    SET<ID> osids = new SET<ID>();
    SET<ID> userids = new SET<ID>();
   
    LIST<Target_Sale__c> targetsalelistdelete = new LIST<Target_Sale__c>();
    LIST<Target_Sale__c> targetsalelistinsert = new LIST<Target_Sale__c>();
    MAP<String,Target__c> targetmap = new MAP<String,Target__c>();
       
    String omaplookup;
    String osmaplookup;
   
    if(Trigger.IsInsert || Trigger.IsUpdate){
        for(Opportunity o : Trigger.new){
            if(o.Probability==100){
                oppyear.add(string.Valueof(o.CloseDate.Year()));
                oids.add(o.Id);
                oppmonth.add(string.ValueOf(o.CloseDate.Month()));
            }
        }
       
        system.debug('oppyear: '+oppyear);
        system.debug('oids: '+oids);
        system.debug('oppmonth: '+oppmonth);
    
     //Query Opportunity Splits linked to Opportunity and assign user ids to set
    for(OpportunitySplit getos : [SELECT Id, OpportunityId, SplitAmount, SplitOwnerId
                                FROM OpportunitySplit
                                WHERE OpportunityId IN :oids]){
                                    if(Opportunity.Probability=100){
                                        osids.add(getos.Id);
                                        userids.add(getos.SplitOwnerId);
                                  }
                                }
       
      system.debug('userids: '+userids);
      system.debug('osids: '+osids);
     
    //Query Target Sales already linked to Opportunity for delete
    for(Target_Sale__c getts : [SELECT Id, Name, Amount__c, Opportunity_Reference__c, Opportunity_Split_Reference__c, Opportunity_Name__c FROM Target_Sale__c WHERE Opportunity_Split_Reference__c IN :osids]){
        targetsalelistdelete.add(getts);   
    }
       
        system.debug('targetsalelistdelete: '+targetsalelistdelete);
   
        if(!targetsalelistdelete.isEmpty()){
            delete targetsalelistdelete;
        }
     
    //Query Target records for users for Month and Date of Opportunity
    for(Target__c gett : [SELECT Id, Name, Target_Amount__c, Actual_Sales__c, OwnerId, Sales_Rep__r.Id, Year__c, Month__c
                        FROM Target__c
                        WHERE Sales_Rep__r.Id IN :userids
                        AND Year__c IN :oppyear
                        AND Month__c IN :oppmonth]){
                        //add Targets to Target Map
                        targetmap.put(gett.Sales_Rep__r.Id + '_' + gett.Year__c + '_' + gett.Month__c,gett);                     
                        }
       
    system.debug('targetmap: '+targetmap);

    for(OpportunitySplit oppsplits : [SELECT Id, OpportunityId, SplitAmount, SplitOwnerId
                                FROM OpportunitySplit
                                WHERE Id IN :osids]){
            osmaplookup = oppsplits.SplitOwnerId + '_' + string.Valueof(oppsplits.Opportunity.CloseDate.Year()) + '_' + string.Valueof(oppsplits.Opportunity.CloseDate.Month()); 
            system.debug('osmaplookup: '+osmaplookup);
                            if(targetmap.get(osmaplookup) != null){
                                            targetsalelistinsert.add(new Target_Sale__c(Target__c = targetmap.get(osmaplookup).Id,
                                                                    Opportunity_Reference__c = oppsplits.OpportunityId,
                                                                    Opportunity_Split_Reference__c = oppsplits.Id,
                                                                    Amount__c = oppsplits.SplitAmount,
                                                                    Opportunity_Name__c = oppsplits.OpportunityId.Name)); 
                                            }
        }
            
        system.debug('targetsalelistinsert: '+targetsalelistinsert);
        if(!targetsalelistinsert.isEmpty()){
            insert targetsalelistinsert;
        }
    }
   
    if(Trigger.IsDelete){
        for(Opportunity o : Trigger.old){
            if(o.Probability=100){
                oids.add(o.Id);
            }
        }
       
        system.debug('oids: '+oids);
       
        for(Target_Sale__c getts : [SELECT Id, Name, Amount__c, Opportunity_Reference__c, Opportunity_Split_Reference__c, Opportunity_Name__c FROM Target_Sale__c WHERE Opportunity_Reference__c IN :oids]){
            targetsalelistdelete.add(getts);   
        }
       
        system.debug('targetsalelistdelete: '+targetsalelistdelete);
        if(!targetsalelistdelete.isEmpty()){
            delete targetsalelistdelete;
        }
    }
}

Any help would be greatly appreciated as I just can't figure out what the error is refering to!
Hi All,

This is driving me a little crazy! I have written a simple trigger which creates or updates the PriceBookEntry in the Standard Pricebook when the Product is inserted or updated. It works when doing it manually but as soon as I upsert using the Data Loader or Jitterbit, it only fires on the first record.

I can't seem to figure out why and was hoping someone may be able to take a look at the code and identify the issue.

Any help would be very much appreciated :)

Code:

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

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

for (Product2 p : Trigger.new) {ids.add(p.Id);}
   
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 AND Pricebook2ID = '01s20000000FVFV' AND IsActive = TRUE];
  
if(trigger.isupdate){
    for (Product2 p : Trigger.new) {
PriceBookEntry.UnitPrice=p.Item_Cost__c;
       }
Update pbe;
}
}

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; }
}
}
}

 

 

 

Hi all,

 

I am trying to create a trigger that will insert and/or update a PricebookEntry when the Product is inserted or updated. I have limited Apex knowledge and have put together the part that inserts the Pricebook Entry on Insert of the Product and that seems to be working correctly, however I cannot seem to get the update side of things working. 

 

Here is my code so far:

 

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

sObject s = [select ID from Pricebook2 where IsStandard = TRUE];

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 FROM PricebookEntry WHERE Product2ID =:trigger.new[0].id];

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

 

The error message I am getting is : 

Error: Compile Error: Expression cannot be assigned at line -1 column -1

 

I'm hoping someone has come accross this before and has some idea on how I can make it work.

 

Any advice would be greatly appreciated!

 

Thank you!!

 

Hi All,

 

I am a complete novice to the development side, especially Apex and am trying to create a trigger that will 'roll-up' or sum a field from a child record and I can't use standard roll ups due to limits. I have found a lot of code that can help with normal roll ups but the tricky part of what I am trying to do is that the child records of a specific 'type' can only be counted once and there is also another requirement that a field on the child record can't be empty. And that field is only populated, by other code, after the record is inserted. 

 

So basically it's a child object linked to Accounts. When a child is updated, the trigger will need to check if a child record with a specific type already exists for that account. The 'type' field on the child object  is a picklist field. If it does NOT already exist then the field on accoints called points will need to be increased by 10.  If it does already exist then nothing will happen and the record will just be updated as normal. Also, there will need to be an after delete so that when a child record is removed and it is the only one of that type, 10 points will need to be removed from the field on accounts. 

 

I am probably over complicating it but does anyone out there have a similar code I could take a look at that would give me a good starting point?

 

Hi

 

I tried to make the visualforce page renderas PDF in landscape with following styles

 

<style >
    @page {
        size:landscape;
   }
 </style>
 

 

But it is not working now. Is anybody face the same issue? or have any idea to render the PDF as landscape??

 

Thanks!

  • August 18, 2009
  • Like
  • 0