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
SF DEVSF DEV 

How to display message on insert.

I have a trigger which calls a class method, now inside the class i need to send message.

 

Eg:

 

Trigger: calling class Testclass

class: Testclass(){

if(somecondition)

{

from here i want pass messsage to trigger.

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

You can return some specifc values for particular error condition. then accordintg to the return value you can add the suitable error using addError.

 

public class Class1
{
	public Class1()
	{
	}
	
	public Integer validationMethod()
	{
	Integer returnValue = 0;
		if(condition1)
		{
			returnValue = 1;
			
		}
		else if(condition2)
		{
			returnValue = 2;
		}
		else
		{
			returnValue = 0;
		}
	}
}

 

trigger Trigger1 on YourObject__c (before insert) {

  if(Trigger.isBefore && Trigger.isInsert)
  {
	Class1 newC= new Class1();
	Integer validationId = newC.validationMethod();
	
	if(validationId == 1)
	{
	//Trigger.addError('your msg');
	Trigger.new[0].Name.addError('Your error msg1');
	}
	else if(validationId == 2)
	{
	//Trigger.addError('your msg');
	Trigger.new[0].Name.addError('Your error msg2');
	}
  }
  
  }

 

Hit the Kudos button if any post helps you - Mark the answer as solution, It might help others running to into similar problem in future.

All Answers

harsha__charsha__c

Hi

 

If you are trying to throw an error to the detail page like "insert successful" or any type of error message

 

Then  you can use

 

Trigger.addError('your msg');

 

If this is not the case can you elaborate a bit that why you need to send a msg to the trigger..!

SF DEVSF DEV
Im calling class from trigger, so error msg should be sent from class.
Chamil MadusankaChamil Madusanka

You can return some specifc values for particular error condition. then accordintg to the return value you can add the suitable error using addError.

 

public class Class1
{
	public Class1()
	{
	}
	
	public Integer validationMethod()
	{
	Integer returnValue = 0;
		if(condition1)
		{
			returnValue = 1;
			
		}
		else if(condition2)
		{
			returnValue = 2;
		}
		else
		{
			returnValue = 0;
		}
	}
}

 

trigger Trigger1 on YourObject__c (before insert) {

  if(Trigger.isBefore && Trigger.isInsert)
  {
	Class1 newC= new Class1();
	Integer validationId = newC.validationMethod();
	
	if(validationId == 1)
	{
	//Trigger.addError('your msg');
	Trigger.new[0].Name.addError('Your error msg1');
	}
	else if(validationId == 2)
	{
	//Trigger.addError('your msg');
	Trigger.new[0].Name.addError('Your error msg2');
	}
  }
  
  }

 

Hit the Kudos button if any post helps you - Mark the answer as solution, It might help others running to into similar problem in future.

This was selected as the best answer
Chamil MadusankaChamil Madusanka

f you got the answer from this post, Please hit the Kudos button

SF DEVSF DEV

Thanks Chamil.