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
dfiorentdfiorent 

Trigger to update a checkbox on Opportunity if Contact.mailing address is blank

I have no experience writing triggers and am looking for a sample that I can use to create my trigger.  Any examples would be appreciated.

 

When an Opportunity is being created or updated, I need to set a check box to true if the mailing address on the Contact is blank.

David81David81

You should be able to do this with a Workflow Rule + Field Update.

 

Workflow rule would be:

 

If formula evaluates to true

 

Formula =  ISBLANK(FIELD YOU WANT TO CHECK GOES HERE)

 

Then have it use a field update to set the checkbox.

incuGuSincuGuS

Can be accomplished in various ways, with a workflow rule as the above poster suggeted or a trigger.

 

 

 

trigger OpportunityCheckbox on Opportunity (before insert) {
    for(Opportunity opp: Trigger.new){
        If (opp.Contact == null || opp.Contact.MailingAddress == '' ){
           opp.Checkbox__c = true;
        }
    }
}

 

 

Just add whatever logic you need there, i havent compiled this its just to get you started :)

 

Hope this helps

Gaston.

dfiorentdfiorent

I am still a little confused - I would like to use the workflow rule and field update but I have a question:

 

I want to check the if the mailing address is blank on the contact when a new opportunity is created.  If it is, I want to set a check box to true on the Opportunity.   If do the workflow rule on mailing street,  and have it use the field update - how do I make sure this is only done when an Opportunity is created?

 

This is all new to me!

dfiorentdfiorent

I am trying to get the trigger to work now - how do I link Contact and Opportunity? Do I need to do a Select against OpportunityContactRole to get the contact id to select the contact record so I can check the address field for spaces?

David81David81

Which Contact in particular are you checking against?

 

Is there a lookup field on the Opp record or are you looking for a particular Contact related through Contact Roles?

dfiorentdfiorent

I am looking for the address on the Contact that is related to the Opportunity.  I do not have a lookup field on the Opportunity table, I would be looking through ContactRoles

David81David81

Ah, that does make it a bit more difficult. How will you be deciding which Contact to check if there are multiple Contacts listed?

dfiorentdfiorent

In our instance - an Opportunity will only have 1 related contact.

David81David81

If that is the case, then something like this should work. This isn't tested, so there may be typos.

 

 

trigger CheckContactAddress on Opportunity( before update,before insert ){

List<OpportunityContactRole> contactRoles = [SELECT OpportunityId,ContactId FROM OpportunityContactRole WHERE OpportunityId IN :trigger.new.keyset()];

if(contactRoles.size()>0){
Map<Id,Id> oppCon_map = new Map<Id,Id>(); for(OpportunityContactRole cr : contactRoles){ oppCon_map.put(cr.OpportunityId,cr.ContactId); } Map<Id,Contact> con_map = [SELECT Id,MailingStreet FROM Contact WHERE Id IN :oppCon_map.values();]; for(Opportunity o :trigger.new){ if(oppCon_map.containsKey(o.Id)){ if(con_map.get(oppCon_map.get(o.id)).MailingStreet!=''){ o.CheckBox__c=TRUE; } else{ o.CheckBox__c=FALSE; } } } }
}

 

 

dfiorentdfiorent

Thanks so much - I will give it a try!

David81David81

That code assumes just one Contact for each Opp. Probably wouldn't hurt to put some checks in there to ensure that there aren't multiple Contacts, just in case.

dfiorentdfiorent

Thank you for your help.  I made a few modifications and now have it working - partially.  It works for the before update, but not before insert.  Any suggestions?

 

 

 

trigger CheckContactAddress on Opportunity(before insert, before update)
{
   

        List<OpportunityContactRole> contactRoles = [SELECT OpportunityId,ContactId
                                                       FROM OpportunityContactRole
                                                      WHERE OpportunityId IN : Trigger.new];
   
        if(contactRoles.size()>0)
        {
            Map<Id,Id> oppCon_map = new Map<Id,Id>();
       
            for(OpportunityContactRole cr : contactRoles)
            {
                oppCon_map.put(cr.OpportunityId,cr.ContactId);
            }
       
            Map<Id,Contact> con_map = new Map<ID,Contact>([SELECT Id,MailingStreet
                                                             FROM Contact
                                                            WHERE Id IN : oppCon_map.values()]);
       
            for(Opportunity o : Trigger.new)
            {
      
                if(oppCon_map.containsKey(o.Id))
                {
                          
                     if(con_map.get(oppCon_map.get(o.id)).MailingStreet == null )
                     {
                    
                            o.Use_SW_Address_as_Ship_Bill_To__c = TRUE;
                     }
                     else
                     {
                     
                          o.Use_SW_Address_as_Ship_Bill_To__c = FALSE;
                     }
                }
            }
        }
    
 
}

David81David81

Now that I think about it, before insert doesn't make much sense, does it? You can't insert an Opp with a Contact Role. The contact role has to be created after the Opp is inserted. Ideal situation would be to setup a trigger on the OppContactRole, but that is not currently possible.

TinkuTinku

I have a similar requirement.

 

Accounts_to_be_assigned__c is a checkbox in accounts. And i need to write a trigger to check that checkbox when OpportunityStage='Won'.

 

any suggestions please. since this is a cross-object reference, i am confused on how and where to start.

sales4cesales4ce

Pls note that 1 Account can have multiple opportunities.

 

so, you mean if any one opportunity's stage is set to "won", you want that checkbox to be checked on the account?

 

Trigger is the way to do that . You can write a trigger on Opportunity for an update event that does the trick.

 

Let me know if this helped.

 

Sales4ce

Akanksha Gupta 77Akanksha Gupta 77
public class AccountIfContactIsNewAndRenewHandler {
    
    public static void isCheckAccount(List<Account>accList){
        for(Account a:accList){
            if(a.IsActive__c==true){
                a.addError('Cannot update isActive field now first create contacts');
            }
        }
    }
    
   public static void isCheckContact(List<Account>accList){
        System.debug('Trigger.new'+accList);
        Integer newStatus=0;
        Integer renewStatus=0;
        Set<Id> accId=new Set<Id>();
        
        for(Account a:accList){
            accId.add(a.Id);
        }
        System.debug('accList'+accList);
        System.debug('accId'+accId);
       // List<Contact> con=new List<Contact>[Select Id,Name Type__c From Contact Wh];
        map<Id,List<Contact>> accountContactMap = new map<id,List<Contact>>();
        
       List<Account> lstAccount = [SELECT Id,
                                          name,
                                          IsActive__c,
                                    (SELECT Id,
                                            Name,
                                            Type__c
                                     FROM   Contacts
                                     Where Type__c=:'New'
                                     Or Type__c=:'Renew')
                                    FROM   Account
                                    Where Id IN :accId];
        
        for(Account acc : lstAccount)
        {
            accountContactMap.put(acc.id, acc.Contacts);
        }
        System.debug(accountContactMap);
        
        for (List<Contact> outerList : accountContactMap.values())
        {
            for(Contact con:outerList){
                System.debug('Type'+con.Type__c);
                if(con.Type__c=='New'){
                    newStatus=newStatus+1;
                    System.debug('newStatus'+newStatus);
                }
                else if(con.Type__c=='Renew'){
                    renewStatus=renewStatus+1;
                    System.debug('renewStatus'+renewStatus);
                    
                }
            }
        }
        
        for(Account a : accList){
            if(a.IsActive__c==true){
                if(newStatus>0 && renewStatus>0)
                {
                    System.debug('You can update');
                    
                    a.IsActive__c=true;
                }
                else
                {
                    a.addError('You should have atleast 1 contact of type new and 1 of type renew');
                    a.IsActive__c=false;
                }
            }
        }
        
    }
    /*public static void isCheckContact(List<Account>accList){
        System.debug('Trigger.new'+accList);
        Integer newStatus=0;
        Integer renewStatus=0;
        Set<Id> accId=new Set<Id>();
        
        for(Account a:accList){
            accId.add(a.Id);
        }
        System.debug('accList'+accList);
        System.debug('accId'+accId);
        List<Account> lstAccount = [SELECT Id,
                                          name,
                                          IsActive__c,
                                    (SELECT Id,
                                            Name,
                                            Type__c
                                     FROM   Contacts
                                     Where Type__c=:'New'
                                     Or Type__c=:'Renew')
                                    FROM   Account
                                    Where Id IN :accId];
        
        
    }*/
    
} how to bulkify ths code
please anyone can help me