You need to sign in to do that
Don't have an account?
how to convert string to sobject?
hi all,
i have a trigger which is giving error Illegal assignment from String to SOBJECT,
i want to fetch the name from query given in below code into contact field for case object.
please suggest me how to do this
code:
trigger testcases on Case (after insert,after update ) {
case casd=new case();
List<String> emailAddresses = new List<String>();
for (Case caseObj:Trigger.new) {
if (caseObj.Account==null &&
caseObj.Status== 'closed')
{
emailAddresses.add(caseObj.SuppliedEmail);
}
}
contact listcontact = [Select Id,name From contact Where Email in :emailAddresses];
// we will query for all fields which are required for contact and accounts//
//system.debug('----TEST---'+listcontact);
casd.contact=listcontact.name;
}
The "normal" values with the word ID at the end are used for relationships, either for "upsert" commands or when retrieving related results. Neither case applies here, so you want to reference by ID.
Furthermore, you need to use a "before dml" trigger, or your code simply won't work. Records are read-only in an after-DML event, so this code wouldn't work.
Thirdly, you created a case in memory, but you never committed it to the database; this means that your work would have been lost anyways.
Finally, your code wouldn't work with more than one case (or, at least, more than one contact).
The code I provided above provides basically the logic you were looking for.
All Answers
Change your last line to:
casd.contact=listcontact.Id;
The "normal" values with the word ID at the end are used for relationships, either for "upsert" commands or when retrieving related results. Neither case applies here, so you want to reference by ID.
Furthermore, you need to use a "before dml" trigger, or your code simply won't work. Records are read-only in an after-DML event, so this code wouldn't work.
Thirdly, you created a case in memory, but you never committed it to the database; this means that your work would have been lost anyways.
Finally, your code wouldn't work with more than one case (or, at least, more than one contact).
The code I provided above provides basically the logic you were looking for.
thanks a ton