function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Conor Bradley 5Conor Bradley 5 

My code doesn't work on reports

Hi All

I have the following code to take an opportunity contact role and add it to a field on the opportunity called Opprtunity_Contact__c. The code works fine however it throws up some bad results when i add the opportunity_contact__c field to reports. Does anyone know how this can happen?

Thanks
Conor
trigger MnCopyPrimaryContact on Opportunity (before update) {
    Set<Id> setOfIds = new Set<Id>();
    for (Opportunity o : Trigger.new) {
        if(o.Id!=Null){
            setOfIds.add(o.Id);
        }
    }
    OpportunityContactRole[] contactRoleArray= [select ContactID, isPrimary from OpportunityContactRole where OpportunityId IN :setOfIds  order by isPrimary desc,CreatedDate];
    for (Opportunity o : Trigger.new) {
        if (contactRoleArray.size() > 0) {
            o.Opportunity_contact__c = contactRoleArray[0].ContactID;           
        }else{
            o.Opportunity_contact__c = null;
        }
    }
}