You need to sign in to do that
Don't have an account?

unit test cannot pass please help
ive written a trigger and helper class but struggling to write unit test to pass.
error message
System.DmlException: Update failed. First exception on row 0 with id a0A25000001lav1EAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, easyoffertrigger: execution of AfterUpdate
caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Contact_Name__c]: [Contact_Name__c]
Class.easyoffertriggerhelper.offeroppaffiliates: line 34, column 1
Trigger.easyoffertrigger: line 5, column 1: []
stack trace
Class.easyoffertriggertest.testrigger: line 29, column 1
here is triiger:
here is class:
here is unit test:
error message
System.DmlException: Update failed. First exception on row 0 with id a0A25000001lav1EAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, easyoffertrigger: execution of AfterUpdate
caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Contact_Name__c]: [Contact_Name__c]
Class.easyoffertriggerhelper.offeroppaffiliates: line 34, column 1
Trigger.easyoffertrigger: line 5, column 1: []
stack trace
Class.easyoffertriggertest.testrigger: line 29, column 1
here is triiger:
trigger easyoffertrigger on pba__offer__c (before insert, after insert, before update, after update, before delete, after delete, after undelete){ if(Trigger.isUpdate && Trigger.isAfter){ easyoffertriggerhelper.offeroppaffiliates(Trigger.new);
here is class:
public with sharing class easyoffertriggerhelper { //newcode1 public static void offeroppaffiliates(List<pba__offer__c> offers) { list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>(); recordtype[] tt = [Select r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales']; for (pba__offer__c off : offers) { if( off.pba__Status__c == 'Accepted' ) { Easy_Opportunity__c newopp = new Easy_Opportunity__c (); newopp.ownerid = off.ownerid; newopp.name = 'Offer Opportunity - Affiliates'; newopp.CurrencyIsoCode = off.currencyisocode; newopp.stage__c = 'New'; newopp.recordtypeid = tt[0].Id; newopp.Contact_Name__c = off.Contact_Name__c; newopp.Close_Date__c = Date.today().addDays(2); opplist.add(newopp); insert opplist; List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Recordtype.name":"Residential Sales","Name":"Affiliates","List_Price__c":"111.00","Sold_Price__c":"111.00"}]', List<Product__c>.class); for (Product__c eachProd : Pd) eachProd.Easy_Opportunity__c = opplist[0].id; insert Pd; } } }
here is unit test:
@isTest public class easyoffertriggertest { static testmethod void testrigger(){ contact ct = new contact(firstname = 'iain', lastname = 'banks'); pba__Listing__c lt = new pba__Listing__c(name = 'iainbanks title'); pba__Property__c py = new pba__Property__c(name = 'iainbanks title', PropertyOwnerContactId__c = ct.id); User u1 = [SELECT Id FROM User WHERE Alias='ibank']; pba__offer__c off = new pba__offer__c( currencyisocode = 'GBP', ownerid = u1.id, Package_pruchased__c = '1', pba__Status__c = 'Active', pba__Listing__c = lt.id); insert off; Test.StartTest(); off.pba__Status__c = 'Accepted'; update off; Test.StopTest(); } }
I hope you need to pass "Contact_Name__c " on "pba__offer__c" object like below:-