0

i have written before insert trigger on quote,when inserting a quote i want to populate contact detail like name,email,phone,fax from opportunity the contact which is acting as a contactroles related list in opportunity.this is working fine i write test class but i did not get coverage.sometimes method is passing and getting 57% coverage.it is not covering from line no 21 to 27 please help me out

Class

/**** This class is responsible for Auto populate Contact fileds like contactname, email,phone,fax from opportunity into Quote when opportunity has Contactroles relatedlist ***/

public class AutoPopulateContactFieldsInQuoteHelper {

    public void PopulateContactFieldsInQuote(list<Quote> quotelist1){

        List<Quote> quoteList = new List<Quote>();
        Map<String, Quote> QuotetoOpportunityIDMap = new Map<String, Quote>();
        for( Quote Qid : quotelist1 )
        {
            QuotetoOpportunityIDMap.put( Qid.OpportunityID, Qid ); 
        } 

        List<OpportunityContactRole> Contactlist = [select OpportunityID, IsPrimary,ContactID,contact.email,contact.phone,contact.fax from OpportunityContactRole where OpportunityID in :QuotetoOpportunityIDMap.keySet() and IsPrimary = true];

        for (OpportunityContactRole oppConrole : Contactlist) {                            

            Quote quoteToUpdate = QuotetoOpportunityIDMap.get(oppConrole.OpportunityID);

            quoteToUpdate.ContactId = oppConrole.ContactId;       
            quoteToUpdate.phone = oppConrole.Contact.phone;
            quoteToUpdate.fax = oppConrole.Contact.fax;
            quoteToUpdate.email = oppConrole.Contact.email;
            quoteList.add(quoteToUpdate);       
        }
    }
}

test class

@isTest
public class AutoPopulateContactFieldsInQuoteTest{
    static testmethod void PopulateContactFieldsInQuoteTest(){
        account a=new account();
        a.name='tes';
        insert a;

        contact c=new contact();
        c.lastname='ctes';
        c.accountid=a.id;
        insert c;

        opportunity o = new opportunity();
        o.name = 'Testoppty';
        o.StageName = 'lead';
        o.CloseDate = date.today();
        o.accountid = a.Id;
        o.Billing_Type__c='Aggregate';
        insert o;

        quote q=new quote();
        q.name='tesq';
        q.contactid=c.id;
        q.email='[email protected]';
        insert q;

    }
}
1
  • here i am gettig error is "System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [OpportunityId]: [OpportunityId] Stack Trace: Class.AutoPopulateContactFieldsInQuoteTest.PopulateContactFieldsInQuoteTest: line 25, column 1"
    – Gopinath
    May 28, 2016 at 2:08

1 Answer 1

2

For Quote Object ,the reference to Opportunity is mandatory.

The below code will link quote to opportunity

quote q=new quote();
q.name='tesq';
q.contactid=c.id;
q.email='[email protected]';
q.opportunityId = o.Id;//Link Quote and opportunity record
insert q;

Apart from this ,You will also need to create OpportunityContactRole records by referencing opportunity and contact

8
  • System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AutoPopulateContactFieldsInQuote: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name] ,,i am got this error with opportunity id
    – Gopinath
    May 28, 2016 at 2:16
  • and test method also failed,withot quote insert functionality test method is passing and coverage is 0%
    – Gopinath
    May 28, 2016 at 2:17
  • You can use logs to see which object Name is missing in your code ? May 28, 2016 at 2:22
  • ok,i will check
    – Gopinath
    May 28, 2016 at 2:22
  • 2
    You need some trailhead lessons developer.salesforce.com/… May 28, 2016 at 2:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .