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
Salesforce Admin 110Salesforce Admin 110 

stop trigger firing anytime source record is edited

i need to stop this trigger firing everytime contact record is edited
here is trigger:


trigger newoppty on contact (after insert, after update) {

     
    
    list<opportunity> opplist = new list<opportunity>();
    
    for (contact con : trigger.new) {

        if(con.Stage__c == 'Lead' && con.contacttype__C =='Seller') {
            opportunity newopp = new opportunity ();
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.accountid = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stagename = 'Lead';
            newopp.recordtypeid = '012250000000Jev';
            newopp.contact__c = con.id;
            newopp.closedate = Date.today().addDays(7);
            newopp.PriceBook2Id = '01s250000005Du6';
            
            

            opplist.add(newopp);

}
}


        try {
        insert opplist;
       OpportunityLineItem[] lines = new OpportunityLineItem[0];
PricebookEntry[] entry = [SELECT Id, Name, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = '01s250000005Du6' AND name In ('Market Appraisal')];
for(Opportunity record: opplist) {
    for (PricebookEntry thisentry : entry)
        lines.add(new OpportunityLineItem(PricebookEntryId=thisentry.Id, OpportunityId=record.Id, UnitPrice=thisentry.UnitPrice, Quantity=1));
}
        insert lines;     
} catch (system.dmlexception e) {
system.debug (e);
}

    
    }
RAM AnisettiRAM Anisetti
try to see this one...
 
trigger newoppty on contact (after insert, after update) {

     
    
    list<opportunity> opplist = new list<opportunity>();
    public boolean checkedited=false;
    for (contact con : trigger.new) {
	
	 Contact oldcon = Trigger.oldMap.get(con.Id);
	 
	 if(oldcon.source!=con.source){
	   checkedited=true;
	 
	 }

        if(con.Stage__c == 'Lead' && con.contacttype__C =='Seller' && checkedited==false) {
            opportunity newopp = new opportunity ();
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.accountid = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stagename = 'Lead';
            newopp.recordtypeid = '012250000000Jev';
            newopp.contact__c = con.id;
            newopp.closedate = Date.today().addDays(7);
            newopp.PriceBook2Id = '01s250000005Du6';
            
            

            opplist.add(newopp);

}
}
      if(opplist.size()>0 && checkedited==false){

        try {
        insert opplist;
       OpportunityLineItem[] lines = new OpportunityLineItem[0];
PricebookEntry[] entry = [SELECT Id, Name, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = '01s250000005Du6' AND name In ('Market Appraisal')];
for(Opportunity record: opplist) {
    for (PricebookEntry thisentry : entry)
        lines.add(new OpportunityLineItem(PricebookEntryId=thisentry.Id, OpportunityId=record.Id, UnitPrice=thisentry.UnitPrice, Quantity=1));
}
        insert lines;     
} catch (system.dmlexception e) {
system.debug (e);
}

}

 }

 
RAM AnisettiRAM Anisetti
ur asking about total contact record or source field in contact..
if it is field,the above one is works

or

if you want to stop contact record,follow below one

just remove after update event in trigger definations...
 
trigger newoppty on contact (after insert) {

     
    
    list<opportunity> opplist = new list<opportunity>();
    
    for (contact con : trigger.new) {

        if(con.Stage__c == 'Lead' && con.contacttype__C =='Seller') {
            opportunity newopp = new opportunity ();
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.accountid = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stagename = 'Lead';
            newopp.recordtypeid = '012250000000Jev';
            newopp.contact__c = con.id;
            newopp.closedate = Date.today().addDays(7);
            newopp.PriceBook2Id = '01s250000005Du6';
            
            

            opplist.add(newopp);

}
}


        try {
        insert opplist;
       OpportunityLineItem[] lines = new OpportunityLineItem[0];
PricebookEntry[] entry = [SELECT Id, Name, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = '01s250000005Du6' AND name In ('Market Appraisal')];
for(Opportunity record: opplist) {
    for (PricebookEntry thisentry : entry)
        lines.add(new OpportunityLineItem(PricebookEntryId=thisentry.Id, OpportunityId=record.Id, UnitPrice=thisentry.UnitPrice, Quantity=1));
}
        insert lines;     
} catch (system.dmlexception e) {
system.debug (e);
}

    
    }

 
Salesforce Admin 110Salesforce Admin 110
hi RAM, thanks for your reply, i tried your code then clicked edit save on the contact record and new "duplicate" opp was created........this is what i want to prevent....if the contact record is edited for example phone number and then saved the trigger still creates a new opportunity even if one is already there......can you help further?
Vishal Negandhi 16Vishal Negandhi 16
So what is the criteria? Should a Contact only have one opportunity?
In that case, just have a trigger on insert and nothing to be done on updates.

Or if you want a new Opportunity every time a certain value is updated on Contact, you need to point that out.
RAM AnisettiRAM Anisetti
Let me know r u remove after update event in trigger??
please confirm.