• Conor Bradley 5
  • NEWBIE
  • 45 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 9
    Replies
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;
        }
    }
}

 
Hi 
Could someone help me write a apex unit test for the following trigger. I am new to apex.
trigger acceptedspeakerroles on CampaignMember (after insert, after update, after delete, after undelete) {
Map<Id,Contact> contacts = new Map<Id,Contact>();
  if(Trigger.new<>null)
    for(CampaignMember c:Trigger.new)
      if(c.ContactId<>null)
        contacts.put(c.ContactId,new Contact(id=c.ContactId));
  if(Trigger.old<>null)
    for(CampaignMember c:Trigger.old)
      if(c.ContactId<>null)
        contacts.put(c.ContactId,new Contact(id=c.ContactId));
  update contacts.values();
}


Many Thanks
Conor

Hi

I have an apex trigger that takes a contact from the contact roles on opportunity, and adds it to a look up field on the opportunity called opportunity_contact__c.

I also need this to then go to another lookup field on the oportunity line items, also called opportunity_contact__c. Can anyone help me with this? Trigger below:
 
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;
        }
    }
}

Many Thanks
Conor​
Hi, we are a publishing company that sell adverts into different magazine 'Issues'(custom SF Object)

I have an apex trigger called EndOfSeriesIssue that takes the latest Issue from opportunityLineItem and puts it to a look up field on opportunity called last_issue__c, based on Issue_publication_date__c

This works fine. However there is a problem when another product is added to the opportunity, which does not contain an issue. Could anyone tell me how to modify my trigger to deal with this problem? Trigger below:
trigger EndOfSeriesIssue on Opportunity (before update) {
    if(trigger.isBefore && trigger.isUpdate) {
        List<OpportunityLineItem> oliList = [SELECT Issue__c, Issue_publication_date__c, OpportunityId FROM OpportunityLineItem WHERE OpportunityId In:trigger.newMap.keySet() ORDER BY Issue_publication_date__c DESC];
        Map<Id, String> oppIdVsLatestIssueMap = new Map<Id, String>();
        if(oliList != null) {
            for(OpportunityLineItem objOLI : oliList) {
                if(!oppIdVsLatestIssueMap.containsKey(objOLI.OpportunityId)) { //Add only one OppId and its Issue which is having latest Issue_publication_date
                    oppIdVsLatestIssueMap.put(objOLI.OpportunityId, objOLI.Issue__c);
                }
            }
            //Updated latest issue to opportunities
            if(!oppIdVsLatestIssueMap.isEmpty()) {
                for(Id filterdOppId : oppIdVsLatestIssueMap.keySet()) {
                    trigger.newMap.get(filterdOppId).last_issue__c = oppIdVsLatestIssueMap.get(filterdOppId);
                }
            }
        }
    }
}
Many Thanks
Conor
Hi

I am having trouble with my apex trigger. I have written SOQL inside FOR loop which causes Exception : "System.LimitException: Too many SOQL queries: 101". Can someone help me 'bulkify' this trigger as I can't seem to do so. Many Thanks, Triger below: 
 
trigger MnCopyPrimaryContact on Opportunity (before update) {
   for (Opportunity o : Trigger.new) {
       OpportunityContactRole[] contactRoleArray =
       [select ContactID, isPrimary from OpportunityContactRole where OpportunityId = :o.id ORDER BY isPrimary DESC, createdDate];
       if (contactRoleArray.size() > 0) {
           o.Opportunity_contact__c = contactRoleArray[0].ContactID;           
       }else{
           o.Opportunity_contact__c = null;
       }
   }
 }

 
Hi, we are a publishing company that sell adverts into different magazine 'Issues'(custom SF Object)

I have an apex trigger called EndOfSeriesIssue that takes the latest Issue from opportunityLineItem and puts it to a look up field on opportunity called last_issue__c, based on Issue_publication_date__c

This works fine. However there is a problem when two issues have the same Issue_publication_date__c. Could anyone tell me how to modify my trigger to deal with this problem? Trigger below:


trigger EndOfSeriesIssue on Opportunity (before update) {
    
    for (Opportunity o : Trigger.new) {
        
        OpportunityLineItem[] LineItemArray =
       [select Issue__c, Issue_publication_date__c from OpportunityLineItem where OpportunityId = :o.id ORDER BY Issue_publication_date__c DESC];
       if (LineItemArray.size() > 0) {
           
           o.last_issue__c = LineItemArray[0].Issue__c;
           
       }else{
           
            o.last_issue__c = null;   
           
      }
   }
}
Hi, I have a VF page that show 'Similar Leads' based on matching by email. Because in our org it is okay to have the same lead twice as they could be for different events. Any way I have a VF page with apex class that shows the 'similar leads'. Howvwer this just shows the leads, doesnt allow you to edit them. Can you suggest how this could be done? Code below:

Apex: 
public class GetLeadsApexController {
    private final Lead lead;
    
    public GetLeadsApexController(ApexPages.StandardController stdController) {
        if (!Test.isRunningTest()) {
            stdController.addFields(new List<String>{'Name', 'Email', 'OwnerId', 'Status', 'Product_of_Interest__c'});
        }
        this.lead = (Lead)stdController.getRecord();
    }

    public List<Lead> getLeads () {             
        List<Lead> leads = [
            SELECT Id, Name, Email, Status, OwnerId, Product_of_Interest__c FROM Lead WHERE Id != :lead.Id AND Email = :lead.Email
        ];
        return leads;
    }
}

VF Page:
<apex:page lightningStylesheets="true" standardController="Lead" extensions="GetLeadsApexController" tabStyle="Lead" sidebar="false">
 <apex:pageBlock >
          <apex:pageBlockTable value="{! leads }" var="ct" id="leads_list">
            <apex:column value="{! ct.name}" />
            <apex:column value="{! ct.email}" />           
            <apex:column value="{! ct.Product_of_Interest__c}" />
            <apex:column value="{! ct.OwnerId}" />
            <apex:column value="{! ct.Status}" />
            <apex:inputCheckbox />
          </apex:pageBlockTable>    
      </apex:pageBlock>
</apex:page>


Many Thanks
Conor
 
Hi 
Could someone help me write a apex unit test for the following trigger. I am new to apex.
trigger acceptedspeakerroles on CampaignMember (after insert, after update, after delete, after undelete) {
Map<Id,Contact> contacts = new Map<Id,Contact>();
  if(Trigger.new<>null)
    for(CampaignMember c:Trigger.new)
      if(c.ContactId<>null)
        contacts.put(c.ContactId,new Contact(id=c.ContactId));
  if(Trigger.old<>null)
    for(CampaignMember c:Trigger.old)
      if(c.ContactId<>null)
        contacts.put(c.ContactId,new Contact(id=c.ContactId));
  update contacts.values();
}


Many Thanks
Conor

Hi

I have an apex trigger that takes a contact from the contact roles on opportunity, and adds it to a look up field on the opportunity called opportunity_contact__c.

I also need this to then go to another lookup field on the oportunity line items, also called opportunity_contact__c. Can anyone help me with this? Trigger below:
 
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;
        }
    }
}

Many Thanks
Conor​
Hi

I am having trouble with my apex trigger. I have written SOQL inside FOR loop which causes Exception : "System.LimitException: Too many SOQL queries: 101". Can someone help me 'bulkify' this trigger as I can't seem to do so. Many Thanks, Triger below: 
 
trigger MnCopyPrimaryContact on Opportunity (before update) {
   for (Opportunity o : Trigger.new) {
       OpportunityContactRole[] contactRoleArray =
       [select ContactID, isPrimary from OpportunityContactRole where OpportunityId = :o.id ORDER BY isPrimary DESC, createdDate];
       if (contactRoleArray.size() > 0) {
           o.Opportunity_contact__c = contactRoleArray[0].ContactID;           
       }else{
           o.Opportunity_contact__c = null;
       }
   }
 }

 
Hi, we are a publishing company that sell adverts into different magazine 'Issues'(custom SF Object)

I have an apex trigger called EndOfSeriesIssue that takes the latest Issue from opportunityLineItem and puts it to a look up field on opportunity called last_issue__c, based on Issue_publication_date__c

This works fine. However there is a problem when two issues have the same Issue_publication_date__c. Could anyone tell me how to modify my trigger to deal with this problem? Trigger below:


trigger EndOfSeriesIssue on Opportunity (before update) {
    
    for (Opportunity o : Trigger.new) {
        
        OpportunityLineItem[] LineItemArray =
       [select Issue__c, Issue_publication_date__c from OpportunityLineItem where OpportunityId = :o.id ORDER BY Issue_publication_date__c DESC];
       if (LineItemArray.size() > 0) {
           
           o.last_issue__c = LineItemArray[0].Issue__c;
           
       }else{
           
            o.last_issue__c = null;   
           
      }
   }
}
Hi, I have a VF page that show 'Similar Leads' based on matching by email. Because in our org it is okay to have the same lead twice as they could be for different events. Any way I have a VF page with apex class that shows the 'similar leads'. Howvwer this just shows the leads, doesnt allow you to edit them. Can you suggest how this could be done? Code below:

Apex: 
public class GetLeadsApexController {
    private final Lead lead;
    
    public GetLeadsApexController(ApexPages.StandardController stdController) {
        if (!Test.isRunningTest()) {
            stdController.addFields(new List<String>{'Name', 'Email', 'OwnerId', 'Status', 'Product_of_Interest__c'});
        }
        this.lead = (Lead)stdController.getRecord();
    }

    public List<Lead> getLeads () {             
        List<Lead> leads = [
            SELECT Id, Name, Email, Status, OwnerId, Product_of_Interest__c FROM Lead WHERE Id != :lead.Id AND Email = :lead.Email
        ];
        return leads;
    }
}

VF Page:
<apex:page lightningStylesheets="true" standardController="Lead" extensions="GetLeadsApexController" tabStyle="Lead" sidebar="false">
 <apex:pageBlock >
          <apex:pageBlockTable value="{! leads }" var="ct" id="leads_list">
            <apex:column value="{! ct.name}" />
            <apex:column value="{! ct.email}" />           
            <apex:column value="{! ct.Product_of_Interest__c}" />
            <apex:column value="{! ct.OwnerId}" />
            <apex:column value="{! ct.Status}" />
            <apex:inputCheckbox />
          </apex:pageBlockTable>    
      </apex:pageBlock>
</apex:page>


Many Thanks
Conor