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
venkatesh puttamvenkatesh puttam 

Getting started with Apex Trigger challenge Issue

Need help?
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AddRelatedRecord: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Discount_percent__c]: [Discount_percent__c] Trigger.AddRelatedRecord: line 14, column 1: []


trigger AccountAddressTrigger on Account (before insert,before update) {
    for(Account a :Trigger.new)
    {
        if(a.Match_Billing_Address__c == true)
        {
            a.ShippingPostalcode = a.BillingPostalcode;
        }
    }
}
Best Answer chosen by venkatesh puttam
Amit Chaudhary 8Amit Chaudhary 8
It look like you created any trigger "AddRelatedRecord" which is creating some record and in that record Discount_percent__c field required field

Please deactivate that trigger or Edit "Discount_percent__c" field and remove required checkbox.

And update your code like below
trigger AccountAddressTrigger on Account (before insert, before update) {
	for(Account act : Trigger.new){
		If(act.Match_Billing_Address__c == true && act.BillingPostalCode!= null){
			act.ShippingPostalCode=act.BillingPostalCode;
		}
	}
}

Let us know if this will help you
 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

You have missed the required field value assignment  for "Discount_percent__c" 

i have added some dummy value for this in your code. kinldy modify that using correct value. 

use below updated code. 
trigger AccountAddressTrigger on Account (before insert,before update) {
    for(Account a :Trigger.new)
    {
        if(a.Match_Billing_Address__c == true)
        {
            a.Discount_percent__c= 20;
            a.ShippingPostalcode = a.BillingPostalcode;
        }
    }
}

Hope this will help you. 

Thanks
karthik
 
Amit Chaudhary 8Amit Chaudhary 8
It look like you created any trigger "AddRelatedRecord" which is creating some record and in that record Discount_percent__c field required field

Please deactivate that trigger or Edit "Discount_percent__c" field and remove required checkbox.

And update your code like below
trigger AccountAddressTrigger on Account (before insert, before update) {
	for(Account act : Trigger.new){
		If(act.Match_Billing_Address__c == true && act.BillingPostalCode!= null){
			act.ShippingPostalCode=act.BillingPostalCode;
		}
	}
}

Let us know if this will help you
 
This was selected as the best answer
Giancarlo AmatiGiancarlo Amati
I have the following questions then:

my very first code was this one where I used the __c. Why is it wrong? is it wrong using __c? thank you
trigger AccountAddressTrigger on Account (before insert, before update) {
    
 if (Trigger.isInsert || Trigger.isUpdate) {

        
        for (Account a: Trigger.new){
            if (a.Match_Billing_Address__c = true ){
                a.ShippingPostalCode__c = a.BillingPostalCode__c;
            }
            
            System.debug('GC --->' + a.ShippingPostalCode__c);
        }
    }
    
    for (Account a: Trigger.new) {
        System.debug('GC After UPDATE --->' + a.ShippingPostalCode__c);
    }
    
}

The error I was getting is that the Trigger was not updating the Shipping post code.


GC
 
Amit Chaudhary 8Amit Chaudhary 8
Issue is in a.Match_Billing_Address__c = true

It should be a.Match_Billing_Address__c == true
DMGmefullDMGmefull
Hi Amit,

I want to insert new Opportunity and with that insertion I want to create an OpportunityContactRoles , but can't achieve it.
Please help me in this!!

Here is my code.
What thing I should Implement to achieve the target.

trigger oppowithOCR on Opportunity (after insert) {
    
     List<OpportunityContactRole> ocr = new List<OpportunityContactRole>();
    List<Contact> con = [Select Id,Name from Contact];
    For(Opportunity opp : trigger.new){
        OpportunityContactRole oo =  new OpportunityContactRole();
      //  oo.Id = opp.ContactId;
        oo.OpportunityId = opp.Id;
        oo.IsPrimary = true;
        ocr.add(oo);
    }
    insert ocr;
}
    

Regards,
DMG
AZHAR BAHRAINWALAAZHAR BAHRAINWALA
There is something I don't understand; after "a.ShippingPostalCode = a.BillingPostalCode", should there be code that inserts the data back in trigger.new? Otherwise the matched data would simply remain in the variable "a" . Appreciate any help