+ Start a Discussion
Vincent van Drunen LittelVincent van Drunen Littel 

Can someone please help me on this trigger

Hi, 
I keep getting this error,
Error: Compile Error: Expression cannot be assigned at line -1 column -1

What im trying is when an Account is created it automatically creates a Concorrent with basic fields such as Name, Segmento, Tipo de Empresa, Telefone
API's for object Concorrent are - 
Concorrent__c
Segmento__c
Telefone__c
Tipo_de__c
And Name is a default field 

API's on Account
Segmento__c
Tipo_de_empresa__c

Phone and Account name are default. 

I'm probably doing a bunch of things wrong... Please o Please help me!! 


trigger Concorrent on Account (before insert) {
List<Concorrent__c> conList = new List<Concorrent__c>();
    for(Account acc : Trigger.New){
        if(acc.Type == 'Individual'){
            Concorrent__c con = new Concorrent__c();
            Concorrent__c.Name = acc.Name;
            Concorrent__c.Segmento__c = acc.Segmento__c;
            Concorrent__c.Telefone__c = acc.Phone;
            Concorrent__c.Tipo_de__c = acc.Tipo_de_empresa__c;
            conList.add(con);
        }
    }
    if(!conList.IsEmpty()){
        try{
            insert conList;
        } catch(Exception e){
            system.debug('Following Exceptions occured'+e.getmessage());
        }
    }
}

Best Answer chosen by Vincent van Drunen Littel
Krishna SambarajuKrishna Sambaraju
Hi Vincent,

Your custom object Concorrent__c is having a master detail relationship with Account, The custom field Concorrent__c on your custom object is a required field and you need the pass the Id of the Account to this field. You cannot pass the Id field in the before insert trigger, as the id will not be generated at that time. You need to use the after insert trigger and pass the account id to your Concorrent__c field on the Concorrent__c object.

I have changed it to after insert and also set the Concorrent__c to account Id. It is working now. You can check.

Regards,
Krishna.

All Answers

Krishna SambarajuKrishna Sambaraju
Hi,

In the above code you are not using the variable "con" to set the values. Replace Concorrent__c.Name to con.Name, do the same on the rest of the lines and try it.

Regards,
Krishna.
surasura
trigger Concorrent on Account (before insert) {
List<Concorrent__c> conList = new List<Concorrent__c>();
    for(Account acc : Trigger.New){
        if(acc.Type == 'Individual'){
            Concorrent__c con = new Concorrent__c();
            con.Name = acc.Name;
            con.Segmento__c = acc.Segmento__c;
            con.Telefone__c = acc.Phone;
            con.Tipo_de__c = acc.Tipo_de_empresa__c;
            conList.add(con);
        }
    }
    if(!conList.IsEmpty()){
        try{
            insert conList;
        } catch(Exception e){
            system.debug('Following Exceptions occured'+e.getmessage());
        }
    }
}
Vincent van Drunen LittelVincent van Drunen Littel

Thank you both for a quick answer but both are still giving errors]
@Sura & Krishna, when I changed all variable "con" to set the values (Replace Concorrent__c.Name to con.Name)
I received this error :
Error: Compile Error: Incorrect SObject type: Account should be Concorrent__c at line 1 column 1
then I changed Account to Concorrent__c like said in error
I received this error:
Error: Compile Error: Loop variable must be of type Concorrent__c at line 3 column 17
Keeping changing things keeps giving errors..... :( 

 

Krishna SambarajuKrishna Sambaraju
Are you sure you have added this trigger on the Account object? If it is then the following code should work.
trigger Concorrent on Account (before insert) {
List<Concorrent__c> conList = new List<Concorrent__c>();
    for(Account acc : Trigger.New){
        if(acc.Type == 'Individual'){
            Concorrent__c con = new Concorrent__c();
            con.Name = acc.Name;
            con.Segmento__c = acc.Segmento__c;
            con.Telefone__c = acc.Phone;
            con.Tipo_de__c = acc.Tipo_de_empresa__c;
            conList.add(con);
        }
    }
    if(!conList.IsEmpty()){
        try{
            insert conList;
        } catch(Exception e){
            system.debug('Following Exceptions occured'+e.getmessage());
        }
    }
}
Vincent van Drunen LittelVincent van Drunen Littel
Hi Krishna, 

I did add it to Account object and the trigger saved without errors. The only thing is when I create a new account it does not create a record on  "concorrent" automatically... would you know why?? 

Thank you so much for the help!!!
 
surasura
According to your trigger 
concorrent record is only created if the created account type is 'Individual'
Vincent van Drunen LittelVincent van Drunen Littel

sura, you would suggest me to change it to what? 
 

Thank you!!

surasura
I did not suggest any change , I only said that  , concorrent record is only created  if inseted Accounts type field value is Individual. are you sure that Account record you inserted has the value "individual' in type field
Vincent van Drunen LittelVincent van Drunen Littel

Honestly Sura, I'm new to Salesforce and not familiar with this part of creating triggers etc. 

What i really need here is that when created a new account it automatically creates a record with basic info on concorrent. 
Im not sure what is meant by field value is indiviudual, (sorry) 
 

Would you have a suggention for me on how to change individual to something that would create a new record anytime a account has been created.!?? 

Thank you so much.. .sorry for the somewhat silly questions of mine... :/
 

Krishna SambarajuKrishna Sambaraju
Hi Vincent,

In the trigger just after the for statement, you are checking using an "if" statement (if (acc.Type == 'Individual')), because of this it works only if the Type field on the account is set to "Individual". If you want this trigger to run for every Account, then remove this if condition. See below.


List<Concorrent__c> conList = new List<Concorrent__c>();
    for(Account acc : Trigger.New){
            Concorrent__c con = new Concorrent__c();
            con.Name = acc.Name;
            con.Segmento__c = acc.Segmento__c;
            con.Telefone__c = acc.Phone;
            con.Tipo_de__c = acc.Tipo_de_empresa__c;
            conList.add(con);
    }
    if(!conList.IsEmpty()){
        try{
            insert conList;
        } catch(Exception e){
            system.debug('Following Exceptions occured'+e.getmessage());
        }
    }
}

The above change will create a Concurrent record for every Account created.

Regards,
Krishna.
Vincent van Drunen LittelVincent van Drunen Littel
Hi Krishna, thanks for the explanation and adjust... but unfortunately it is still not working... 
Would you like to see or get more information ? im using my dev org as testing ground so if you want i can provide access and you can verify things there? 

What worries me the most is that this is only the start of what I have to do. this is supposed to be the "easy" part...

I appreciate your help so much.. and once again sorry for the silly questions.. 
Krishna SambarajuKrishna Sambaraju
Hi Vincent,

How did you verify, if the Concorrent records are created or not? Did you create a tab for this Concorrent object?

Can you open the Developer Console and verify using the following SOQL query.

SELECT Id, Name FROM Concorrent__c

The above query should all the Concorrent__c records created so far.

If you want me to check your dev org, you can pass me the credentials, so I can have a look.

Regards,
Krishna.
Vincent van Drunen LittelVincent van Drunen Littel
what is your email? i can send your username and password.. 
 
Krishna SambarajuKrishna Sambaraju
My email id is ksambaraju66@gmail.com. I have a dev org registered with this email id as the username. So use this email address and a different username.
Vincent van Drunen LittelVincent van Drunen Littel
I did create a tab of Concorrent Object Tab, there you can create new records,
these ones are were created in the concorrent tab.. not in accounts
User-added image



 
Krishna SambarajuKrishna Sambaraju
Hi Vincent,

Your custom object Concorrent__c is having a master detail relationship with Account, The custom field Concorrent__c on your custom object is a required field and you need the pass the Id of the Account to this field. You cannot pass the Id field in the before insert trigger, as the id will not be generated at that time. You need to use the after insert trigger and pass the account id to your Concorrent__c field on the Concorrent__c object.

I have changed it to after insert and also set the Concorrent__c to account Id. It is working now. You can check.

Regards,
Krishna.
This was selected as the best answer