You need to sign in to do that
Don't have an account?
Zen Newman 1
Lookup field update with trigger
Hello- I'm trying to create a trigger that uses a SOQL query to find a record on a custom object matching criteria and then populate a lookup on the Opportunity with that record. My code is below, but I keep running into an error: "Illegal assignment from Schema.SObjectField to ID" on line 4.
trigger SalesPerformance on Opportunity (before insert, before update) { for (Opportunity o : [SELECT Sales_Performance__c.OwnerId FROM Sales_Performance__c WHERE Sales_Performance__c.OwnerId IN (SELECT Opportunity.OwnerId FROM Opportunity)]) { if (Sales_Performance__c.Performance_Start__c == Opportunity.Client_Services_Launch_Date__c) { o.Sales_Performance__c = Sales_Performance__c.Id; } } }Thank you!
I gone through your problem, refer through these code.
trigger SalesPerformance on Opportunity (before insert,before update) {
List<Sales_Performance__c> lsales=[select OwnerId,Performance_Start__c from Sales_Performance__c ];
for(Opportunity op:Trigger.new){
for(Sales_Performance__c s:lsales){
if (s.Performance_Start__c == op.Client_Services_Launch_Date__c) {
op.Sales_Performance__c = s.Id;
}
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
All Answers
For Insert operation you can try this below code and modify based on Your need:
trigger SalesPerformance on Opportunity (before insert, before update) {
List <Sales_Performance__c > li = [select id,Performance_Start__c From Sales_Performance__c ];
for(Opportunity o : trigger.new){
for(Sales_Performance__c tec : li){
if(tec.Performance_Start__c == o.Client_Services_Launch_Date__c){
o.Sales_Performance__c = tec.id;
}
}
}
}
I gone through your problem, refer through these code.
trigger SalesPerformance on Opportunity (before insert,before update) {
List<Sales_Performance__c> lsales=[select OwnerId,Performance_Start__c from Sales_Performance__c ];
for(Opportunity op:Trigger.new){
for(Sales_Performance__c s:lsales){
if (s.Performance_Start__c == op.Client_Services_Launch_Date__c) {
op.Sales_Performance__c = s.Id;
}
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
but it gives me this error, "SalesPerformance: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object". Can you tell me where I'm going wrong?
Thanks!
I have gone through your problem and did some corrections in code. The reason behind your error is that if you apply for each loop for opportunity sObject then in you must be query at same sObject type means opportunity but you have made it on different sObject type - Sales_Performance__c which is not correct.
Here is the correct code, just replace all of your code with it- I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com (http://www.ajaydubedi.com )