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
venkat bojjavenkat bojja 

Validation on Case

Hi Team,
I have a requirment.
I have a Case Objec having Lookup to Account And Asset.
After select Account on Case the selected Account related Assets only we need to select and save the case record. If the asset is not related to this seleected Account we need to throw the validation error like Please select the Selected Account related Asset and it should only applicable for recordtypename='service' on the Case object. Please help me on this ASASP.
                 Thanks in Advance.....

Thanks
Venkat.
 
Best Answer chosen by venkat bojja
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Venkat,

Can you try the validation rule as below.
 
AND(NOT(ISBLANK(AccountId)),NOT(ISBLANK( AssetId )), AccountId!= Asset.Account.Id )
If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Venkat,

Can you try the below trigger logic on Case object. Update the Asset__c field wirh your API name.
 
trigger CaseValidation on Case (before insert,before update) {
    
    set<Id> assetids= new set<id>();
    
    for (Case cs:Trigger.new){
        
        if(cs.accountid!=null && cs.Asset__c!=null){
        
            assetids.add(cs.Asset__c);
        }
    }
    
Map<id,Asset> mapassets=new map<id,Asset>([select id,Accountid from Asset where id in :assetids]);
  
    for(Case cs:Trigger.new){
        if(cs.AccountId!=null && cs.Asset__c!=null){
            if(cs.AccountId !=mapassets.get(cs.Asset__c).Accountid)
            cs.adderror('Account and Asset were not matching');
        }
        
    }

}

If this solution helps, Please mark it as best answer.

Thanks,
 
venkat bojjavenkat bojja
Hi Sai Praveen,
Thanks for your quick responce...
I don't want to go with trigger, need to wtrite a validatio rule for it on Case Object.

Thanks 
Venkat
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Venkat,

Can you try the validation rule as below.
 
AND(NOT(ISBLANK(AccountId)),NOT(ISBLANK( AssetId )), AccountId!= Asset.Account.Id )
If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
venkat bojjavenkat bojja
Hi Sai Praveen ,
It's not working ,Could you please once check it :
FYI: 

AND(NOT(ISBLANK(AccountId)),NOT(ISBLANK( AssetId )), RecordTypeId ='Service', AccountId!= Asset.Account.Id )

Thanks 
Venkat
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Venkat,

Use RecordType.Name instead of RecordTypeId. You need to specify id if you are using RecordTypeId.

Thanks,