• Sfdc User
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
hi All,
i created a batch to bring zendesk users in to salesforce contact now i want if the email of the zendesk users is already exist in any existing contact's email than we should not insert the zendesk user in salesforce contact and just put the zendesk_id in the existing contact and if email not exist in existing salesforce contact than simply insert the zendesk users in salesforce contact.
right now i am inserting zendesk users in salesforce contact.where shouid i put the logic of the existing email if email is existing in salesforce contact.
Any suggestion? how to do it?
my batch class is given below:-
/*
 * Batch to upsert Zendesk Users into salesforce Contacts
*/
public class BatchZendeskToSalesforceCon implements Database.Batchable<String>,Database.AllowsCallouts, Database.Stateful{
    
            // start method to return the list of end points of Zendesk Users
     public Iterable<String> start(Database.BatchableContext BC){
        
        List<Contact> conList = new List<Contact>();
        List<String> endPoints = new List<String>();
         // instance of class ZendeskApis to call Contacts Api
        ZendeskApis.ContactJSON2Apex firstRecords =  ZendeskApis.ZendeskContactApi();
        Map<String, Id> existingContactMap = new Map<String, Id>();
       /*for(Contact con : [Select id,Name, Email, Zendesk_Id__c 
                           from contact 
                           where Email != null AND RecordType.DeveloperName = 'Time_Rack']){
           existingContactMap.put(con.Zendesk_Id__c , con.Id);                       
        }*/
        
       Map<String , Id> mapzendeskIdToAccountId = new Map<String, Id>();
       for(Account acc : [Select id, Zendesk_Id__c 
                          from account 
                          where Zendesk_Id__c != null]){
           mapzendeskIdToAccountId.put(acc.Zendesk_Id__c, acc.Id);                      
       }
          Map<String,Id> mapZendeskEmailToContactId = new Map<String,Id>();
         for(Contact con :[Select id,Name, Email, Zendesk_Id__c 
                           from contact 
                           where Email != null AND RecordType.DeveloperName = 'Time_Rack']){
          mapZendeskEmailToContactId.put(con.Email,con.id); 
        }
         
        conList.addAll(insertContactData(firstRecords, existingContactMap, mapzendeskIdToAccountId, mapZendeskEmailToContactId));
        if(conList.size() >0){
          Database.upsert(conList, false);
        }
        Integer totalRecords = firstRecords.count ;
        if(totalRecords > 100 && 
           firstRecords.next_page != null && 
           firstRecords.next_page != ''){
               totalRecords = totalRecords / 100;
               for(Integer i= 2; i< totalRecords+2; i++){
                   String currentEndPoints = firstRecords.next_page;
                   currentEndPoints = currentEndPoints.substringBefore('?');
                   currentEndPoints = currentEndPoints + '?page='+i;
                   endPoints.add(currentEndPoints);
               }
           }
        return endPoints;
        
    }
            //execute method to upsert Users
     public void execute( Database.BatchableContext BC, List<String> endPoints){
        List<Contact> conList = new List<Contact>();
        Map<String, Id> existingContactMap = new Map<String, Id>();
        /*for(Contact con : [Select id,Name, Email, Zendesk_Id__c 
                           from contact 
                           where Email != null AND RecordType.DeveloperName = 'Time_Rack']){
           existingContactMap.put(con.Zendesk_Id__c , con.Id);                       
        }*/
        Map<String , Id> mapzendeskIdToAccountId = new Map<String, Id>();
       for(Account acc : [Select id, Zendesk_Id__c 
                          from account 
                          where Zendesk_Id__c != null]){
           mapzendeskIdToAccountId.put(acc.Zendesk_Id__c, acc.Id);                      
       }
         Map<String,Id> mapZendeskEmailToContactId = new Map<String,Id>();
         for(Contact con :[Select id,Name, Email, Zendesk_Id__c 
                           from contact 
                           where Email != null AND RecordType.DeveloperName = 'Time_Rack']){
          mapZendeskEmailToContactId.put(con.Email,con.id); 
                               
       }
        ZendeskApis.ContactJSON2Apex ajNextRecords =  ZendeskApis.ZendeskContactNextApi(endPoints[0]);
        conList.addAll(insertContactData(ajNextRecords, existingContactMap, mapzendeskIdToAccountId, mapZendeskEmailToContactId));
        if(conList.size() > 0){
            Database.upsert(conList, false);
        }
        
    }
             // method to insert Users Records into Contact
     public static List<Contact> insertContactData(ZendeskApis.ContactJSON2Apex conData, 
                                                  Map<String, Id> existingContactMap,
                                                  Map<String, Id> existingAccountMap,
                                                  Map<String, Id> mapZendeskEmailToContactId){
        List<Contact> conList = new List<Contact>();
        //ZendeskApis.ZendeskContactApi();
        for(ZendeskApis.ContactUsers conWrapData :  conData.users){
            Contact con = new Contact();
            /*if(existingContactMap.containsKey(String.valueOf(conWrapData.id))){
              con = new Contact(Id = existingContactMap.get(String.valueOf(conWrapData.id)));
            }*/
            if(existingAccountMap.containsKey(conWrapData.organization_id) ){
                    con.AccountId = existingAccountMap.get(conWrapData.organization_id);
            }    
            if(mapZendeskEmailToContactId.containsKey(conWrapData.email)){
                con.Zendesk_Id__c = String.valueOf(conWrapData.id);
            }
            con.lastName = conWrapData.name;
            if(conWrapData.id != null){
                con.Zendesk_Id__c = String.valueOf(conWrapData.id);
            }
            con.Zd_url__c = conWrapData.url;
            con.Email = conWrapData.email;
            if(conWrapData.created_at != null){
                con.Zd_Created_at__c = Date.valueOf(conWrapData.created_at);
            }
            if(conWrapData.updated_at != null){
                con.Zd_Updated_at__c = Date.valueOf(conWrapData.updated_at);
            }
            con.Zd_time_zone__c = conWrapData.time_zone;
            con.Zd_iana_time_zone__c = conWrapData.iana_time_zone;
            if(conWrapData.phone != null){
                con.Phone = String.valueOf(conWrapData.phone);
            }
            if(conWrapData.shared_phone_number != null){
                con.Zd_Shared_phone_number__c = Boolean.valueOf(conWrapData.shared_phone_number);
            }
            if(conWrapData.photo != null){
                con.Photo__c = String.valueOf(conWrapData.photo);
            }
            con.locale_id__c = conWrapData.locale_id;
            con.locale__c = conWrapData.locale;
            if(conWrapData.organization_id != null){
                con.Zd_Organisation_id__c = String.valueOf(conWrapData.organization_id);
            }
            con.Zd_role__c = conWrapData.role;
            con.Zd_verified__c = conWrapData.verified;
            con.External_Id__c = conWrapData.external_id;
            if(conWrapData.tags != null){
                con.Zd_Tags__c = String.valueOf(conWrapData.tags);
            }
            con.Alias__c = conWrapData.alias;
            con.Active__c = conWrapData.active;
            con.Zd_Shared__c = conWrapData.shared;
            con.Zd_Shared_agent__c = conWrapData.shared_agent;
            if(conWrapData.last_login_at != null){
                con.Zd_last_login_at__c = Date.valueOf(conWrapData.last_login_at);
            }
            if(String.valueOf(conWrapData.two_factor_auth_enabled) != null && 
               String.valueOf(conWrapData.two_factor_auth_enabled) != ''){
                  con.Zd_two_factor_auth_enabled__c = conWrapData.two_factor_auth_enabled;
               }
            con.Zd_Signature__c = conWrapData.signature;
            con.Zd_details__c = conWrapData.details;
            con.Notes__c = conWrapData.notes;
            con.Role_Type__c = conWrapData.role_type;
            con.Custom_Role_id__c = conWrapData.custom_role_id;
            con.Moderator__c = conWrapData.moderator;
            con.Zd_ticket_restriction__c = conWrapData.ticket_restriction;
            con.Only_private_Comments__c = conWrapData.only_private_comments;
            con.Zd_Restricted_Agent__c = conWrapData.restricted_agent;
            con.Zd_Suspended__c = conWrapData.suspended;
            con.Chat_only__c = conWrapData.chat_only;
            con.Zd_default_group_id__c = conWrapData.default_group_id;
            con.Zd_report_csv__c = conWrapData.report_csv;
            if(conWrapData.user_fields != null){
                con.Zd_user_fields__c = String.valueOf(conWrapData.user_fields);
            }
            conList.add(con);    
        }
        return conList;
    }
    public void finish(Database.BatchableContext BC){
        
    }
 }
how to do it?
Any suggestions?
Hello I would like to rollup a field value from all of the assets on an account up to that account's field. Currently, the trigger is updating the account field, but it's overwriting the existing value, and not collecting the asset field values. On line 24 I'm getting an error of illegal assignment from set to string (ProductsOwned_c += prodFam.clone()). 

I'm trying to take all of the unique string values in the ProdFam set variable and load them into the ProductsOwned__ field. Can I load a collection into a text field? Thank you for your help.  

trigger BulkifyAssets on Asset (before insert, before update) {
    Set<Id> accountId = new Set<Id>();
    Map<Id, Account> parentRecords = new Map<Id, Account>();
    Map<String,Account> accProductsOwnedMap = new Map<String,Account>();
    Set<String> prodFam = new Set<String>();
   
for(Asset a: trigger.new){
    if(a.Status != null || a.status != 'Retired' || a.status != 'Obsolete' )
    accountId.add(a.AccountId);
    accountId.remove(null);
    String prodFamAccount = a.ProductFamily__c;
    prodFam.add(prodFamAccount);
   
   
    for(Id parentId: accountId){
        String prodFamSingle;
       
        for(String f: prodFam)
                    parentRecords.put(parentId, new Account(Id = parentId, ProductsOwned__c =+ prodFam.clone()));
 
       
        for(Asset asset: [Select Id, ProductFamily__c, AccountId from Asset Where Id In: accountId]){
           if(parentRecords.get(asset.AccountId).ProductsOwned__c != null){
           parentRecords.get(asset.AccountId).ProductsOwned__c += ',' + parentRecords.values();
        }
        else{
     parentRecords.get(asset.AccountId).ProductsOwned__c += ',' + parentRecords.values();             
        }
        }
   
    if(parentRecords.size()>0)
    {
   update parentRecords.values();
}  
}
}
}
 
  • April 11, 2019
  • Like
  • 0