You need to sign in to do that
Don't have an account?
chaitanya sfdc
How to compare Trigger.new values with Trigger.old values
Hi,
I am new to Salesforce. I need to write a trigger to show an error messages by comparing old values. Pls. see below detailed.
I have a junction object 'Related Products' which has master detail relationship with 'Contact' and have a lookup relationship with 'Product'. And I have added a related list of 'Related Products' in Contact detail page where user can create new Related Product from the Contact and can add multiple Products to it.
Now my requirment is "if any user selects a product which is already selected to that contact should through error".
For example, I have contact called "Test Contact111", In it I have created one Related Product "RP-0001" and selected a Product called "Prod111", It will get to save and will show in the Contact's detail page under 'Related Products' section.
Again from the same Contact I will create another Related Product "RP-0002" and again will select the Same Product "Prod111", now the trigger need to throw an error states "Prod111 is already selected"
Pls. help me on this.
Thanks in Advance :).
I am new to Salesforce. I need to write a trigger to show an error messages by comparing old values. Pls. see below detailed.
I have a junction object 'Related Products' which has master detail relationship with 'Contact' and have a lookup relationship with 'Product'. And I have added a related list of 'Related Products' in Contact detail page where user can create new Related Product from the Contact and can add multiple Products to it.
Now my requirment is "if any user selects a product which is already selected to that contact should through error".
For example, I have contact called "Test Contact111", In it I have created one Related Product "RP-0001" and selected a Product called "Prod111", It will get to save and will show in the Contact's detail page under 'Related Products' section.
Again from the same Contact I will create another Related Product "RP-0002" and again will select the Same Product "Prod111", now the trigger need to throw an error states "Prod111 is already selected"
Pls. help me on this.
Thanks in Advance :).
{
Set<Id> setProductId = new Set<Id>();
Set<Id> setOfProductHaveRelation = new Set<Id>();
for (RelatedProduct__c objRelatedProduct : Trigger.New)
{
setProductId.add(objRelatedProduct.Product__c);
}
for (RelatedProduct__c objRelatedProduct :[SELECT Id, Product__c FROM RelatedProduct__c WHERE Product__c IN: setProductId])
{
setOfProductHaveRelation.add(objRelatedProduct.Product__c);
}
for (RelatedProduct__c objRelatedProduct : Trigger.New)
{
if(!setOfProductHaveRelation.contains(objRelatedProduct.Product__c )) continue;
objRelatedProduct.addError('This Product already have the Relation.');
}
}
All Answers
{
Set<Id> setProductId = new Set<Id>();
Set<Id> setOfProductHaveRelation = new Set<Id>();
for (RelatedProduct__c objRelatedProduct : Trigger.New)
{
setProductId.add(objRelatedProduct.Product__c);
}
for (RelatedProduct__c objRelatedProduct :[SELECT Id, Product__c FROM RelatedProduct__c WHERE Product__c IN: setProductId])
{
setOfProductHaveRelation.add(objRelatedProduct.Product__c);
}
for (RelatedProduct__c objRelatedProduct : Trigger.New)
{
if(!setOfProductHaveRelation.contains(objRelatedProduct.Product__c )) continue;
objRelatedProduct.addError('This Product already have the Relation.');
}
}
Thank you for your valuable replies. It's working perfectly. thank you very much to both of you.
set<id>oppid = new set<id>();
for(Opportunity op: trigger.new){
if(op.id!= null)
{
oppid.add(op.id);
}
}
system.debug('Opportunity'+oppid);
list<Opportunity>opp = [select id,Name,CloseDate,StageName from Opportunity where StageName!=null Order By lastmodifiedDate DESC limit 1];
// system.debug('Opportunitylist'+opp);
if(trigger.isbefore ){
for(Opportunity opt: trigger.new){
if(opt.StageName=='Closed Won'|| opt.StageName=='Needs Analysis' || opt.StageName=='Proposal/Price Quote'
|| opt.StageName=='Negotiation/Review' || opt.StageName=='Closed Lost' || opt.StageName=='Qualification' )
{
opt.adderror('choose Prospecting first');
}
}
}
// map<id,Opportunity>mapopp = [select id,Name,CloseDate,StageName from Opportunity where StageName!=null Order By lastmodifiedDate DESC limit 1];
if(trigger.isAfter && trigger.isupdate){
for(Opportunity opt: trigger.new){
/* if(Trigger.oldmap.get(opt.id).StageName != Trigger.newmap.get(opt.id).StageName && Trigger.newmap.get(opt.id).StageName == 'Closed Lost') {
opt.adderror('ClosedLost=null');
}
if(Trigger.oldmap.get(opt.id).StageName == 'Closed Won' && Trigger.newmap.get(opt.id).StageName == 'Closed Lost') {
opt.adderror('Cannot change');
} */
if(Trigger.oldmap.get(opt.id).StageName == 'Closed Won'){
if(Trigger.oldmap.get(opt.id).StageName == 'Closed Won' && Trigger.newmap.get(opt.id).StageName == 'Closed Lost' ||
Trigger.newmap.get(opt.id).StageName == 'Negotiation/Review' || Trigger.newmap.get(opt.id).StageName == 'Qualification' ||
Trigger.newmap.get(opt.id).StageName == 'Proposal/Price Quote' || Trigger.newmap.get(opt.id).StageName == 'Prospecting'
|| Trigger.newmap.get(opt.id).StageName == 'Needs Analysis' ) {
opt.adderror('Cannot Decrease ');
}
}
if(Trigger.oldmap.get(opt.id).StageName == 'Negotiation/Review'){
if(Trigger.oldmap.get(opt.id).StageName == 'Negotiation/Review' && Trigger.newmap.get(opt.id).StageName == 'Closed Lost'
|| Trigger.newmap.get(opt.id).StageName == 'Qualification' ||
Trigger.newmap.get(opt.id).StageName == 'Proposal/Price Quote' || Trigger.newmap.get(opt.id).StageName == 'Prospecting'
|| Trigger.newmap.get(opt.id).StageName == 'Needs Analysis' ) {
opt.adderror('Cannot change Negotiation');
}
}
if(Trigger.oldmap.get(opt.id).StageName == 'Proposal/Price Quote'){
if(Trigger.oldmap.get(opt.id).StageName == 'Proposal/Price Quote' && Trigger.newmap.get(opt.id).StageName == 'Closed Lost'
|| Trigger.newmap.get(opt.id).StageName == 'Qualification' ||
Trigger.newmap.get(opt.id).StageName == 'Prospecting'
|| Trigger.newmap.get(opt.id).StageName == 'Needs Analysis' ) {
opt.adderror('Cannot change Proposal/Price Quote');
}
if(Trigger.oldmap.get(opt.id).StageName == 'Proposal/Price Quote' && Trigger.newmap.get(opt.id).StageName == 'Closed Won') {
opt.adderror('Select Negotiation/Review ');
}
}
if(Trigger.oldmap.get(opt.id).StageName == 'Needs Analysis'){
if(Trigger.oldmap.get(opt.id).StageName == 'Needs Analysis' && Trigger.newmap.get(opt.id).StageName == 'Closed Lost'
|| Trigger.newmap.get(opt.id).StageName == 'Qualification' ||
Trigger.newmap.get(opt.id).StageName == 'Prospecting'
) {
opt.adderror('Cannot change Needs Analysis');
}
if(Trigger.oldmap.get(opt.id).StageName == 'Needs Analysis' && Trigger.newmap.get(opt.id).StageName == 'Closed Won' ||
Trigger.newmap.get(opt.id).StageName == 'Negotiation/Review') {
opt.adderror('Select Proposal/Price Quote ');
}
}
if(Trigger.oldmap.get(opt.id).StageName == 'Qualification'){
if(Trigger.oldmap.get(opt.id).StageName == 'Qualification' && Trigger.newmap.get(opt.id).StageName == 'Closed Lost'
|| Trigger.newmap.get(opt.id).StageName == 'Prospecting' )
{
opt.adderror('Cannot change Qualification');
}
if(Trigger.oldmap.get(opt.id).StageName == 'Qualification' && Trigger.newmap.get(opt.id).StageName == 'Proposal/Price Quote'
|| Trigger.newmap.get(opt.id).StageName == 'Closed Won' || Trigger.newmap.get(opt.id).StageName == 'Negotiation/Review'
) {
opt.adderror('select Needs Analysis ');
}
}
if(Trigger.oldmap.get(opt.id).StageName == 'Prospecting'){
if(Trigger.oldmap.get(opt.id).StageName =='Prospecting' && Trigger.newmap.get(opt.id).StageName == 'Proposal/Price Quote'
|| Trigger.newmap.get(opt.id).StageName == 'Closed Won' || Trigger.newmap.get(opt.id).StageName == 'Negotiation/Review'
|| Trigger.newmap.get(opt.id).StageName == 'Needs Analysis' )
{
opt.adderror(' select Qualification');
}
if(Trigger.oldmap.get(opt.id).StageName == 'Prospecting' && Trigger.newmap.get(opt.id).StageName == 'Closed Lost' ) {
opt.adderror('Cannot change Prospecting ');
}
}
/* if(Trigger.oldmap.get(opt.id).StageName == 'Prospecting'){
if(Trigger.oldmap.get(opt.id).StageName == 'Prospecting' && Trigger.newmap.get(opt.id).StageName == 'Closed Lost'||
Trigger.newmap.get(opt.id).StageName == 'Closed Won' ||
Trigger.newmap.get(opt.id).StageName == 'Negotiation/Review' || Trigger.newmap.get(opt.id).StageName == 'Qualification' ||
Trigger.newmap.get(opt.id).StageName == 'Proposal/Price Quote' || Trigger.newmap.get(opt.id).StageName == 'Needs Analysis' ) {
opt.adderror('Cannot change Prospecting');
}
} */
}
}
}