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

Record is creating with record id, not name
Hi All,
I am new in apex coding, I am trying to write trigger and this is my code.
My requirement is whenever I am creating new visitor record in the system( when I select multiselect picklist value which is 'Feve' and 'Dry Cough') new patient record should create under patient record.
My code is working fine, but patient record creates with ID, not name.
When I select only 1 value from the mutli-select picklist, then patient record creates with name.
trigger VisitorChangedIntoPatient on Visitor__c (after insert) {
List<Patient__c> pat = new List<Patient__c>();
for(Visitor__c visitor : Trigger.New)
{
Patient__c p = new Patient__c();
if(trigger.isInsert && Visitor.Covid_19_Symtomp__c=='Fever' && Visitor.Covid_19_Symtomp__c=='DryCough')
{
p.id=visitor.id;
p.Name=visitor.Name+'SQC';
p.IsInfected__c=TRUE;
}
pat.add(p);
}
insert pat;
}
I am new in apex coding, I am trying to write trigger and this is my code.
My requirement is whenever I am creating new visitor record in the system( when I select multiselect picklist value which is 'Feve' and 'Dry Cough') new patient record should create under patient record.
My code is working fine, but patient record creates with ID, not name.
When I select only 1 value from the mutli-select picklist, then patient record creates with name.
trigger VisitorChangedIntoPatient on Visitor__c (after insert) {
List<Patient__c> pat = new List<Patient__c>();
for(Visitor__c visitor : Trigger.New)
{
Patient__c p = new Patient__c();
if(trigger.isInsert && Visitor.Covid_19_Symtomp__c=='Fever' && Visitor.Covid_19_Symtomp__c=='DryCough')
{
p.id=visitor.id;
p.Name=visitor.Name+'SQC';
p.IsInfected__c=TRUE;
}
pat.add(p);
}
insert pat;
}
if this helps you, please mark it as a best answer, it may help others.
All Answers
For this code and for your code you can refer the below sample code.
trigger SalesRepOnAccount_Owner on Account (before insert,before update) {
Set<Id> newAccountList=new Set<Id>();
for(Account a:trigger.new)
{
newAccountList.add(a.OwnerId);
}
Map<id,User> mapid= new Map<id,User>([select name from user where id in:newAccountList]);
for(Account acc:trigger.new)
{
user usr=mapid.get(acc.OwnerId);
acc.sales_Rep__c=usr.Name;
}
}
Thanks & regards
Divyansh Verma
if this helps you, please mark it as a best answer, it may help others.
Is there any relation between the objects Visitor__c and Patient__c. I see the code inside if loop in line 11 you are trying to set the id of patient record ( p.id=visitor.id;) which is not possible. Salesforce will generate Ids once the record is created sucessfully. But if you are adding the patient record to Visitor record as related list record with the related reference field (lookup field) look for correct field API name.
Go to the Patient object-> fields & Relationships-> Check if there is any field with reference to Visitor object as lookup or master detailed. Copy the API name if its there and try replacing the p.id with p.API_NAME. (p.Visitor__c=Visitor.id;)
If there is no relation between two objects but just create records based on condition you can remove line 11 from your code.
Hope this helps! Please mark as best if it solves your problem
Thanks
Please ignore this line : ( p.id=visitor.id;)
Records are creating but with ID not name in Patient object.
I got the answer. I used below solution:
if(Visitor.Covid_19_Symtomp__c.contains('Fever') && Visitor.Covid_19_Symtomp__c.contains('DryCough'))
Thansk For your help.
Please try with below code. It worked for me using contains. Also, make sure your visitor record's name field value is not blank.
The above code will create patient records only when a new visitor record is beign created with Covid_19_Symtomp__c having fever and dry cough selected with any other values.
Thanks