Validation Rule salesforce - salesforce

I would love to have someone assist me with this task:
In the standard "Account" object, I need to create a Validation Rule that allows user to
change "Type" to "Customer – Direct" only if the account has at least 1 child
Opportunity in stage "Closed Won" (I can create whatever help fields/values
I need for this purpose).
thanks

Try the following code:
trigger AccountTrigger on Account (before update){
for(Account Acc: Trigger.New)
{
Opportunity opportunityData = [select id, AccountId, StageName from
Opportunity where StageName ='Closed Won'
And AccountId =:Acc.Id limit 1];
if(Acc.Type == 'Customer - Direct' && opportunityData == null)
{
Acc.addError('Enter Your Error Message');
}
}
}

Related

Update Parent Object Field in Apex Trigger

I have three objects
1)Truck__c
2)Booking__C
3)Payment___C.
Truck and Booking has master detail relationship where Truckk is master and Booking is detail.
Booking and payment has lookup relationship where Booking is parent and payment is child.
I want to update a field present isBooked(checkbox) in Truck based on the value present in a field remainingAmount in payment object. I have added trigger on Payment object like below
trigger TestTrigger on Payment__c (after insert) {
if(Trigger.isAfter && Trigger.isUpdate){
for (Payment__c pay:Trigger.New)
{
payId.add(pay.id);
paidAmount =Integer.valueOf(pay.Paid_Amount__c);
}
Map <id,Booking__c> bookingMap = new Map <id,Booking__c> ();
for (Booking__c obj: [Select Id, Booking_ID__c from Booking__c where Booking_ID__c in:payId])
{
bookingMap.put(obj.Booking_ID__c, obj);
}
for(Booking__c objBooking:matchingIdsMap){
Truck__c truckObj = [Select id,Price__c from Truck__c where Truck__c.id = objBooking.Truck__c.id];
//Integer paidAmount = Payment__c.Paid_Amount__c;
Integer totalAmount = Integer.valueOf(truckObj.Price__c);
Integer remainingAmount = totalAmount-paidAmount;
If(remainingAmount == 0){
truckObj.Booked__c = true
}
update truckObj;
}
}
}
Here first I am getting payment ID's and based on that I am fetching Booking objects which is lookup parent of payment. After this I am trying to fetch the truck object which is master of Booking. But I don't know how to query on this as where clause in query giving error
Truck__c truckObj = [Select id,Price__c from Truck__c where Truck__c.id = objBooking.Truck__c.id];
Please note there is no direct relationship between Truck and Payment
How can I fetch truck object
Thanks in Advance
Short answer: while referring to parent fields, you should use the relationship name, so Truck__r instead of Truck__c.
Anyway it is not the only problem with that code.
Long answer:
your trigger is in after insert, but you check for an after update event: if(Trigger.isAfter && Trigger.isUpdate). Probably you want to run this trigger in both after insert and after update.
You never declared payId nor paidAmount which you use in your first for-loop. Anyway paidAmount would just hold the last value, which probably you won't need.
[Select Id, Booking_ID__c from Booking__c where Booking_ID__c in:payId] this query should return an empty list because in payId you've stored the ids of Payment__c, that is a child of Booking__c, while in the first loop you should have stored the ids of parents Booking__c
[Select id,Price__c from Truck__c where Truck__c.id = objBooking.Truck__c.id] Here there is no reason to write where Truck__c.id It should be just WHERE Id = :objBooking.Truck__c.
Beware: putting a SOQL in a loop you will easily hit the Governor Limit about SOQL, which will raise a System.LimitException: Too many SOQL queries: 101.
The same goes by putting a DML in a loop.
I'm going to assume that the API Name of the lookup fields is the same of the parent object, so that a Booking__c field exists on Payment__c object and a Truck__c exists on Booking__c object.
If I got the logic right about how setting the flag on Truck object, this should be the trigger code.
trigger TestTrigger on Payment__c (after insert, after update) {
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {
Map<Id, List<Payment__c>> mapBookingIdPaymentList = new Map<Id, List<Payment__c>>();
for (Payment__c pay : Trigger.New) {
List<Payment__c> paymentList = mapBookingIdPaymentList.get(pay.Booking__c);
if (paymentList == null) {
paymentList = new List<Payment__c>();
mapBookingIdPaymentList.put(pay.Booking__c, paymentList);
}
paymentList.add(pay);
}
Map<Id, Decimal> mapTruckPrice = new Map<Id, Decimal>();
Map<Id, Integer> mapTruckRemainingAmount = new Map<Id, Integer>();
for (Booking__c booking: [SELECT Id, Truck__c, Truck__r.Price__c FROM Booking__c WHERE Id IN :mapBookingIdPaymentList.keySet()]) {
mapTruckPrice.put(booking.Truck__c, booking.Truck__r.Price__c);
Integer sumOfRemainingAmount = mapTruckRemainingAmount.containsKey(booking.Truck__c) ? mapTruckRemainingAmount.get(booking.Truck__c) : 0;
for (Payment__c pay : mapBookingIdPaymentList.get(booking.Id)) {
sumOfRemainingAmount += pay.Paid_Amount__c != null ? pay.Paid_Amount__c.intValue() : 0;
}
mapTruckRemainingAmount.put(booking.Truck__c, sumOfRemainingAmount);
}
List<Truck__c> trucksToUpdate = new List<Truck__c>();
for (Id truckId : mapTruckPrice.keySet()) {
// There is no need to query a record just to update it if you already have its Id.
Truck__c truck = new Truck__c(Id = truckId);
truck.Booked__c = mapTruckPrice.get(truckId) - mapTruckRemainingAmount.get(truckId) == 0;
trucksToUpdate.add(truck);
}
update trucksToUpdate; // dml outside the loop
}
}
By the way, you should move the logic in an handler class, following the best practices.

Avoiding Hard Coding in Apex following Best Practices

In reference to the best practice #10 avoiding hard coding IDs, is my code considered to follow best practice without using Lists or Maps ??
Best Practices Link
trigger RecordTypeTester on Account (before update) {
for (Account acc: trigger.new)
{
if(acc.RecordTypeId == [SELECT Id from RecordType WHERE sObjectType = 'Account' AND IsActive = True AND Name = 'Health Care' LIMIT 1].Id)
{
//some code
}
else if (acc.RecordTypeId == [SELECT Id from RecordType WHERE sObjectType = 'Account' AND IsActive = True AND Name = 'Hi-Tech' LIMIT 1].Id)
{
//some code
}
}
}
Initialize constant first
public final static Id INVOICE_SUMMARY_RECORD_TYPE_ID = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Invoice Summary').getRecordTypeId();
and then, use it in a method
if (oppItem.RecordTypeId != INVOICE_SUMMARY_RECORD_TYPE_ID)

Need Help in displaying Error Message on Same page

I have craeted a trigger which will Fire on update on Field on campaign and restrict the user from leaving the Comments field blank upon rejecting the record.
Here is my trigger code:
trigger RequireRejectionComment on Campaign (before update)
{
Map<Id, Campaign > rejectedStatements
= new Map<Id, Campaign>{};
for(Campaign inv: trigger.new)
{
Campaign oldInv = System.Trigger.oldMap.get(inv.Id);
if ((oldInv.BR_ApprovalStatusRegulatory__c != 'Reprovado'
&& inv.BR_ApprovalStatusRegulatory__c == 'Reprovado')||
(oldInv.BR_ApprovalStatusLegal__c!= 'Reprovado' &&
inv.BR_ApprovalStatusLegal__c== 'Reprovado') )
{
rejectedStatements.put(inv.Id, inv);
}
}
if (!rejectedStatements.isEmpty())
{
List<Id> processInstanceIds = new List<Id>{};
for (Campaign invs : [SELECT (SELECT ID
FROM ProcessInstances
ORDER BY CreatedDate DESC
LIMIT 1)
FROM Campaign
WHERE ID IN :rejectedStatements.keySet()])
{
processInstanceIds.add(invs.ProcessInstances[0].Id);
}
// Now that we have the most recent process instances, we can check
// the most recent process steps for comments.
for (ProcessInstance pi : [SELECT TargetObjectId,
(SELECT Id, StepStatus, Comments
FROM Steps
ORDER BY CreatedDate DESC
LIMIT 1 )
FROM ProcessInstance
WHERE Id IN :processInstanceIds
ORDER BY CreatedDate DESC])
{
if ((pi.Steps[0].Comments == null ||
pi.Steps[0].Comments.trim().length() == 0))
{
Trigger.new[0].parentId.addError(' My error Message ');
//rejectedStatements.get(pi.TargetObjectId).addError(
// ' My error Message');
}
}
}
}
This trigger works fine but it displays an error message on new page..
My requirement: Error message should appear on record or the same page, while rejecting the record.
Please suggest,thanks..
Have you tried creating the validation within the apex page itself?
You can set the page item to have a validation that doesn't allow the page to be submitted if a specified page item is NULL.
Look for the validations section within the page-processing section when editing your page.
Here is a link to documentation regarding validations:
https://docs.oracle.com/database/121/HTMDB/bldr_validate.htm#HTMDB28931

Apex Trigger-Salesforce before insert

I have two custom objects 1. Customer 2.Complaint and they are lookup relationship with each other.
I have problem like that the below trigger is not working.
My requirement is like that before inserting complaint, first it will check email id and contact number and then complaint will register.
trigger Demo on Complaint__c (before insert) {
Set<Id> customerIds = new Set<Id>();
for (Shan__Complaint__c complaint : Trigger.new) {
customerIds.add(complaint.Shan__customer__c);
}
Map<String, Shan__cust__c> customers =
new Map<String, Shan__cust__c>([SELECT Shan__cust_contact__c, Shan__cust_email__c
FROM Shan__cust__c WHERE id IN: customerIds]);
for (Shan__Complaint__c complaint : Trigger.new) {
Shan__cust__c customer = customers.get(complaint.Shan__customer__c);
if (customer == null || complaint.Shan__E_mail__c == customer.Shan__cust_email__c
&& complaint.Shan__Phone_Number__c == customer.Shan__cust_contact__c) {
complaint.adderror('Your phone and Email does not exists in out database ');
}
}
}
This trigger should not be compiled at all.
You should see error
Loop variable must be of type Complaint__c
Trigger.new is a List<Complaint__c>
So, there are 2 errors:
for (Shan__Complaint__c complaint : Trigger.new) {
customerIds.add(complaint.Shan__customer__c);
...
for (Shan__Complaint__c complaint : Trigger.new) {
Shan__cust__c customer = customers.get(complaint.Shan__customer__c);
...

Initialise object in apex with related data

I require a separate object which contains the related fields of a child object.
Currently I do it like this:
Opportunity opp = [SELECT Id, Name, Account.Id, Account.Name FROM Opportunity LIMIT 1];
Account acc = new Account(
Id = opp.Account.Id,
Name = opp.Account.Name
);
When working with a large related object I have to initialise many more fields than this and the script becomes very large and ugly.
What is the quickest way to initialise the related field data into a separate object?
You must define the all fields in your SOQL query (mor info here).
But it is not necessary if you want to clone the object:
Opportunity opp = [SELECT Account.Id, Account.Name
FROM Opportunity
LIMIT 1];
Account acc = opp.Account;
Example with Custom Object:
Contract__c c = [ Select Account__r.FirstName
From Contract__c
Where Account__r.FirstName != null
Limit 1];
Account a = c.Account__r;

Resources