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
Justin.WilliamsJustin.Williams 

Fill in account from case to the contact from quick create

When a service rep creates a case from an account they may create a contact by using the Quick Craete  button.  We know this quick create feature coes not allow the user to select an account.  I have a trigger that should grab the Account id and contact IDs on the case and add the Account on the case to the newly created contact.  I finally got past the errors but nw nothing happens.

trigger QuickContactUpdate on Case (after insert, after update) {
    String CPMCase = 'CPM Case';
    Map<String, Schema.RecordTypeInfo>  CaseType = new Map<String, Schema.RecordTypeInfo>();
    CaseType = Schema.SObjectType.Case.getRecordTypeInfosByName();
    String CaseTypeId = CaseType.get(CPMCase).getRecordTypeId();
        
    List<ID> CaseList = new list<ID>();
    for (Case c : Trigger.new) {
        IF(c.RecordTypeID == CaseTypeID && c.contact != NULL && c.account != NULL){
            CaseList.add(c.id);
        }
    }
    
    Map<string,string> ConID = New Map<string,string>();
    FOR(Case CaseConnect:[SELECT ContactID, AccountID FROM Case WHERE AccountID != NULL AND contact.AccountID = null AND ID IN :CaseList]){
        ConID.put(CaseConnect.ContactID,CaseConnect.AccountID);
    
        FOR(Contact ConCorrect:[SELECT ID, AccountID FROM Contact WHERE ID in :ConID.KeySet()]){
            ConCorrect.AccountID = ConID.get(ConCorrect.ID);
            UPDATE ConCorrect;
        }
    }
}

  Any thoughts on what I'm missing?

MikeGillMikeGill

I think you should be able to get to the same result without coding.

 

I would create a custom button which pops the actual contact page pre-populated with Account Id from Case - haven't tried it, but it seems like it would be possible.

 

This is a good thread to get going

 

https://success.salesforce.com/answers?id=90630000000giziAAA

Justin.WilliamsJustin.Williams

The problem with this is that it takes extra clicks to then go back to the case and lookup that user and then add them.  I have a high volume call center where every click matters.  Here is another code that still didn't work.

 

trigger QuickContactUpdate on Case (after insert, after update) {
    
    //This section sets the right record type to be used for later
    String CPMCase = 'CPM Case';
    Map<String, Schema.RecordTypeInfo>  CaseType = new Map<String, Schema.RecordTypeInfo>();
    CaseType = Schema.SObjectType.Case.getRecordTypeInfosByName();
    String CaseTypeId = CaseType.get(CPMCase).getRecordTypeId();
    
    
    String AccID = '';
    List<ID> tList = new list<ID>();
    List<ID> cList = new list<ID>();
    
    for (Case c1 : Trigger.new) {
        IF(c1.RecordTypeID == CaseTypeID && c1.contact != NULL && c1.contact.accountID == NULL && c1.account != NULL){
            tList.add(c1.id);
        }
    }
    
    IF(tList.size() > 0){
        FOR (CASE uCase: [SELECT accountID, contactID FROM Case WHERE id IN :tList]){
            cList.add(uCase.contactID);
            AccID = uCase.accountID;
            
            FOR(Contact uCon: [SELECT id, accountid FROM Contact WHERE id in :cList ]){
                uCon.accountid = AccID;
                Update uCon;
            }
        }    
    }
}

 

Justin.WilliamsJustin.Williams

I got the account to update the contact!!!!!  But now it does it every time.  I think it has something to do with how I check for a null value for the contact's account field (c1.contact.accountID==null) .

 

trigger QuickContactUpdate on Case (after insert, after update) {
    
    //This section sets the right record type to be used for later
    String CPMCase = 'CPM Case';
    Map<String, Schema.RecordTypeInfo>  CaseType = new Map<String, Schema.RecordTypeInfo>();
    CaseType = Schema.SObjectType.Case.getRecordTypeInfosByName();
    String CaseTypeId = CaseType.get(CPMCase).getRecordTypeId();
    
    
    String AccID = '';
    List<ID> tList = new list<ID>();
    List<ID> cList = new list<ID>();
    String ConID = '';
    
    for (Case c1 : Trigger.new) {
        IF(c1.RecordTypeID == CaseTypeID && c1.contactID != NULL && c1.contact.accountID==null && c1.accountID != NULL){
            tList.add(c1.id);
        }
    }
    
    IF(tList.size() > 0){
        FOR (CASE uCase: [SELECT accountID, contactID FROM Case WHERE id IN :tList]){
            AccID = uCase.accountID;
            ConID = uCase.contactID;
            
            FOR(Contact uCon: [SELECT id, leadsource, accountid FROM Contact WHERE id = :uCase.contactID ]){
                uCon.accountid = AccID;
                uCon.leadsource = 'other';
                Update uCon;
            }
        }    
    }
}