You need to sign in to do that
Don't have an account?

Please help me to practice Development with best practices
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');
}
}
}
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');
}
}
}
Basically, you need a List<String> for the city names. then loop through the cities and put all their names in that. Then use that list instead of ":lst.name" in your soql query.
All Answers
Basically, you need a List<String> for the city names. then loop through the cities and put all their names in that. Then use that list instead of ":lst.name" in your soql query.
Thanks Aubry for the suggestion it worked fine for me
can you please suggest on below test class issue as well please
error message : System.ListException: Duplicate id in list: 0691l000000VZQHAA4
This is coming from main class and which it is not a list it is a set. Please help me here im trying this since past few days 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);
}
}