+ Start a Discussion
Brandon NelsonBrandon Nelson 

Returning picklist vale from another object using SOQL and APEX

I have two object
1. Contacts__c
2. Sales_VP_By_Territory__c

On the Contacts__c object, I have two fields
1. Sales VP (Picklist / read only)
2. Territory (Picklist)

On the Sales_VP_By_Territory__c, I have two fields. This object is hidden from users
1. Sales VP
2. Territory

On the Contacts__c object, I would like the user to select the Territory. Once they select one and save the record, a Trigger will fire and store the current record Territory in a variable and then execute a SOQL query on the Sales_VP_By_Territory object and get the Sales_VP__c field value and store it in a variable. Then it will go back and populate the Contact__c records field Sales_VP__c.

Here is my code, but the issue is, it keeps telling me 
"GetSalesVP: data changed by trigger for field Sales VP: bad value for restricted picklist field: (Sales_VP_By_Territory__c:{Sales_VP__c=Brandon Nelson, Id=a0z3a000007e8LSAAY})"

If you notice in my SOQL query, I'm not pulling ID so why is it trying to insert it?
 
trigger GetSalesVP on Contact__c (before insert, before update) {
   	
    String territory;
    String getSalesVP;
    
    for(Contact__c c : Trigger.new) {
        territory = c.Territory__c;
        
	List<Sales_VP_By_Territory__c> getSalesVP = [SELECT Sales_VP__c FROM Sales_VP_By_Territory__c WHERE territory__c = :territory]; 
          
    c.Sales_VP__c = String.valueOf(getSalesVP);
        
    }  
}

 
Shaijan ThomasShaijan Thomas
Hi Brandon Nelson
  
     Your trigger name and the variable name (line 9) is same, could you change the the variable name and try it

Regards
Shaijan Thomas
Brandon NelsonBrandon Nelson
Same result after I change the variable name. 
GetSalesVP: data changed by trigger for field Sales VP: bad value for restricted picklist field: (Sales_VP_By_Territory__c:{Sales_VP__c=Brandon Nelson, Id=a0z3a000007e8LSAAY})
I dont know why it is grabbing the ID and trying to throw it in the picklist when I am not pulling the ID from the SOQL query..... 
Prangya JenaPrangya Jena
Hi Brandon,

1.Can you check once that 'Sales_VP_By_Territory__c' field is a dependent field or any field dependent on this field. 
2. On top of that do you have any other validation or any rule or trigger on that field.

Basically is there any operation realted to that field is there?

Regards,
Prangya