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
Salesforce_user007Salesforce_user007 

Trigger on comparing object fields and displaying error maeesge.

Suppose i have two object 

     1.Account- standard object[it has a field name Status__c which is a picklist having value inprogress and closed]

     2.Client__c - custom object[it also have same field name Status__c which is a picklist having value inprogress and closed]

 

and Client__c has lookup to Account name which means Account has a related list of client object .

 

My question is :

    I want to write a trigger where if i put account status to "closed"  i can not put client status to "closed",it should throw an error message on client object or if i put client status to closed i can not put account status to closed vice versa.

 

Can any one please help me to write a trigger on this??

 

Thanks in advance.

 

HariDineshHariDinesh

Hi,

 

You can use below code samples to produce an Error.

 

trigger.new[0].addError(e.getMessage());

 

catch(Exception ex)
{
   ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR ,ex.getMessage());
   ApexPages.addMessage(msg);
  }

 

Here write trigger such that if your error criteria matches then add error to that page by using above code samples.

Salesforce_user007Salesforce_user007

Thanks for your reply...but can you please help me with 1st and 2nd points above and help me to write the trigger??

HariDineshHariDinesh

Hi,

 

Please try to write the trigger and Look for the help where you are getting some errors.

Post the code where you are getting Error and let me know so that I can help you.

 

IF you need to get some understand on writing Triggers please go through below link

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_trigger.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

Salesforce_user007Salesforce_user007

I have the basic understanding of trigger but i just want to know ,how  do i compare the status__c field b/w Account and Client object  using trigger??

Salesforce_user007Salesforce_user007

I am trying to write the trigger like this:

 

trigger StatusUpdate on Account (before insert , before update) {

 

for(Account a: Trigger.new) {

List<Client__c> acc = new List<Client__c>();

acc = [SELECT ID,Status__c from Client__c WHERE AccountID IN: a.ID];
for (Client__c li: acc) {
if ( li.Status__c == li.Account.Status__c )
{
li.AddError('Some Error Message');
// li.Status__c = li.Client__r.Status__c;
}
}
}
// update li;
}

but when i save the trigger i get an error message that " No such column 'AccountID' on entity 'Client__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name"

 

How do i write the relationship here?

any help on this??

HariDineshHariDinesh

Hi,

Here problem with the syntax of your coding.

acc = [SELECT ID,Status__c from Client__c WHERE AccountID IN: a.ID];

 

Replace your line of select query with the below line

(Here you should refer account with ther related field API name in your Custom Object)

For Ex if your relation name in Custom object is Account__C then the syntax will be like this

 

acc = [SELECT ID,Status__c from Client__c WHERE Account__C =: a.ID];