• Kev Brier
  • NEWBIE
  • 65 Points
  • Member since 2014
  • Reporting & System Development Manager
  • OCS Group UK

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 19
    Replies
Hi,

Hoping someone can help to point us in the right direction, it seems that whatever we do, we are unable to get better than 26% code coverage with our Invocable Email Class calling an @future Method to send a single email with a visualforce attachement generated on the record. The Class works like a charm and does exactly what it is meant to do and we are even able to dummy an email being sent and assert the fact it was but it doesn't get close to covering the code. 

Any guidance would be greatfully appreciate, as even though we run at 95% coverage, i'm not prepared to push a class with only 26% even though it works fully.

Please help save my sanity? 
 
Invoked from process builder with 100% coverage

public class ProcessHandler {
    public class NPS{
        @InvocableVariable(required=true)
        public Id npsid;
        @InvocableVariable(required=true)
        public String mname;
        @InvocableVariable(required=true)
        public String oname;
        @InvocableVariable(required=true)
        public String subject;
        @InvocableVariable(required=true)
        public String client;
        @InvocableVariable(required=true)
        public String md;
        @InvocableVariable(required=true)
        public String body;
    }
    
    @InvocableMethod
   public static void invokeapexcallout(NPS[] nps) {
     Futureapexcallout.apexcallout(nps[0].mname, nps[0].oname, nps[0].npsid, nps[0].subject, nps[0].client,nps[0].md,nps[0].body);

    }
}
 
@Future Method with only 26% coverage

public class Futureapexcallout{

  @future(Callout=true)
  public static void apexcallout(string mname,string oname,Id NPSId, String subject, String client, string md, string body){
         
        
         
        pageReference pdfPage = Page.NPS_Survey;
        pdfPage.getParameters().put('id',NPSID);
        
        if(Test.isRunningTest()){
        blob b = blob.valueOf('Unit.Test');
        }else{
        blob b = pdfpage.getContentaspdf(); 

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

        Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        efa1.setFileName('NPS.pdf');
        efa1.setBody(b);

        String addresses;
        email.setSubject(subject);
        email.setToAddresses(new List<String> {mname});
        email.setCCAddresses(new List<String> {oname});
        email.setPlainTextBody(body);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}
}
 
Test Class, probably really badly written :)

@isTest
private class testProcessHandler
{
private static testMethod void testProcessHandler() {
    
    test.startTest();
    
        User u2 = new User(Alias = 'nUr32', Email='newuser@testorg32.com', 
        EmailEncodingKey='UTF-8', LastName='Testing12', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = '00e90000001yvFD' , 
        TimeZoneSidKey='America/Los_Angeles', UserName='newuser32@testorg.com',External_Use__c='SRK1132AA',Operating_Country__c='Global',Region__c='Global' ,Team__c='Client Services - Global');
        insert u2;
     
        Account a = new Account(Name = 'Test A',
        BillingCountry = 'UK',
        Ownership_Type__c = 'Private',
        Phone = '07826533392',
        Industry = 'Property development & management',
        Type = 'Customer Direct');
        insert a;
    
        Contact c = new Contact (FirstName='Test', LastName='Test', AccountId = a.Id,
        Phone = '07826533392');
        insert c;    
        
        Net_Promoter__c n = new Net_Promoter__c (Account__c=a.Id,
        Assigned_To__c=u2.id,
        Client_Visiting__c=c.id,
        Activity_Date__c = datetime.newInstance(2017, 9, 15, 12, 30, 0),
        End_Time__c=datetime.newInstance(2017, 9, 15, 14, 30, 0),
        Site_Location__c='Test'                                                                                   
        );
        insert n;
        
        n.Survey_Status__c='Complete';
        n.Score_Updated__c=TRUE;
        update n;
        
        List<ProcessHandler.NPS> Params = new List<ProcessHandler.NPS>();

        ProcessHandler.NPS p = new ProcessHandler.NPS();
        p.npsid = n.id;
        p.mname = n.MD_Name__c;
        p.oname = n.Ops_Director__c;
        p.subject = n.subject__c;
        p.client = n.client_visiting__r.FirstName;      
        p.md = n.MD_Email__c;
        p.body = n.Body__c; 
        
        Params.add(p);
        
        ProcessHandler.Invokeapexcallout(params);
        
        Futureapexcallout.apexcallout(n.MD_Name__c, n.Ops_Director__c, n.id, n.subject__c,n.client_visiting__r.FirstName, n.MD_Name__c, N.Body__c );
        Integer invocations = Limits.getEmailInvocations();
        
        pageReference pdfPage = Page.NPS_Survey;
        pdfPage.getParameters().put('id',n.id);
        
        if(Test.isRunningTest()){
        blob b = blob.valueOf('Unit.Test');
        }else{
        blob b = pdfpage.getContentaspdf(); 
         
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

        Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        efa1.setFileName('NPS.pdf');

        String addresses;
        email.setSubject(n.subject__c);
        email.setToAddresses(new List<String> {n.MD_Name__c});
        email.setCCAddresses(new List<String> {n.Ops_Director__c});
        email.setPlainTextBody(N.Body__c);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        
        test.stopTest();
        
        system.assertEquals(p.npsid, n.id);       
        system.assertEquals(1, invocations, '1 email should be sent');
}           
}
}

 
Hi,

We have a requirement on our custom object bid to show the opportunity line items in a table on a visualforce page when selected by a user on a Force.com licence. We have the functionality working whereby an extender class gets and sets the ID via an opportunity lookup which works when the user can read the opportunity but doesn't show to our Force.com users. We've seen this working in principal on another platform but we can't seem to recreate the logic, we've found with most things a text formula lookup or a workflow field update is an easy work around but can't seem to figure out the easy fix for this.


I'll attach the extender class and visualforce page below hoping that someone can offer some guidance on a workable fix.

 
public class bidproducts
{
    public List<OpportunityLineItem> lineItems { get; set; }
    
    Public bidproducts( ApexPages.StandardController std )
    {
        if( std.getRecord().Id != null )
        {
            Go_Or_No_Go__c con = [ Select Id, Opportunity__c from Go_Or_No_Go__c where Id =: std.getRecord().Id ];
            
            lineItems = [ Select Id, Opportunityid, Opportunity.Name , PricebookEntry.Name, TotalPrice, Quantity, Product2.Name, Description, Ad_Hoc__c, Gross_Margin_Percentage__c, Gross_Margin_Currency__c from OpportunityLineItem where Opportunityid =: con.Opportunity__c ];
        }
        else
        {
            lineItems = new List<OpportunityLineItem>();
        }
    }         
}
 
<apex:page standardController="Go_or_No_Go__c" extensions="bidproducts" renderAs="pdf" showheader="false" sidebar="false">          
         <apex:form >
           <head>
           <style> 
            body { font-family: 'Arial Unicode MS'; }
            .companyName { font: bold 30px; color: red; }  
        </style>
    </head>
        <center>
        <h1>OCS Go/No Go Script</h1>
        <h2>{!Go_or_No_Go__c.Bid__r.Owner.Name} - {!Go_Or_No_Go__c.Bid__r.Opportunity_Name__c}</h2>
        </center>
<body>

<h2> Account Information </h2>

<apex:pageblock >
<b>Description</b> - {!Go_or_No_Go__c.bid__r.Account_Description__c}
<br/>
<br/>
<apex:pageblockTable value="{!Go_or_No_Go__c.Bid__r}" width="75%" var="ac" border="0.5" >
<apex:column value="{!ac.Credit_Score__c}" />
<apex:column value="{!ac.Credit_Rating__c}" />
<apex:column value="{!ac.Credit_Comment__c}" />
</apex:pageblockTable>
<br/>
<b>Existing Business</b> - {!Go_or_No_Go__c.Existing_Business__c}<br/>
<br/>
<apex:pageblockTable value="{!Go_or_No_Go__c}" width="100%" var="r1" border="0.2" >
<apex:column value="{!r1.Decision_Maker_Name__c}" />
<apex:column value="{!r1.Decision_Maker_Job_Title__c}" />
<apex:column value="{!r1.Decision_Maker_Map_to_OCS__c}" />
</apex:pageblockTable>
<br/>
<apex:pageblockTable value="{!Go_or_No_Go__c}" width="100%" var="r2" border="0.2" >
<apex:column value="{!r2.Other_Name_1__c}" />
<apex:column value="{!r2.Other_Job_Title_1__c}" />
<apex:column value="{!r2.Other_Map_to_OCS_1__c}" />
</apex:pageblockTable>
<br/>
<apex:pageblockTable value="{!Go_or_No_Go__c}" width="100%" var="r3" border="0.2" >
<apex:column value="{!r3.Other_Name_2__c}" />
<apex:column value="{!r3.Other_Job_Title_2__c}" />
<apex:column value="{!r3.Other_Map_to_OCS2__c}" />
</apex:pageblockTable>
<br/>
<h2> Opportunity Information </h2>
<b>Description</b> - {!Go_or_No_Go__c.bid__r.Opportunity_Description__c}<br/>
<b>Locations</b> - {!Go_or_No_Go__c.Locations__c}<br/>
<br/>
<b>Bid Submission Date</b> - {!Go_or_No_Go__c.Bid__r.Bid_Submission_Date__c}<br/>
<b>Service Start Date</b> - {!Go_or_No_Go__c.Bid__r.Service_Start_Date__c}<br/>
<br/>
<b>Key Reasons to Bid</b><br/>
&bull; {!Go_or_No_Go__c.Reason_1__c}<br/>
&bull; {!Go_or_No_Go__c.Reason_2__c}<br/>
&bull; {!Go_or_No_Go__c.Reason_3__c}<br/>
<br/>
<b>Site References and Reasons</b><br/>
&bull; {!Go_or_No_Go__c.Reference_1__c} - {!Go_or_No_Go__c.Reason_for_Reference_1__c} <br/>
&bull; {!Go_or_No_Go__c.Reference_2__c} - {!Go_or_No_Go__c.Reason_for_Reference_2__c} <br/>
&bull; {!Go_or_No_Go__c.Reference_3__c} - {!Go_or_No_Go__c.Reason_for_Reference_3__c} <br/>
<br/>
<b>Terms and Conditions</b> - {!Go_or_No_Go__c.Terms_and_Conditions__c}
<br/>
<b>Known Competition</b> - {!Go_or_No_Go__c.Known_Competition__c}
<br/>
<h2>Product Summary and Estimated Value</h2>
<b>Estimated Annual Value</b> - {!Go_or_No_Go__c.Bid__r.Annual_Value__c} <br/>
<b>Term of Contract</b> - {!Go_or_No_Go__c.Bid__r.Term__c} <br/>
<b>Estimated Contract Value</b> - {!Go_or_No_Go__c.Bid__r.Contract_Value__c} <br/>
<b>Estimated Capex</b> - {!Go_or_No_Go__c.Estimated_CAPEX__c} <br/>
<br/>

<apex:pageblockTable value="{!LineItems}" width="100%" var="ml" border="0.5">
<apex:column value="{!ml.Product2.Name}" />
<apex:column value="{!ml.Description}" />
<apex:column value="{!ml.Ad_Hoc__c}" />
<apex:column value="{!ml.TotalPrice}" />
<apex:column value="{!ml.Gross_Margin_Percentage__c}" />
<apex:column value="{!ml.Gross_Margin_Currency__c}" />
</apex:pageblockTable>
</apex:pageblock>
</body>
</apex:form>
</apex:page>

Thanks,

Kev
Hi,

We're new to Salesforce Development but we've managed to pull together a trigger that updates a Primary Contact Lookup on the opportunity. The trigger works perfectly well, however, we hit the SQL limit in Salesforce and are unsure how to overcome this.

Please can you advise on how the code could be written in a more efficient way?
 
trigger trgMnCopyPrimaryContact 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;
       }
   }
 }

Thanks,

Kev
 
Hi,

We've been working on a trigger which creates a contract upon an Opportunity being complete and 'Closed Won'. We've managed to get the record to create and copy over important info, although we can't manage to figure the validation logic. Granted we have little apex knowledge and therefore we've attempted to put most of the logic outside the trigger and within a workflowed checkbox on the Opportunity.

However, this don't eliminate the duplication of records if the user edits a Won opportunity. I know the better versed Salesforce developer would have the list check the record doesn't exist in the first place before the trigger even starts and therefore we're reaching out hoping you guys could support us in finishing this piece of work.

Any help or advice would be gratefully appreciated. 

Kev
I've been searching around to try to find the simplest solution to my problem and are somewhat confused on which way to go.

Basically I need the Account lookup field to auto populate on creating a new Opportunity from our custom object Site__c. I've been reading about Process Builder, Workflow Rules and my initial thought a trigger on Opportunity. 

Ideally I'd like to avoid having to write the trigger but assuming I do then I'm guessing this should be on the Opporunity before insert?

For clairty the custom object Site__c has a master detail relationship to the account and therefore I guessing this is where we map the Id?

Any guidance that could be offered would  be really appreciated.
 
Hi,

I'm new to developing in general and I am trying to create a trigger that updates the custom lookup field Bid_Name__c on the standard Opportunity object. I have a created a custom object called Bid__c which has a related one-to-one relationship with the Opportunity. I'm getting an error when attempting to save the Opportunity record, although it mentions the correct reference below but doesnt populate the field. My code is pasted below the error message.

System.StringException: Invalid id: Bid-1312: Trigger.UpdateBid: line 9, column 1

trigger UpdateBid on Opportunity (before insert, before update) {
    if (trigger.isBefore) { 
        if (trigger.isInsert || trigger.isUpdate) {
            //list Bids
            list<Bid__c> Bid = [SELECT ID, Name FROM Bid__c];
            for (Opportunity o : trigger.new) {
                o.Bid_Name__c = null;
                for (Bid__c b : Bid){
                    if (b.Name == o.Bid_Name__c) {
                        o.Bid_Name__c = b.Name;
                        break;
                    }
                }
            }
        }
    }
}

Any help that could be offered would be appreciated, struggling to work out what the solution here is.

Thank you
Our custom object is the Bidding element of the Opportunity (which we call Bid__c) and is a related list against the standard opportunity object. On the Opportunity we have created a Lookup Relationship to the bid object, which needs to be auto popluated with the related bid number. We have in past used Workflow/Field Update to acheive this type of scenairo but obviously this doesnt work for Lookup Relationships. 

We have attempted to write our first trigger, we've got the stage that no error's are found but the trigger itself doesn't work. Code below:

trigger UpdateLookup on Opportunity (after insert, before update) 
{
    //instantiate set to hold unique Bid record ids
    Set<Id> BidIds = new Set<Id>();
    for(Opportunity s : Trigger.new)
    {
        BidIds.add(s.Bid_Name__c);
    }

    //instantiate map to hold deployment record Opportunity ids to their corresponding Bid Name
    Map<Id, Bid__c> BidIdsMap = new Map<Id, Bid__c>([SELECT Id, Name FROM Bid__c WHERE Id IN: BidIds]);

    for (Opportunity s : Trigger.new) 
    {
        if (s.Name == null && BidIdsMap.containsKey(s.Bid_Name__c)) 
        {
            s.Name = BidIdsMap.get(s.Bid_Name__c).Name;
        }
    }
}

We've kind of hit a brick wall and would really appreciate any support or guidance?

Opportunity Lookup Field - Bid_Name__c
Custom Object - Bid__c
Related field that we required to populate the lookup - Name (standard field)
Hi,

Hoping someone can help to point us in the right direction, it seems that whatever we do, we are unable to get better than 26% code coverage with our Invocable Email Class calling an @future Method to send a single email with a visualforce attachement generated on the record. The Class works like a charm and does exactly what it is meant to do and we are even able to dummy an email being sent and assert the fact it was but it doesn't get close to covering the code. 

Any guidance would be greatfully appreciate, as even though we run at 95% coverage, i'm not prepared to push a class with only 26% even though it works fully.

Please help save my sanity? 
 
Invoked from process builder with 100% coverage

public class ProcessHandler {
    public class NPS{
        @InvocableVariable(required=true)
        public Id npsid;
        @InvocableVariable(required=true)
        public String mname;
        @InvocableVariable(required=true)
        public String oname;
        @InvocableVariable(required=true)
        public String subject;
        @InvocableVariable(required=true)
        public String client;
        @InvocableVariable(required=true)
        public String md;
        @InvocableVariable(required=true)
        public String body;
    }
    
    @InvocableMethod
   public static void invokeapexcallout(NPS[] nps) {
     Futureapexcallout.apexcallout(nps[0].mname, nps[0].oname, nps[0].npsid, nps[0].subject, nps[0].client,nps[0].md,nps[0].body);

    }
}
 
@Future Method with only 26% coverage

public class Futureapexcallout{

  @future(Callout=true)
  public static void apexcallout(string mname,string oname,Id NPSId, String subject, String client, string md, string body){
         
        
         
        pageReference pdfPage = Page.NPS_Survey;
        pdfPage.getParameters().put('id',NPSID);
        
        if(Test.isRunningTest()){
        blob b = blob.valueOf('Unit.Test');
        }else{
        blob b = pdfpage.getContentaspdf(); 

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

        Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        efa1.setFileName('NPS.pdf');
        efa1.setBody(b);

        String addresses;
        email.setSubject(subject);
        email.setToAddresses(new List<String> {mname});
        email.setCCAddresses(new List<String> {oname});
        email.setPlainTextBody(body);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}
}
 
Test Class, probably really badly written :)

@isTest
private class testProcessHandler
{
private static testMethod void testProcessHandler() {
    
    test.startTest();
    
        User u2 = new User(Alias = 'nUr32', Email='newuser@testorg32.com', 
        EmailEncodingKey='UTF-8', LastName='Testing12', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = '00e90000001yvFD' , 
        TimeZoneSidKey='America/Los_Angeles', UserName='newuser32@testorg.com',External_Use__c='SRK1132AA',Operating_Country__c='Global',Region__c='Global' ,Team__c='Client Services - Global');
        insert u2;
     
        Account a = new Account(Name = 'Test A',
        BillingCountry = 'UK',
        Ownership_Type__c = 'Private',
        Phone = '07826533392',
        Industry = 'Property development & management',
        Type = 'Customer Direct');
        insert a;
    
        Contact c = new Contact (FirstName='Test', LastName='Test', AccountId = a.Id,
        Phone = '07826533392');
        insert c;    
        
        Net_Promoter__c n = new Net_Promoter__c (Account__c=a.Id,
        Assigned_To__c=u2.id,
        Client_Visiting__c=c.id,
        Activity_Date__c = datetime.newInstance(2017, 9, 15, 12, 30, 0),
        End_Time__c=datetime.newInstance(2017, 9, 15, 14, 30, 0),
        Site_Location__c='Test'                                                                                   
        );
        insert n;
        
        n.Survey_Status__c='Complete';
        n.Score_Updated__c=TRUE;
        update n;
        
        List<ProcessHandler.NPS> Params = new List<ProcessHandler.NPS>();

        ProcessHandler.NPS p = new ProcessHandler.NPS();
        p.npsid = n.id;
        p.mname = n.MD_Name__c;
        p.oname = n.Ops_Director__c;
        p.subject = n.subject__c;
        p.client = n.client_visiting__r.FirstName;      
        p.md = n.MD_Email__c;
        p.body = n.Body__c; 
        
        Params.add(p);
        
        ProcessHandler.Invokeapexcallout(params);
        
        Futureapexcallout.apexcallout(n.MD_Name__c, n.Ops_Director__c, n.id, n.subject__c,n.client_visiting__r.FirstName, n.MD_Name__c, N.Body__c );
        Integer invocations = Limits.getEmailInvocations();
        
        pageReference pdfPage = Page.NPS_Survey;
        pdfPage.getParameters().put('id',n.id);
        
        if(Test.isRunningTest()){
        blob b = blob.valueOf('Unit.Test');
        }else{
        blob b = pdfpage.getContentaspdf(); 
         
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

        Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        efa1.setFileName('NPS.pdf');

        String addresses;
        email.setSubject(n.subject__c);
        email.setToAddresses(new List<String> {n.MD_Name__c});
        email.setCCAddresses(new List<String> {n.Ops_Director__c});
        email.setPlainTextBody(N.Body__c);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        
        test.stopTest();
        
        system.assertEquals(p.npsid, n.id);       
        system.assertEquals(1, invocations, '1 email should be sent');
}           
}
}

 
Hi,

We're new to Salesforce Development but we've managed to pull together a trigger that updates a Primary Contact Lookup on the opportunity. The trigger works perfectly well, however, we hit the SQL limit in Salesforce and are unsure how to overcome this.

Please can you advise on how the code could be written in a more efficient way?
 
trigger trgMnCopyPrimaryContact 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;
       }
   }
 }

Thanks,

Kev
 
Hi,

We've been working on a trigger which creates a contract upon an Opportunity being complete and 'Closed Won'. We've managed to get the record to create and copy over important info, although we can't manage to figure the validation logic. Granted we have little apex knowledge and therefore we've attempted to put most of the logic outside the trigger and within a workflowed checkbox on the Opportunity.

However, this don't eliminate the duplication of records if the user edits a Won opportunity. I know the better versed Salesforce developer would have the list check the record doesn't exist in the first place before the trigger even starts and therefore we're reaching out hoping you guys could support us in finishing this piece of work.

Any help or advice would be gratefully appreciated. 

Kev
I've been searching around to try to find the simplest solution to my problem and are somewhat confused on which way to go.

Basically I need the Account lookup field to auto populate on creating a new Opportunity from our custom object Site__c. I've been reading about Process Builder, Workflow Rules and my initial thought a trigger on Opportunity. 

Ideally I'd like to avoid having to write the trigger but assuming I do then I'm guessing this should be on the Opporunity before insert?

For clairty the custom object Site__c has a master detail relationship to the account and therefore I guessing this is where we map the Id?

Any guidance that could be offered would  be really appreciated.
 
Hi,

I'm new to developing in general and I am trying to create a trigger that updates the custom lookup field Bid_Name__c on the standard Opportunity object. I have a created a custom object called Bid__c which has a related one-to-one relationship with the Opportunity. I'm getting an error when attempting to save the Opportunity record, although it mentions the correct reference below but doesnt populate the field. My code is pasted below the error message.

System.StringException: Invalid id: Bid-1312: Trigger.UpdateBid: line 9, column 1

trigger UpdateBid on Opportunity (before insert, before update) {
    if (trigger.isBefore) { 
        if (trigger.isInsert || trigger.isUpdate) {
            //list Bids
            list<Bid__c> Bid = [SELECT ID, Name FROM Bid__c];
            for (Opportunity o : trigger.new) {
                o.Bid_Name__c = null;
                for (Bid__c b : Bid){
                    if (b.Name == o.Bid_Name__c) {
                        o.Bid_Name__c = b.Name;
                        break;
                    }
                }
            }
        }
    }
}

Any help that could be offered would be appreciated, struggling to work out what the solution here is.

Thank you
Our custom object is the Bidding element of the Opportunity (which we call Bid__c) and is a related list against the standard opportunity object. On the Opportunity we have created a Lookup Relationship to the bid object, which needs to be auto popluated with the related bid number. We have in past used Workflow/Field Update to acheive this type of scenairo but obviously this doesnt work for Lookup Relationships. 

We have attempted to write our first trigger, we've got the stage that no error's are found but the trigger itself doesn't work. Code below:

trigger UpdateLookup on Opportunity (after insert, before update) 
{
    //instantiate set to hold unique Bid record ids
    Set<Id> BidIds = new Set<Id>();
    for(Opportunity s : Trigger.new)
    {
        BidIds.add(s.Bid_Name__c);
    }

    //instantiate map to hold deployment record Opportunity ids to their corresponding Bid Name
    Map<Id, Bid__c> BidIdsMap = new Map<Id, Bid__c>([SELECT Id, Name FROM Bid__c WHERE Id IN: BidIds]);

    for (Opportunity s : Trigger.new) 
    {
        if (s.Name == null && BidIdsMap.containsKey(s.Bid_Name__c)) 
        {
            s.Name = BidIdsMap.get(s.Bid_Name__c).Name;
        }
    }
}

We've kind of hit a brick wall and would really appreciate any support or guidance?

Opportunity Lookup Field - Bid_Name__c
Custom Object - Bid__c
Related field that we required to populate the lookup - Name (standard field)