Apex trigger to add dr. Salutation on lead record. - Salesforce
Dharmik
Handler class:
public class LeadsTriggerHandler {
public static void createLead(List<Lead> lstLead)
{
for( Lead ldcreate: lstLead)
{
if( ldcreate.Is_Doctor__c == True)
{
ldcreate.Salutation = 'Dr.';
}
}
}
}
trigger LeadsTrigger on Lead (before insert , before update) {
LeadsTriggerHandler.createLead(Trigger.new);
}
public class LeadsTriggerHandler {
public static void createLead(List<Lead> lstLead)
{
for( Lead ldcreate: lstLead)
{
if( ldcreate.Is_Doctor__c == True)
{
ldcreate.Salutation = 'Dr.';
}
}
}
}
Trigger:
trigger LeadsTrigger on Lead (before insert , before update) {
LeadsTriggerHandler.createLead(Trigger.new);
}
4:06 AM
Apex
,
apex class
,
Apex trigger
,
Salesforce
Trigger to avoid recursive trigger call on contact object (create contact) - Salesforce
Dharmik
Handler class:
public class ContactInsertHandler { public static void createContact() { Contact cont = new Contact(); cont.LastName = 'Dharmik'; insert cont; } private static boolean run = true; public static boolean runOnce(){ if(run) { run=false; return true; } else { return run; } } } |
Trigger:
trigger InsertContact on Contact (after insert) {
if(ContactInsertHandler.runOnce())
{
ContactInsertHandler.createContact();
}
}
if(ContactInsertHandler.runOnce())
{
ContactInsertHandler.createContact();
}
}
3:42 AM
Apex
,
apex class
,
Apex trigger
,
Salesforce
Apex trigger to sum up (Count) amount from all the related Opportunity and set in custom field.
Dharmik
Handler class:
public class CustomRollUp {
public static void docount()
{
list<Opportunity> setopportunity = [ select Id, Name, Amount from Opportunity where
StageName = 'Closed Won' and RecordTypeId = '0127F00000166iGQAQ' ];
Double j = 0;
for( Opportunity op : setopportunity)
{
j += op.Amount;
}
list<Fund__c> updaterecord = new list<Fund__c>();
for (Fund__c f1 : [ select id from Fund__c ] )
{
f1.Total_opportunity_amount__c = j;
updaterecord.add(f1);
}
update updaterecord;
}
}
Trigger:
trigger CustomRollUp on Opportunity ( after insert , after update, after delete , after undelete) {
CustomRollUp.docount();
}
public class CustomRollUp {
public static void docount()
{
list<Opportunity> setopportunity = [ select Id, Name, Amount from Opportunity where
StageName = 'Closed Won' and RecordTypeId = '0127F00000166iGQAQ' ];
Double j = 0;
for( Opportunity op : setopportunity)
{
j += op.Amount;
}
list<Fund__c> updaterecord = new list<Fund__c>();
for (Fund__c f1 : [ select id from Fund__c ] )
{
f1.Total_opportunity_amount__c = j;
updaterecord.add(f1);
}
update updaterecord;
}
}
Trigger:
trigger CustomRollUp on Opportunity ( after insert , after update, after delete , after undelete) {
CustomRollUp.docount();
}
3:33 AM
Apex
,
apex class
,
Apex trigger
,
Salesforce
Apex trigger to set contact record detail same as account details - salesforce (i.e contact lastname = account lastname)
Dharmik
Handler class:
public class AccountTrigger {
public static void createContact(List<Account> accounts) {
List<Contact> contact = new List<Contact>();
for(Account acc : accounts){
Contact con = new Contact(LastName = acc.name,
AccountId=acc.id,
MailingStreet=acc.BillingStreet,
MailingCity=acc.BillingCity,
MailingState=acc.BillingState,
MailingPostalCode=acc.BillingPostalCode,
MailingCountry=acc.BillingCountry,
Fax=acc.Fax);
contact.add(con);
}
insert contact;
}
}
public class AccountTrigger {
public static void createContact(List<Account> accounts) {
List<Contact> contact = new List<Contact>();
for(Account acc : accounts){
Contact con = new Contact(LastName = acc.name,
AccountId=acc.id,
MailingStreet=acc.BillingStreet,
MailingCity=acc.BillingCity,
MailingState=acc.BillingState,
MailingPostalCode=acc.BillingPostalCode,
MailingCountry=acc.BillingCountry,
Fax=acc.Fax);
contact.add(con);
}
insert contact;
}
}
Trigger:
trigger AccountTrigger on Account (after insert , after update ) {
AccountTriggerHandler.createContact(Trigger.new);
}
AccountTriggerHandler.createContact(Trigger.new);
}
3:27 AM
Apex
,
apex class
,
Apex trigger
,
Salesforce
Apex trigger to show error message if sum of Opportunity amount is greater then 100000 of logged in user.
Dharmik
Trigger:
trigger LimitTotalAmountInOpportunity on Opportunity (before insert) {
LimitTotalAmountInOpportunityHandler.LimitAmount(Trigger.new);
}
Handler Class:
public class LimitTotalAmountInOpportunityHandler {
public static void LimitAmount(list<Opportunity> OpportunityList)
{
double totalamount = 0;
for(opportunity objopportunity : [select Amount from opportunity where createdDate = today and createdById =: UserInfo.getUserId()]){
if(objopportunity.amount != null)
totalamount += objopportunity.amount;
}
for(opportunity objopportunity : OpportunityList){
if(objopportunity.amount != null)
totalamount += objopportunity.amount;
if(totalAmount > 100000){
objopportunity.addError('Total Amount Limit Exceded');
}
}
}
}
trigger LimitTotalAmountInOpportunity on Opportunity (before insert) {
LimitTotalAmountInOpportunityHandler.LimitAmount(Trigger.new);
}
Handler Class:
public static void LimitAmount(list<Opportunity> OpportunityList)
{
double totalamount = 0;
for(opportunity objopportunity : [select Amount from opportunity where createdDate = today and createdById =: UserInfo.getUserId()]){
if(objopportunity.amount != null)
totalamount += objopportunity.amount;
}
for(opportunity objopportunity : OpportunityList){
if(objopportunity.amount != null)
totalamount += objopportunity.amount;
if(totalAmount > 100000){
objopportunity.addError('Total Amount Limit Exceded');
}
}
}
}
12:25 AM
Apex
,
apex class
,
Apex trigger
,
Salesforce
Subscribe to:
Posts
(
Atom
)