function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TrickTrick 

Please help with the trigger

 

trigger case1 on Case (after insert)

 {

List<message__c> l=new list<message__c>();

if(trigger.isinsert)

{

for(case c:trigger.new)

{
system.debug('The value in the case variable c is'+c);if(c.fine__c)

{
Message__c a=new Message__c();

//a.accountid__c=c.accountid;

a.name__c=c.casenumber;

System.debug('The value in the variable a.name__c is'+a.name__c);

//a.name__c=c.contact;

a.Name='Sweet and Sour';

System.debug('The value in the variable a.name explixitly has been assigned is'+a.name);

l.add(a);

}

System.debug('The value in the List l is explicitly has been assigned is'+L);

}

}

insert(l);

}


Guys, I am new to salesforce and to this community.I am working on the problem.I have a trigger on the case object and a check boxfield on the case .If somebody checks that box then another object by the name of message should be created and some value from the case object should be passed on to the fields on the message object. For example,this value a.Name='Sweet and Sour'; has been harcoded becasue I was getting some error.a.name__c=c.casenumber;
Above I have assigned values from casenumber field on the case to the field name__c on the message field.
Firstly,Can I do this without having any relationship between the case and the message object.?Secondly,It is adding all the values to the message list as I can see in the debug log.However,it is not not inserting record with the values in the message custome object.Thirdly,Help me in correcting it so that it can insert record in the message object.


Please help

Thanks,

Trick

 

Best Answer chosen by Admin (Salesforce Developers) 
David81David81

Correct.

All Answers

David81David81

First off, you shouldn't have to test if the trigger isInsert, since you have it sent to only fire "after insert".

 

I'm confused by your second bit. Do you have a second/custom field called "Name" in addition to the standard Name field present on all objects? If so, what value are you putting in each field, Name and Name__c?

 

When you say it isn't inserting the objects, are you getting any sort of error? If there are no errors, you should be seeing the new objects. One way you can check this is to use the Database.insert method which returns a List of SaveResults. You can then iterate through this list and system.debug the Ids of the new Message__c objects.

 

 

 

TrickTrick

Thanks David,

 

I know my code looks nonsensical to you at the moment.Yes,I have two fields one is Name and other is Name__c which is the custom field.In the name field I have hard coded the value Name='Sweet and sour' and in the other custom name field I am putting the case number from the case object to the Name__c  in the  message object.

 

I am not seeing any error.I have simply not been able to see the saved record.I tried system debug and I could see case number value and sweet and sour value but I don't see them in the database.

 

I have tried database statement also but it did not make any difference.

 

Has it got anything to do with relationship between the two objects.One is custom object and the other is standard object.

 

Your suggestions are most welcome. 

 

Thanks,

Trick

David81David81

It shouldn't have anything to do with the custom vs. standard objects. Is there actually a relationship between the objects? It doesn't sound like there is.

 

To verify if you inserts are happening, you can do something like this:

 

Database.SaveResult[] results = Database.insert(<your list of Message__c objects>);

if(!results.isEmpty()){
    for(Database.SaveResult sr : results){
        if(sr.IsSuccess()){
            system.debug('*****Insert Success');
            system.debug('*****Record Id = '+sr.getId();
        }
        else{
            system.debug('*****Insert Not Successful');
            for(Database.Error e : sr.getErrors()){
                system.debug('*****ERROR = '+e.getMessage();
            }
        }
    }
}

 

 

TrickTrick

Thanks a lot, David, for helping,

 

I will try to see what result I get.Otherwise I will come back to you to get some more advice.David,does the rest of the code looks good to you?

 

I am trying to do it without any relationship.

 

Thanks,

Trick

David81David81

The basics are there, but you'll need to query the Contact table if you want the Contact name on your new object. You'll have to go through your cases and get the Contact Ids, then query the contact table for a list of contacts so that you can get the Names.

 

If you need some help with that bit, let me know, but I'm curious to see what you get from the other debugging.

TrickTrick

Hi David,

 

Thanks its working.

 

I could see information in the message ojbect. I tried to debug with the database statement  and I have this id

 

01C000000OYYMUIA5.However,the record id that I have copied from the browser is given below.Why are id's not exactly same.There is a difference of few characters in the end

 

01C000000OYYMU

 

From 

Trick

David81David81

There are two versions of every object Id. One 15 character case sensitive Id and another 18 character non-case sensitive. They can typically be used interchangably unless you are doing comparisons.

 

I'm not aware of a way to convert between the two, particularly 15 to 18, but it may exist.

TrickTrick

So Both the ID's would represent the same record.Is that correct? 

David81David81

Correct.

This was selected as the best answer
TrickTrick

 

Hi David,
The basics are there, but you'll need to query the Contact table if you want the Contact name on your new object. You'll have to go through your cases and get the Contact Ids, then query the contact table for a list of contacts so that you can get the Names.
I am actually trying to do exaclty the same as you have mentioned before.I have taken three objects,case object on which i have a trigger and contact and message.From the contact I am trying  to pass name of the contacts to the message .However,I am not able to do that as I am getting error message which is on line 7
: Compile Error: Incompatible element type LIST<Case> for collection of String at line 7 column 1
Can u please help me with that.I am not able to figure out.I will wait for your reply.?
trigger change1 on Case (after insert)
{
//message__c s;
list<message__c> a=new list<message__c>();
List<string>idlist=new list<string>();
list<case> l=[select contactid,id from case where id=:trigger.new[0].id]; 
idlist.add(l);
list<contact>c = [select name,id from contact where id in:idlist]; 
for(contact m:c)
{
Message__c s=new Message__c();
s.name=m.contact;
s.add(a);.
}
database.insert(a);
}




 

TrickTrick

 

Hi David,
The basics are there, but you'll need to query the Contact table if you want the Contact name on your new object. You'll have to go through your cases and get the Contact Ids, then query the contact table for a list of contacts so that you can get the Names.
I am actually trying to do exaclty the same as you have mentioned before.I have taken three objects,case object on which i have a trigger and contact and message.From the contact I am trying  to pass name of the contacts to the message .However,I am not able to do that as I am getting error message which is on line 7
: Compile Error: Incompatible element type LIST<Case> for collection of String at line 7 column 1
Can u please help me with that.I am not able to figure out.I will wait for your reply.?
trigger change1 on Case (after insert)
{
//message__c s;
list<message__c> a=new list<message__c>();
List<string>idlist=new list<string>();
list<case> l=[select contactid,id from case where id=:trigger.new[0].id]; 
idlist.add(l);
list<contact>c = [select name,id from contact where id in:idlist]; 
for(contact m:c)
{
Message__c s=new Message__c();
s.name=m.contact;
s.add(a);.
}
database.insert(a);
}