+ Start a Discussion
Zen Newman 1Zen 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! 
 
Best Answer chosen by Zen Newman 1
Deepali KulshresthaDeepali Kulshrestha
Hi Zen,

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

gowtham murugesangowtham murugesan
HI Zen Newman 1,
 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;
            }
          
        }
        
    }
}
Deepali KulshresthaDeepali Kulshrestha
Hi Zen,

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
This was selected as the best answer
Zen Newman 1Zen Newman 1
Hi Deepali, thanks for this, it worked great!. One follow up question for you. I was trying to make one modification to this so that instead of trying to match exact dates between Performance_Start__c and Client_Services_Launch_Date__c, it'll match just the year. I tried to apply the year method to each date: 
if(s.Performance_Start__c.year() == op.Client_Services_Launch_Date__c.year()){
                op.Sales_Performance__c=s.Id;
            }

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!  
Ajay K DubediAjay K Dubedi
Hi Zen,

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-
trigger SalesPerformance on Opportunity (before insert, before update) {

       List<opportunity> OppList = [select ownerId,Client_Services_Launch_Date__c from opportunity where Id in         :trigger.new]; 

        List <Sales_Performance__c > SpcList = [select                   Sales_Performance__c.OwnerId,id,Performance_Start__c From                  Sales_Performance__c where Sales_Performance__c.OwnerId IN :OppList]; 

    for(Opportunity opp : OppList){
        for(Sales_Performance__c spc : SpcList){
            if(spc.Performance_Start__c == opp.Client_Services_Launch_Date__c){
                 opp.Sales_Performance__c = spc.id;
            }
          
        }
        
    }
}
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 )