You need to sign in to do that
Don't have an account?
Read only error....
Trying to write a trigger to help out with our reporting on lead sources...
The idea is to have upon creation of opportunity, if lead source is not defined, look into the contact. If the contact lead source is null, look at the custom Account_Source__c field.
I have the following written, but am getting an error
Apex trigger SetOpSource caused an unexpected exception, contact your administrator: SetOpSource: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.SetOpSource: line 6, column 1
trigger SetOpSource on Opportunity (after insert,after update) { //If Opportunity LeadSource = Null and Primary Contact != null then go to Contact Lead Source for(Opportunity o2:Trigger.new){ if(o2.LeadSource == null && o2.Primary_Contact__r.LeadSource != null){ o2.LeadSource=o2.Primary_Contact__r.LeadSource; }else{ //If Opportunity LeadSource = Null and Account.Account_Source__c != null then go to Contact Lead Source if(o2.LeadSource == null && o2.Account.Account_Source__c != null){ o2.LeadSource=o2.Account.Account_Source__c; } } } }
On a side note, I also cant get the trigger to fire based on if statement... can you not use == null on a picklist value?
Thanks!
I believe that the field is not available for comparison using the trigger.new object (maybe it is I have not tested in trigger. Anon block works when using the object itself). You may have to build a list of related records using soql that have an id in the trigger.new and then use that as the comparison....
I.e.
Set<ID> oppID = New Set<ID>();
for (yourobject o : trigger.new)
oppID.add(o.Opportunity)
Map<ID, Opportunity> opp = [Select Field From Opportunity Where ID IN :oppID];
Then loop
for (yourObject o : trigger.new){
if(opp.containsKey(o.Opportunity){
if(opp.FIELD) == Null{
do stuff
}else{
do stuff
}
}
}
or something like that
All Answers
You cannot edit a record that is in AFTER UPDATE
You should be able to accomplish this in a Before update trigger
As for the picklist, == null should work, throw some misc debug statements like:
if(o2.LeadSource == null)
system.debug('LeadSource IS NULL');
else
system.debug('LeadSource IS NOT NULL and Equals: ' + o2.leadsource);
to see what is going on. A picklist value that has not been set is NULL so == Null works...
I think the problem might actually be referencing data from the Primay_Contact__r relationship...
Even if I set the trigger to if(true) I still cant set the opportunity Lead Source with any data from the Primary Contact (using Primary_Contact__r).
If i change to set just text it works...
I believe that the field is not available for comparison using the trigger.new object (maybe it is I have not tested in trigger. Anon block works when using the object itself). You may have to build a list of related records using soql that have an id in the trigger.new and then use that as the comparison....
I.e.
Set<ID> oppID = New Set<ID>();
for (yourobject o : trigger.new)
oppID.add(o.Opportunity)
Map<ID, Opportunity> opp = [Select Field From Opportunity Where ID IN :oppID];
Then loop
for (yourObject o : trigger.new){
if(opp.containsKey(o.Opportunity){
if(opp.FIELD) == Null{
do stuff
}else{
do stuff
}
}
}
or something like that
Thanks!
Finished Product: