• Projjwal Lahiri
  • NEWBIE
  • -1 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 7
    Replies
It is working fine from any browser, even in lightning also. But is is totally not working from salesforce1 app. 
Please suggest many any solution for this.
Hi,
I have  written one trigger and two classes which is working fine in sandbox but in Production I am getting the below error:

Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Account: execution of AfterInsert caused by: System.DmlException: Upsert failed. First exception on row 0; first error: DUPLICATES_DETECTED, You're creating a duplicate record. We recommend you use an existing record instead.: [] Class.AccountServices.associateContacts: line 36, column 1 Class.AccountTriggerHandler.afterInsert: line 12, column 1 Trigger.Account: line 6, column 1: []
Error is in expression '{!createopportunity}' in component <apex:page> in page lead_convert: Class.Leadconvert.createopportunity: line 31, column 1

 
<Account Services>

public with sharing class AccountServices {
    public static List<Account> hasEmailChanged
        (List<Account> newRecords, Map<Id, Account> oldMap)
    {
        List<Account> changed = new List<Account>();
        for (Account account : newRecords)
        {
            if (account.Primary_Contact_Email__c != oldMap.get(account.Id).Primary_Contact_Email__c)
                changed.add(account);
        }
        return changed;
    }

    public static void associateContacts(List<Account> accounts)
    {
        Set<String> addresses = new Set<String>();
        for (Account account : accounts) addresses.add(account.Primary_Contact_Email__c);
        // now you know all the email addresses
        // you can use this collection to get every Contact in one query

        Map<String, Contact> emailToContact = new Map<String, Contact>();
        for (Contact contact : [
            SELECT Email FROM Contact WHERE Email IN :addresses
        ])
        {
            emailToContact.put(contact.Email, contact);
        }
        //now you can find a Contact by its email address

        for (Account account : accounts)
        {
            String address = account.Primary_Contact_Email__c;
            Contact contact = buildContact(account, emailToContact.get(address));
            emailToContact.put(address, contact);
        }
        upsert emailToContact.values();
        // you should really wrap this in a try/catch
        // I'll leave that code up to you
    }
    static Contact buildContact(Account account, Contact existing)
    {
        Contact contact = new Contact(
            AccountId = account.Id,
            Email = account.Primary_Contact_Email__c,
            LastName=account.Name,
            FirstName='',
            Salutation='',
            MobilePhone=account.Phone,
            PB_Contact_Type__c='Primary'
            
        );
        if (existing != null) contact.Id = existing.Id;
        return contact;
    }
}


<Account TriggerHandler>

public with sharing class AccountTriggerHandler {
    final List<Account> newRecords;
    final Map<Id, Account> oldMap;
    public AccountTriggerHandler(List<Account> newRecords, Map<Id, Account> oldMap)
    {
        this.newRecords = newRecords;
        this.oldMap = oldMap;
    }

    public void afterInsert()
    {
        AccountServices.associateContacts(newRecords);
    }

    public void afterUpdate()
    {
        AccountServices.associateContacts(AccountServices.hasEmailChanged(newRecords, oldMap));
    }

}


<Trigger>

trigger Account on Account (after insert, after update)
{
    AccountTriggerHandler handle = new AccountTriggerHandler(trigger.new, trigger.oldMap);
    if (trigger.isAfter)
    {
        if (trigger.isInsert) handle.afterInsert();
        if (trigger.isUpdate) handle.afterUpdate();
    }
}

Please suggest any solution
 
I am using standard Opportunity and I am creating an Account once the Opportunity stage "Closed Won" using a trigger. But after creating the Account I want to update the Account field in the Opportunity with the newly created Account (means I want to link this Opportunity with the newly created Account). Please suggest me as soon as possible how to do this ?
(Very Urgent)
Bellow trigger I have written to create an Account once Opportunity "Closed Own"

trigger UpdateAccount on Opportunity (after insert, after update) {
    
    List <Account> acc = new List <Account>();
  
        for(Opportunity opp : Trigger.new)
        {
                if(opp.Name!=NULL && opp.AccountId!=NULL && opp.PB_Customer_Group__c=='First Time User' && opp.StageName=='Closed Won')
                {
                    Account acc1=new Account();
                    
                    acc1.Name=opp.Name;
                    
                    acc.add(acc1);
                    
                }
                
                
        }
        
        
        try {
                insert acc;
                   
            } 
            
        catch (system.Dmlexception e) {
                system.debug (e);
        }
        
  }