• Amit Kumar1
  • NEWBIE
  • 20 Points
  • Member since 2019
  • Student

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 5
    Replies
Hello all

i am trying to Design a Trigger where it should throw an error upon duplicate record creation, i know this can be achived with Duplicate management, but i have Custom Lookup field(User) which is not showing in the matching rules, Hence i am trying to write trigger, basically i have 3 Custom fields User(Assignedto__c ) which is 
Lookup(User) and other fields are 
Quater_Year__c which is picklist and Month (
Month__c) which is picklist field, 

Please Help with the trigger
trigger contactDuplicatePreventer on Contact(before insert, before update) 
{
	Set<String> setEmailID = new set<String>();
	Set<Id> setContID = new set<ID>();
    for (Contact Contact : System.Trigger.new) 
	{
        if ((Contact.Email != null) &&  (System.Trigger.isInsert ||  (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) 
		{
			setEmailID.add(Contact.Email);
			setContID.add(Contact.id);
		}
    }

	List<Contact> lstCOntact = [select id ,email from contact where email in :setEmailID and id not in :setContID ];
    Map<String, Contact> contactMap = new Map<String, Contact>();

	for(Contact cont : lstCOntact)
	{
		contactMap.put(cont.email, cont);
	}	

    for (Contact Contact : System.Trigger.new) 
	{
        if ((Contact.Email != null) &&  (System.Trigger.isInsert ||  (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) 
		{
			if(contactMap.containsKey(Contact.Email))
			{
				Contact.Email.addError('A Contact with this email address already exists.');
			}
		}	
	}	
}

 
Hello Everyone!

I have built an Apex Class and Scheduler Class in our FSB2 Full Sandbox Environment. The classes are intended to update all Open Opportunities that have a Days_Stale__c  > 30 and (Today_s_Date__c < TODAY () OR Today_s_Date__c = null) with a value of TODAY() in the Today_s_Date__c Opportunity field. 

When the Today_s_Date__c field is updated to TODAY() on the Opportunity record, it is intended to invoke a Process Builder that updates a "Reminder Date" field with a Date and sends an email to the Opportunity Owner advising the Opporunity is "stale" and needs to be updated.

Process Builder: Opportunity - Email Alerts for Stale Dated Opps 

Batch Apex Class: DailyStaleOppsProcessor
Batch Apex Test Class: DailyStaleOppsProcessorTest
Scheduled Apex Class: DailyStaleOppsscheduledBatchable
Scheduled Apex Test Class: DailyStaleOppsscheduledBatchableTest

When we run the Open Execute Anonymous Window to execute the DailyStaleOppsProcessor apex class, we eventually receive an error saying Apex CPU Time Limit exceeded. 

I have 100% Code Coverage on both classes. I am unsure of how to rewrite my class to stop receiving this error. Here's my code: 

global class DailyStaleOppsProcessor implements Database.Batchable<SObject>, Database.Stateful{
    
    
        List<Opportunity> listRecords = new List<Opportunity>();

    global Database.QueryLocator start (Database.BatchableContext BC)
    {
        String query = 'select id, today_s_date__c from opportunity where isclosed= false and (Days_Stale__c > 30) and (today_s_date__c < today or today_s_date__c = null)' ;
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope){

        for(Opportunity obj : (Opportunity []) scope) {
            
        if(obj.Today_s_Date__c!=date.today()){
            obj.Today_s_Date__c = System.today();
            listRecords.add(obj);
            }
         }
     
    }
     
    global void finish(Database.BatchableContext BC){
        system.debug('list to be deleted size  :: '+listRecords.size());
        if(!listRecords.isEmpty())
            {
              update listRecords;
            }

    }
}
I've followed step 2 step by step.  There should be two fields on th Battle Station Custom object (Project Status and Weapons Status).  I have only completed step 1, and when I go to verify step 2, it states I don't have the correct field count....
User-added image

Hi,

 

I am implementing a trigger in Accounts. If some conditions becomes true then I need to display an alert message to the user.

 

I searched in the discussion boards?? But I didnt find anything reg this.

 

Is there any way to achieve this?

 

If so then pls give some examples..

 

Thanks,

Hi,

Can you delete by Id or List<Id>?

I thought you could due to documentation below, but when I pass delete an Id or a List<Id> it says "DML requires SObject or SObject list type: Id."


Code:
delete sObject | Record.ID

delete sObject[] | LIST:ID[] 

 
Thanks,
Mike