• CodeBeginner
  • NEWBIE
  • 20 Points
  • Member since 2019
  • Salesforce

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 3
    Replies
Hi I have developed the test class which should check for each parent record childs should have attachment and they need to be renamed and at parent level should contain 2 attachments and they need to be renamed. 

error message : System.ListException: Duplicate id in list: 0691l000000VZQHAA4

this error is coming from apex class which is containing set im wondered like how set contains duplicate IDs , can anyone help me here please trying since few days but not able to figure it out.

@isTest
private class SVMX_PS_FileTriggerHandlerTest {

    static testMethod void filesInsertTest() {
        
        //Create Account
        Account testAccount = SVMX_PS_TestUtil.createAccount(false);
        testAccount.SVMX_PS_ERP_Id__c = SVMX_PS_TestUtil.generateRandomString(16);
        insert testAccount;

        // create product
        Product2 prod = SVMX_PS_TestUtil.generateProduct();
        prod.ProductCode = SVMX_PS_TestUtil.generateRandomString(16);
        prod.SVMXC__Unit_Of_Measure__c = 'EA';
        insert prod;
        
        //Create Work Order
        SVMXC__Service_Order__c wo = SVMX_PS_TestUtil.createWorkOrder(testAccount.Id, 'New', false);
        wo.SVMXC__Order_Type__c='Field Service';
        wo.SVMXC__Street__c = '16260 Monterey St.';
        wo.SVMXC__City__c = 'Morgan Hill';
        wo.SVMXC__State__c = 'Arizona';
        wo.SVMXC__Zip__c = '95037';
        wo.SVMXC__Country__c = 'United States';
        wo.SVMXC__Priority__c = 'Medium';
        wo.RecordTypeId = SVMX_PS_AdvancedPricingCustomAction.WO_SERVICE_ORDER_RT;
        wo.SMAX_PS_Veolia_Business_Line__c = '12345';
        wo.SVMX_PS_ERP_Region__c = 'EMEA';
        insert wo;
        
        SVMXC__Service_Group__c SvcTeam = SVMX_PS_TestUtil.createServiceTeam(false);
        insert SvcTeam;
        
        SVMXC__Service_Group_Members__c Tech = SVMX_PS_TestUtil.createTechnician(false, SvcTeam.id);
        insert Tech;
        
      //  Id RTID = SVMX_PS_TestUtil.getRecordTypeId('SVMXC__Service_Order_Line__c.SVMXC.Products_Serviced');
        Id RTID = SVMX_PS_TestUtil.WDL_PRODUCTS_SERVICED_RT;
        
         SVMX_PS_VS_Part_Consumption_Settings__c partConsumptionSettings = new SVMX_PS_VS_Part_Consumption_Settings__c(name='Use Allocated Qty' );
         insert partConsumptionSettings;
         
        SVMXC__Service_Order_Line__c wd = SVMX_PS_TestUtil.createWorkDetailLine(wo.id, Tech.id, RTID, false);
        insert wd;
        
        ContentVersion cv = new ContentVersion(Title = 'Test_Do_Not_Remove', VersionData = Blob.valueof('Test Content Data'), IsMajorVersion = true,
                                              PathOnClient = 'Test.pdf');
        insert cv;
        
        ContentVersion cvlmra = new ContentVersion(Title = 'Test_Do_Not_Removelmra', VersionData = Blob.valueof('Test Content Data'), IsMajorVersion = true,
                                              PathOnClient = 'Test123.pdf');
        insert cvlmra;
        
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument LIMIT 1];
            
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId = wo.id;  
        contentlink.ShareType= 'I';               
        contentlink.ContentDocumentId=documents[0].Id;       
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink;
        
         List<ContentDocument> document = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument  where Id NOT IN : documents LIMIT 1];
        
         ContentDocumentLink contentlinklmra=new ContentDocumentLink();
        contentlinklmra.LinkedEntityId = wo.id;  
        contentlinklmra.ShareType= 'I';               
        contentlinklmra.ContentDocumentId=document[0].Id; 
        contentlinklmra.Visibility = 'AllUsers'; 
        insert contentlinklmra;
        
        ContentVersion cv1 = new ContentVersion(Title = 'Test_Do_Not_Remove1', VersionData = Blob.valueof('Test Content Data1'), IsMajorVersion = true,
                                              PathOnClient = 'Test1.pdf');
        insert cv1;
        
        List<ContentDocument> documents1 = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument LIMIT 1];
            
        ContentDocumentLink contentlink1=new ContentDocumentLink();
        contentlink1.LinkedEntityId = wd.id;  
        contentlink1.ShareType= 'I';               
        contentlink1.ContentDocumentId=documents1[0].Id;
        contentlink1.Visibility = 'AllUsers'; 
        insert contentlink1;
        
        Test.startTest();

        SVMX_PS_FileRenameBatch myBatch = new SVMX_PS_FileRenameBatch();
        Database.executeBatch(myBatch);
        
        Test.stopTest();
        ContentDocumentLink reQueryCDLwdcheck = [SELECT ContentDocument.Title FROM ContentDocumentLink WHERE Id =: contentlink1.Id];    
        System.assertNotEquals(cv1.Title, reQueryCDLwdcheck.ContentDocument.Title);
        
        ContentDocumentLink reQueryCDL = [SELECT ContentDocument.Title FROM ContentDocumentLink WHERE Id =: contentlink.Id];    
        System.assertNotEquals(cv.Title, reQueryCDL.ContentDocument.Title);
        
        ContentDocumentLink reQueryCDLlmra = [SELECT ContentDocument.Title FROM ContentDocumentLink WHERE Id =: contentlinklmra.Id];    
        System.assertNotEquals(cvlmra.Title, reQueryCDLlmra.ContentDocument.Title);
        
        SVMXC__Service_Order__c reQueryWO = [SELECT SMAX_PS_Ready_for_Document_Merge__c FROM SVMXC__Service_Order__c WHERE Id =: wo.Id];
        System.assertEquals(true, reQueryWO.SMAX_PS_Ready_for_Document_Merge__c);
    }
}
public class practicingclass {
   public static Map<Id,Opportunity> maptest = new map<Id,Opportunity>();
    public static list<Opportunity> opplist = new list<Opportunity>();
  public static map<Id,list<OpportunityChild__c>> parentchildmap = new map<Id,list<OpportunityChild__c>>();
    public Static void practicingMethod(){
        opplist =[select Name from Opportunity LIMIT 5];
        for(Opportunity opp : opplist){
            maptest.put(opp.Id, opp);
        }
        
       system.debug('maptestvalues ------'  + maptest);
    }
    
    public static void maptestingOpportunity(list<Opportunity> TEST){
        map<Id,list<OpportunityChild__c>> parentchildmap = new map<Id,list<OpportunityChild__c>>();
        system.debug('requireddata testing ' + TEST);
        list<Opportunity> requireddata = new list<Opportunity>();
        requireddata =[select id,Name from Opportunity WHERE Id IN :TEST];
        system.debug('=== data to process===' +requireddata);
        
        list<OpportunityChild__c> oppchild = new list<OpportunityChild__c>();
      oppchild =[select id,Name,Opportunity__c from OpportunityChild__c WHERE Opportunity__c IN :requireddata];
        system.debug('oppchilds'+ oppchild);
        
        for(OpportunityChild__c oppchild1 :oppchild){
            if(!parentchildmap.isEmpty() && parentchildmap.containsKey(oppchild1.Opportunity__c)){
                parentchildmap.get(oppchild1.Opportunity__c).add(oppchild1);
            }
            else {
                parentchildmap.put(oppchild1.Opportunity__c,new list<OpportunityChild__c>{oppchild1});
            }
            
        }
        system.debug('parentchildmap' +parentchildmap.values());
        Parentchildcheck();
        parentupdate();
    }
    Public static void Parentchildcheck(){
        Map<Id,OpportunityChild__c> REC = new Map<Id,OpportunityChild__c>();
        for(OpportunityChild__c RECo : [select id,Price__c from OpportunityChild__c WHERE Opportunity__c IN :parentchildmap.keyset()]) {
            if(REC.isEmpty() && !REC.containsKey(RECo.Id)){
              REC.put(RECo.Id, RECo.Price__c);
            }
            
        } 
        for(Id in :parentchildmap.keyset()){
            for(OpportunityChild__c child :parentchildmap.get(in)){
                if(REC.containsKey(child)){
                    String store = REC.get(child);
                    if(store != null){
                    //nothing to do
                }
                
                }
                else {
                    parentchildmap.remove(in);
                    break;
                }
            }
        }
    }
    
    Public static void parentupdate(){
        if(!parentchildmap.isEmpty()){
            list<Opportunity> records = [select Amount,LeadSource from Opportunity WHERE Id IN:parentchildmap.keyset()];
            for(Opportunity parentid :records){
                if(parentid.Amount != null){
                   parentid.LeadSource = 'Partner Referral';
                }
                else{
                    //nothing to do
                    break;
                }
                
            }
        }
    }
}
I am trying to write a trigger for acount field update when coresponding contact field in updated. Please help me here the code is not saving 

Trigger FieldupdateOnAccount on Contact(after insert, after update){
set<id> conset = new set<id>();
for(Contact c: trigger.new){
conset.add(c.id);
}
list<Account> Lstacc = [select Name, Phone(select Name, Phone from Contacts) from Account where Contactid in: conset];
List<Account> Acclist = new List<Account>();
if(Lstacc.size()>0 && Trigger.newMap.get(Id).Phone != Trigger.oldMap.get(Id).Phone){
for(Account acc :Lstacc){
for(Contact c: acc.Contacts){

acc.Phone = c.Phone;
Acclist.add(acc);
}
}
Update Acclist;
}
}
Hi Developers,
Iam learning salesforce customization part here i tried to create trigger to prevent duplicate entry of records while inserting and updating but i think iam missing somewhere. please review my code and help me to learn best coding practices.
Thanks in advance.
hereis my code

trigger PreventDuplicates on City__c (before insert,before update) {
list<City__c> lst = new list<City__c>(trigger.new);
list<City__c> lstcity =[select name from City__c where name in :lst.name];
if(lstcity.size>0){
for(City__c c: lst){

c.adderror('Duplicate records found');
}
}
}
Hi I have developed the test class which should check for each parent record childs should have attachment and they need to be renamed and at parent level should contain 2 attachments and they need to be renamed. 

error message : System.ListException: Duplicate id in list: 0691l000000VZQHAA4

this error is coming from apex class which is containing set im wondered like how set contains duplicate IDs , can anyone help me here please trying since few days but not able to figure it out.

@isTest
private class SVMX_PS_FileTriggerHandlerTest {

    static testMethod void filesInsertTest() {
        
        //Create Account
        Account testAccount = SVMX_PS_TestUtil.createAccount(false);
        testAccount.SVMX_PS_ERP_Id__c = SVMX_PS_TestUtil.generateRandomString(16);
        insert testAccount;

        // create product
        Product2 prod = SVMX_PS_TestUtil.generateProduct();
        prod.ProductCode = SVMX_PS_TestUtil.generateRandomString(16);
        prod.SVMXC__Unit_Of_Measure__c = 'EA';
        insert prod;
        
        //Create Work Order
        SVMXC__Service_Order__c wo = SVMX_PS_TestUtil.createWorkOrder(testAccount.Id, 'New', false);
        wo.SVMXC__Order_Type__c='Field Service';
        wo.SVMXC__Street__c = '16260 Monterey St.';
        wo.SVMXC__City__c = 'Morgan Hill';
        wo.SVMXC__State__c = 'Arizona';
        wo.SVMXC__Zip__c = '95037';
        wo.SVMXC__Country__c = 'United States';
        wo.SVMXC__Priority__c = 'Medium';
        wo.RecordTypeId = SVMX_PS_AdvancedPricingCustomAction.WO_SERVICE_ORDER_RT;
        wo.SMAX_PS_Veolia_Business_Line__c = '12345';
        wo.SVMX_PS_ERP_Region__c = 'EMEA';
        insert wo;
        
        SVMXC__Service_Group__c SvcTeam = SVMX_PS_TestUtil.createServiceTeam(false);
        insert SvcTeam;
        
        SVMXC__Service_Group_Members__c Tech = SVMX_PS_TestUtil.createTechnician(false, SvcTeam.id);
        insert Tech;
        
      //  Id RTID = SVMX_PS_TestUtil.getRecordTypeId('SVMXC__Service_Order_Line__c.SVMXC.Products_Serviced');
        Id RTID = SVMX_PS_TestUtil.WDL_PRODUCTS_SERVICED_RT;
        
         SVMX_PS_VS_Part_Consumption_Settings__c partConsumptionSettings = new SVMX_PS_VS_Part_Consumption_Settings__c(name='Use Allocated Qty' );
         insert partConsumptionSettings;
         
        SVMXC__Service_Order_Line__c wd = SVMX_PS_TestUtil.createWorkDetailLine(wo.id, Tech.id, RTID, false);
        insert wd;
        
        ContentVersion cv = new ContentVersion(Title = 'Test_Do_Not_Remove', VersionData = Blob.valueof('Test Content Data'), IsMajorVersion = true,
                                              PathOnClient = 'Test.pdf');
        insert cv;
        
        ContentVersion cvlmra = new ContentVersion(Title = 'Test_Do_Not_Removelmra', VersionData = Blob.valueof('Test Content Data'), IsMajorVersion = true,
                                              PathOnClient = 'Test123.pdf');
        insert cvlmra;
        
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument LIMIT 1];
            
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId = wo.id;  
        contentlink.ShareType= 'I';               
        contentlink.ContentDocumentId=documents[0].Id;       
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink;
        
         List<ContentDocument> document = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument  where Id NOT IN : documents LIMIT 1];
        
         ContentDocumentLink contentlinklmra=new ContentDocumentLink();
        contentlinklmra.LinkedEntityId = wo.id;  
        contentlinklmra.ShareType= 'I';               
        contentlinklmra.ContentDocumentId=document[0].Id; 
        contentlinklmra.Visibility = 'AllUsers'; 
        insert contentlinklmra;
        
        ContentVersion cv1 = new ContentVersion(Title = 'Test_Do_Not_Remove1', VersionData = Blob.valueof('Test Content Data1'), IsMajorVersion = true,
                                              PathOnClient = 'Test1.pdf');
        insert cv1;
        
        List<ContentDocument> documents1 = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument LIMIT 1];
            
        ContentDocumentLink contentlink1=new ContentDocumentLink();
        contentlink1.LinkedEntityId = wd.id;  
        contentlink1.ShareType= 'I';               
        contentlink1.ContentDocumentId=documents1[0].Id;
        contentlink1.Visibility = 'AllUsers'; 
        insert contentlink1;
        
        Test.startTest();

        SVMX_PS_FileRenameBatch myBatch = new SVMX_PS_FileRenameBatch();
        Database.executeBatch(myBatch);
        
        Test.stopTest();
        ContentDocumentLink reQueryCDLwdcheck = [SELECT ContentDocument.Title FROM ContentDocumentLink WHERE Id =: contentlink1.Id];    
        System.assertNotEquals(cv1.Title, reQueryCDLwdcheck.ContentDocument.Title);
        
        ContentDocumentLink reQueryCDL = [SELECT ContentDocument.Title FROM ContentDocumentLink WHERE Id =: contentlink.Id];    
        System.assertNotEquals(cv.Title, reQueryCDL.ContentDocument.Title);
        
        ContentDocumentLink reQueryCDLlmra = [SELECT ContentDocument.Title FROM ContentDocumentLink WHERE Id =: contentlinklmra.Id];    
        System.assertNotEquals(cvlmra.Title, reQueryCDLlmra.ContentDocument.Title);
        
        SVMXC__Service_Order__c reQueryWO = [SELECT SMAX_PS_Ready_for_Document_Merge__c FROM SVMXC__Service_Order__c WHERE Id =: wo.Id];
        System.assertEquals(true, reQueryWO.SMAX_PS_Ready_for_Document_Merge__c);
    }
}
public class practicingclass {
   public static Map<Id,Opportunity> maptest = new map<Id,Opportunity>();
    public static list<Opportunity> opplist = new list<Opportunity>();
  public static map<Id,list<OpportunityChild__c>> parentchildmap = new map<Id,list<OpportunityChild__c>>();
    public Static void practicingMethod(){
        opplist =[select Name from Opportunity LIMIT 5];
        for(Opportunity opp : opplist){
            maptest.put(opp.Id, opp);
        }
        
       system.debug('maptestvalues ------'  + maptest);
    }
    
    public static void maptestingOpportunity(list<Opportunity> TEST){
        map<Id,list<OpportunityChild__c>> parentchildmap = new map<Id,list<OpportunityChild__c>>();
        system.debug('requireddata testing ' + TEST);
        list<Opportunity> requireddata = new list<Opportunity>();
        requireddata =[select id,Name from Opportunity WHERE Id IN :TEST];
        system.debug('=== data to process===' +requireddata);
        
        list<OpportunityChild__c> oppchild = new list<OpportunityChild__c>();
      oppchild =[select id,Name,Opportunity__c from OpportunityChild__c WHERE Opportunity__c IN :requireddata];
        system.debug('oppchilds'+ oppchild);
        
        for(OpportunityChild__c oppchild1 :oppchild){
            if(!parentchildmap.isEmpty() && parentchildmap.containsKey(oppchild1.Opportunity__c)){
                parentchildmap.get(oppchild1.Opportunity__c).add(oppchild1);
            }
            else {
                parentchildmap.put(oppchild1.Opportunity__c,new list<OpportunityChild__c>{oppchild1});
            }
            
        }
        system.debug('parentchildmap' +parentchildmap.values());
        Parentchildcheck();
        parentupdate();
    }
    Public static void Parentchildcheck(){
        Map<Id,OpportunityChild__c> REC = new Map<Id,OpportunityChild__c>();
        for(OpportunityChild__c RECo : [select id,Price__c from OpportunityChild__c WHERE Opportunity__c IN :parentchildmap.keyset()]) {
            if(REC.isEmpty() && !REC.containsKey(RECo.Id)){
              REC.put(RECo.Id, RECo.Price__c);
            }
            
        } 
        for(Id in :parentchildmap.keyset()){
            for(OpportunityChild__c child :parentchildmap.get(in)){
                if(REC.containsKey(child)){
                    String store = REC.get(child);
                    if(store != null){
                    //nothing to do
                }
                
                }
                else {
                    parentchildmap.remove(in);
                    break;
                }
            }
        }
    }
    
    Public static void parentupdate(){
        if(!parentchildmap.isEmpty()){
            list<Opportunity> records = [select Amount,LeadSource from Opportunity WHERE Id IN:parentchildmap.keyset()];
            for(Opportunity parentid :records){
                if(parentid.Amount != null){
                   parentid.LeadSource = 'Partner Referral';
                }
                else{
                    //nothing to do
                    break;
                }
                
            }
        }
    }
}
I am trying to write a trigger for acount field update when coresponding contact field in updated. Please help me here the code is not saving 

Trigger FieldupdateOnAccount on Contact(after insert, after update){
set<id> conset = new set<id>();
for(Contact c: trigger.new){
conset.add(c.id);
}
list<Account> Lstacc = [select Name, Phone(select Name, Phone from Contacts) from Account where Contactid in: conset];
List<Account> Acclist = new List<Account>();
if(Lstacc.size()>0 && Trigger.newMap.get(Id).Phone != Trigger.oldMap.get(Id).Phone){
for(Account acc :Lstacc){
for(Contact c: acc.Contacts){

acc.Phone = c.Phone;
Acclist.add(acc);
}
}
Update Acclist;
}
}
Hi Developers,
Iam learning salesforce customization part here i tried to create trigger to prevent duplicate entry of records while inserting and updating but i think iam missing somewhere. please review my code and help me to learn best coding practices.
Thanks in advance.
hereis my code

trigger PreventDuplicates on City__c (before insert,before update) {
list<City__c> lst = new list<City__c>(trigger.new);
list<City__c> lstcity =[select name from City__c where name in :lst.name];
if(lstcity.size>0){
for(City__c c: lst){

c.adderror('Duplicate records found');
}
}
}