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
anji punyamanthulaanji punyamanthula 

If I try to create Contact record without Account Id then it has to throw an error by using trigger hoe to do that

Below Example after taking Account als its throwing same error where i'm wrong let me know any one help in that 


trigger con_accname_error on Contact (before insert,before update) {
    for(contact c:trigger.new){
        if(c.Account==null){
            c.addError('pl enter account name');
        }
       
        
    }

}

 
Best Answer chosen by anji punyamanthula
Maharajan CMaharajan C
Hi Anji,

Please use the below code:

trigger con_accname_error on Contact (before insert,before update) {
    for(contact c:trigger.new){
        if(c.AccountId==null){
            c.addError('pl enter account name');
        }
       
        
    }

}

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
​Raj

All Answers

Maharajan CMaharajan C
Hi Anji,

Please use the below code:

trigger con_accname_error on Contact (before insert,before update) {
    for(contact c:trigger.new){
        if(c.AccountId==null){
            c.addError('pl enter account name');
        }
       
        
    }

}

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
​Raj
This was selected as the best answer
Eric AnEric An
Hi

addError method is provided by standard method, it dont mean that return the trigger. If you want to it do that you have to add return; after the addError.
anji punyamanthulaanji punyamanthula
Thanks for your response its working fine