+ Start a Discussion
aaaaaaaaaaaaaaaaaaaaaa 

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;
}

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
trigger testcases on case (before insert, before update) {
    map<string, contact> contacts = new Map<string, contact>();
    for(case record:trigger.new)
        contacts.put(record.suppliedemail, null);
    contacts.remove(null);
    for(contact record:[select id,accountid,email from contact where email in :contacts.keyset()])
        contacts.put(record.email, record);
    for(case record:trigger.new)
        if(record.status=='Closed' && record.contactid==null && contacts.containskey(record.suppliedemail)) {
            record.contactid = contacts.get(record.suppliedemail).id;
            record.accountid = contacts.get(record.suppliedemail).accountid;
    }
}

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

k_bentsenk_bentsen

Change your last line to:

 

casd.contact=listcontact.Id;

sfdcfoxsfdcfox
trigger testcases on case (before insert, before update) {
    map<string, contact> contacts = new Map<string, contact>();
    for(case record:trigger.new)
        contacts.put(record.suppliedemail, null);
    contacts.remove(null);
    for(contact record:[select id,accountid,email from contact where email in :contacts.keyset()])
        contacts.put(record.email, record);
    for(case record:trigger.new)
        if(record.status=='Closed' && record.contactid==null && contacts.containskey(record.suppliedemail)) {
            record.contactid = contacts.get(record.suppliedemail).id;
            record.accountid = contacts.get(record.suppliedemail).accountid;
    }
}

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.

This was selected as the best answer
aaaaaaaaaaaaaaaaaaaaaa

thanks a ton

sfdcfox :)
i understood my mistakes
and ya this works perfectly fine..!!!