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
MeerMeer 

Multiple if's

Hi,

 

I have an object 'Values' having the following fields:

 

Value Code(Name)[textbox]

Description(VSDesc__c)[textbox]

Parent(VSHead__c)[checkbox]

Parent Value(Parent_Value__c)[lookup with the filter, where VSHead__c = true]

Level(VSLevel__c)[numeric]

 

 

now, i simply want to maintain 3 Level hierarchy at the backend, i have written the following trigger: 

 

trigger SetValueLevels on Seg1__c (before insert)
{
FOR(Seg1__c val : trigger.new)
{
IF (val.Parent_Value__c == null && val.VSHead__c == true)
{val.VSLevel__c = 1;}
IF(val.Parent_Value__c !=null && val.Parent_Value__r.Parent_Value__c==null)
{val.VSLevel__c =2;}
ELSE
{val.VSLevel__c = 3;}
}
}

 

 It is working fine but the only issue is that it is not going for 3rd level.

 

Please help in this,  Hope everything is clear..

 

Thanks in Advance :)

Best Answer chosen by Admin (Salesforce Developers) 
MeerMeer

I added one formula field in the object with the following formula and it worked :)

 

IF(AND(VSHead__c =true, Parent_Value__c =null),1,IF(AND( Parent_Value__c <>null,Parent_Value__r.Parent_Value__c =null),2, 3))

All Answers

SeAlVaSeAlVa

Try the following

 

trigger SetValueLevels on Seg1__c (before insert){
	List<Seg1__c> allOfThem = [select Parent_Value__c, VSHead__c, Parent_Value__r.Parent_Value__c, VSLevel__c from Seg1__c where id in :trigger.newMap.keySet()];
	for(Seg1__c val : trigger.new){
		if (val.Parent_Value__c == null && val.VSHead__c == true){
			val.VSLevel__c = 1;
		}
		if(val.Parent_Value__c !=null && val.Parent_Value__r.Parent_Value__c==null){
			val.VSLevel__c =2;
		}
		else{
			val.VSLevel__c = 3;
		}
	}
}

 

Regards.

MeerMeer
At first I get the duplication error for Parent_Value__c in line 2,  so I remove one of the Parent_Value__c feild than i got the following error while saving the record
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger SetValueLevels caused an unexpected exception, contact your administrator: SetValueLevels: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.SetValueLevels: line 2,
column1
SeAlVaSeAlVa

(I have modified the code in my previous reply)

 

Can you quote the line where you got the null pointer exception?

DeepeshDeepesh

Why don't you try using a formula field instead?

MeerMeer

Still not working getting the following error

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger SetValueLevels caused an unexpected exception, contact your administrator: SetValueLevels: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.SetValueLevels: line 2, column 1

 

here it is mentioning the Null Pointer:

List<Seg1__c> allOfThem = [select Parent_Value__c, VSHead__c, Parent_Value__r.Parent_Value__c, VSLevel__c from Seg1__c where id in :trigger.newMap.keySet()];

 

 

actually i am very new to Salesforce so i dont know much of it, if u can guide me for Formula field I shall be very thankful to you

 

MeerMeer

I used the formula field having the following formula:

 

IF(Parent_Value__c=null ,1,  IF(AND(VSHead__c = true, Parent_Value__r.Parent_Value__c  <> null), 2,3) )

 

I am unable to go to second leve.

 

Please help

 

Thanks

MeerMeer

I added one formula field in the object with the following formula and it worked :)

 

IF(AND(VSHead__c =true, Parent_Value__c =null),1,IF(AND( Parent_Value__c <>null,Parent_Value__r.Parent_Value__c =null),2, 3))

This was selected as the best answer