+ Start a Discussion
Bob_poliquinBob_poliquin 

How to check a opportunity owner's records have the same field value

I created a trigger that should display an error message if the owner of a opportunity tries to enter the same number in a field that is already on another record. Ny trigger below is working somewhat. The message shows at the field level when there is another opportunity with the same number, but it shows the error message regardless of who owns the record. I'm not sure how to get the trigger to reconize that the user trying to enter the number is the same as the owner. 

I first tried using "integer" in my set collection, but when i tried to compile I recieved the following error message
Invalid bind expression type of String for column of type Decimal
So I create a formula field (quoteprioritynum__c) and use TEXT to get this to work with Set<string><string> I tried a few things but I wasn't able to save my trigger successfully. 

So I'm stuck with the trigger half working. Any help would be greatly appreciated. 
 
trigger TR_RejectDupPri on Opportunity (before insert,before update) {
//Display error message if another opportunity has the same number in Quting_Priority__c field

 Set<string> oppSet = new Set<string>();

 for(Opportunity o : trigger.new){

     oppSet.add(o.quoteprioritynum__c);
       
 }

 //query all existing record for quoteprioritynum__c
 List<Opportunity> oppsList = [select id, Owner.Id,Quting_Priority__c,LastModifiedBy.Id,quoteprioritynum__c from Opportunity where quoteprioritynum__c in :oppSet];

for(Opportunity o:trigger.new) 
{
    //Update
    if(Trigger.isUpdate && o.quoteprioritynum__c==o.quoteprioritynum__c && oppsList.size()>0 && Trigger.oldmap.get(o.id).quoteprioritynum__c!=o.quoteprioritynum__c && o.LastModifiedBy.Id == o.Owner.Id )
    {
       o.Quting_Priority__c.adderror('Another quote has the same priority number, please enter a new number!');
    }

    //Only check for size
    if(Trigger.isInsert && o.quoteprioritynum__c==o.quoteprioritynum__c && oppsList.size()>0 && o.LastModifiedBy.Id == o.Owner.Id)
    {
       o.Quting_Priority__c.adderror('Another quote has the same priority number, please enter a new number!');

    }

} 
}

 
Best Answer chosen by Bob_poliquin
Andrew GAndrew G
//query all existing record for quoteprioritynum__c
 List<Opportunity> oppsList = [SELECT id, Owner.Id, Quting_Priority__c, LastModifiedBy.Id, quoteprioritynum__c 
      FROM Opportunity 
      WHERE quoteprioritynum__c in :oppSet AND Owner.Id = :userinfo.getuserid()];
You could modify you query to include using the Id on the current user.
You could then simply test for a results size   
//if we have returned results, then both priority number and user Id match 
if (oppsList .size() > 0 )  { 
  //adderror
}
However, looking at the structure and logic, I would question if this is a truly bulk trigger.  If you are to ever bulk upload (say using dataloader), then the structure and logic will give incorrect results.  
1.  Any bulk upload would only retrieve oppty where the loader is the owner.
2.  Any bulk upload would include multiple priority numbers and therefore (potentially) return a large number of Oppty records which would then cause the error to fire even if not relevant and stop all updates.

It may not be relevant if you restrict bulk load tools to System Admins and they would never "own" an oppty.

Regards
Andrew

 

All Answers

Andrew GAndrew G
//query all existing record for quoteprioritynum__c
 List<Opportunity> oppsList = [SELECT id, Owner.Id, Quting_Priority__c, LastModifiedBy.Id, quoteprioritynum__c 
      FROM Opportunity 
      WHERE quoteprioritynum__c in :oppSet AND Owner.Id = :userinfo.getuserid()];
You could modify you query to include using the Id on the current user.
You could then simply test for a results size   
//if we have returned results, then both priority number and user Id match 
if (oppsList .size() > 0 )  { 
  //adderror
}
However, looking at the structure and logic, I would question if this is a truly bulk trigger.  If you are to ever bulk upload (say using dataloader), then the structure and logic will give incorrect results.  
1.  Any bulk upload would only retrieve oppty where the loader is the owner.
2.  Any bulk upload would include multiple priority numbers and therefore (potentially) return a large number of Oppty records which would then cause the error to fire even if not relevant and stop all updates.

It may not be relevant if you restrict bulk load tools to System Admins and they would never "own" an oppty.

Regards
Andrew

 
This was selected as the best answer
Bob_poliquinBob_poliquin
Hi Andrew,
Thank you so much for helping me.  The updated code you provided you code you provided worked! Thank you so much for helping