+ Start a Discussion
davidesidavidesi 

Issue changing owner and trigger

Hi All,
I have a trigger onUpdate & onBefore for Opportunities.
When it's triggered, it reads a opportunity field and updates the owner of the opportunity.

It works fine with any change I do on the opportunity, except if I change the owner.

In the debug log I can see that the trigger is fired, and it change the owner, but the opportunity finally doesn't have the owner assigned in the trigger but the owner that I have selected with the lookup.

Any help?

Thanks in advance
Vishant ShahVishant Shah
Hi

Can you post your workflow and trigger to look at

Ta
Vish

davidesidavidesi
Hi, 
The trigger is handled by following triggerhandler class

public without sharing class ProjectTriggerHandler {

  /*
This method receives a Project and a Zip Code, and updates the Project with ZC address*/
private void updateAddress(Opportunity p, Zip_Code__c zc){
  p.Zip_Code__c=zc.Id;
  if(zc.PK__c!=null){
  p.Postal_Code__c=zc.PK__c;
  }

}
/*
This method receives a project and a ZipCode
It assigns the project owner to a person CMS or AM
It reads the value of the assigned new owner from the formula field Assignation_rule__c , and assign as the owner of the project
*/
private void assignOwner(Opportunity p){
  System.debug('Asigna la oportunidad ' + p.name + ' al usuario ' + p.Owner_Assignment_Field__c);
  p.OwnerId=(Id)p.Owner_Assignment_Field__c;
}

public User quienEs(Id idusuario){

User u = [select firstname,lastname from User where id=:idusuario];
return u;

}


public void beforeInsertUpdate(Opportunity[] projects){
 
     Set<String> postalCodes = new Set<String>();
     Set<string>projectReferences = new Set<String>();
for (Opportunity p : projects){
 
  
if(p.Postal_Code__c!=null ){
          postalCodes.add(p.Postal_Code__c);
}else if (p.Zip_Code__c!=null){
       projectReferences.add(p.zip_code__c);
}
         
          if (p.Project_Area__c==null){
          p.addError(Label.Project_Area_Mandatory);
          } 

}

  if ((postalCodes!=null && postalCodes.size()>0) || (projectReferences!=null && projectReferences.size()>0)){
  
   //invoke method for obtain its corresponding ZipCodes
        Map<String,Zip_Code__c> zipcodes=ZipCodeSeeker.getZipCode(postalCodes);
        Map<String,Zip_Code__c> zipCodesRef=null;
        if (projectReferences!=null && projectReferences.size()>0){
        zipCodesRef=ZipCodeSeeker.getZipCodeReference(projectReferences);
        }
       for (Opportunity p: projects){
       
      
          //looking for if the postal code is contained at the map received
     
        
         if(P.Postal_Code__c!=null && zipcodes!=null && !zipcodes.containsKey(p.Postal_Code__c) && zipcodesRef!=null &&  p.Zip_Code__c!=null && !zipCodesRef.containsKey(p.zip_code__c)){
             //if not, add an error to that project
         //    p.addError(Label.Error_Zip_Code  +zipcodes.containsKey(p.Postal_Code__c) + ' zipCodesRef=' + zipCodesRef.containsKey(p.zip_code__c) + ' Oportunidad='+ p + ' ZipcodesRef=' + zipCodesRef );
             p.addError(Label.Error_Zip_Code  +zipcodes.containsKey(p.Postal_Code__c));
           
         }else{
          //update project with zip code data
          if (zipcodes!=null && p.Postal_Code__c!=null && zipcodes.get(p.Postal_code__c)!=null){
           updateAddress(p,zipcodes.get(p.postal_code__c));
          }else if (zipCodesRef!=null && zipCodesRef.get(p.zip_code__c)!=null){
           updateAddress(p,zipCodesRef.get(p.zip_code__c));
          
         
          }
        
   
          }
         
    //if we are converting a lead and we receive Zip_Code, then we assign its postal code
        if (p.Zip_Code__r!=null && p.Zip_Code__r.pk__c!=null){
        p.Postal_Code__c=p.Zip_Code__r.pk__c;
    
        }
       
      if (!p.Assign_Project_Manually__c && p.Owner_Assignment_Field__c!=null){
       assignOwner(p); 
        }
         }
  }

}

}