I'm trying to write a trigger to display a error message on Quote Page, where in I'm facing an issue.
This is what I have written :
for(sObject obj: trigger.new)
{
Quote q = (Quote)obj;
Quote qOld = (Quote)Trigger.oldMap.get(q.ID);
if(u1.profile.name != 'SDS Integration Profile' && u1.profile.name != 'System Administrator'
&& q.Opportunity.Account.Block_Status_c = 'Block' && (q.Active__c = qOld.Active__c))
q.addError('________________');
}
Now Block_Status__c
is an Account Field which I'mm not able to use here in QuoteHandler Class.
How do I do this ?
Attribution to: user7395
Possible Suggestion/Solution #1
Assuming that your quotes are related to accounts, and you're wanting to load the Block_Status__c
field for the related accounts, you need to do the query yourself, related fields are not pulled into the trigger automatically.
You could add a formula field to pull the value onto the quote object, but if you need other fields as well then you're best off doing the query.
Pseudocode would be something like (assuming it's an update trigger since you're using oldMap
):
Map<Id, String> statuses = new Map<Id, String>();
for(Quote q : trigger.new)
{
statuses.Put(q.Id, null);
}
for(Quote q : [select Id, Opportunity.Account.Block_Status__c from Quote where Id in : statuses.keySet()])
{
status.put(q.Id, q.Opportunity.Account.Block_Status__c);
}
// now you have a map to use:
for(Quote q : trigger.new)
{
Quote qOld = Trigger.oldMap.get(q.ID);
if(u1.profile.name != 'SDS Integration Profile' && u1.profile.name != 'System Administrator'
&& statuses.get(q.id) = 'Block' && (q.Active__c = qOld.Active__c))
{
q.addError('________________');
}
}
Attribution to: Matt Lacey
Possible Suggestion/Solution #2
You cannot traverse relationships / access the field of the object referenced in the look up from the trigger.new context variable. You will have to query and get details of the look-up before using. So you can do something like this:
//Query the Opportunity.Account.Block_Status_c field and store it in a map.
map<id, quote> quoteMap = new map<id, quote>([SELECT id, Opportunity.Account.Block_Status_c from quote where id in : trigger.newMap.keySet()]);
for(sObject obj: trigger.new)
{
Quote q = (Quote)obj;
Quote qOld = (Quote)Trigger.oldMap.get(q.ID);
//Get the value from the map
if(u1.profile.name != 'SDS Integration Profile' && u1.profile.name != 'System Administrator'
&& quoteMap.get(q.Id).Opportunity.Account.Block_Status_c = 'Block' && (q.Active__c = qOld.Active__c))
q.addError('________________');
}
Attribution to: Sam
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/30322