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
LooseLipsLooseLips 

Creating Records in APEX and linking them to other Objects

Hi,

 

I've scoured the board and there seems to be plenty of examples of how to create new records in objects based on other submissions but my requirement seems a little more complex.

 

I currently have a mobile app submitting incident tickets into SFDC.

 

At present when a new incident__c record is created an APEX trigger finds the correct account that needs to fix that incident and attachs the account ID onto the ticket through a related field.

 

Then I can go to my account and see a list of related incident tickets that have been created on the account page.

 

So far, so good. What I now need to do is take this a bit further. I have created a new object called App_User__c and I want to do the same. However, the amount of Accounts is fixed, so I already have them created in SFDC. The users aren't known to me until they submit so I would like to 

 

a) Check if the app_user__C already exists based on their email in the incident__C object.

b) If not, create the app_user__C and copy 5 fields from the incident__c to populate the object

c) On creation or successful query, link the App_user__C ID to the incident__C ticket so I can see all incident's linked to my users on their page in SFDC, just like I can with Accounts.

 

Is this one trigger?  Many?

 

Any thoughts gratefully received!

Best Answer chosen by Admin (Salesforce Developers) 
LooseLipsLooseLips

Naomi - that's fantasitic.

 

Oddly it does work with brackets.

 

Ran first time and works like a charm.

 

Thank you so much for helping me so quickly.

 

I've accepted it as the solution.

 

Here's my full working code as it is now - you were right it needed :incident.email__c

 

trigger App_UserIDUpdater on Incident__c (before insert, before update) {

for (incident__C incident : trigger.new)
{
    app_user__c user;
    app_user__C[] queryResult;   
    /*Check if the app_user__C already exists based on their email in the incident__C object. */
    queryResult = ([select id from app_user__C where Email__c = :incident.email__c]);
    
    /*b) If not, create the app_user__C and copy 5 fields from the incident__c to populate the object*/
    if (queryResult.isEmpty())
    {
        user =  new app_user__C(
                                email__c = incident.email__c,
                                firstname__c = incident.firstname__c, 
                                lastname__c = incident.surname__c, 
                                phone__c = incident.phone__c 
                               );
        insert user;
    }
    else
    {
     user = queryResult[0];
    }
   
    /*c) On creation or successful query, link the App_user__C ID to the incident__C ticket so I can see all incident's linked to my users on their page in SFDC, just like I can with Accounts.*/
    incident.App_user__C = user.id;
}

}

 

 

 

All Answers

sfcksfck

You can, and should, do it in a single trigger (BeforeInsert).

 

I've sketched out some code for you here. You might find it works with a little tweaking. However it is not "bulkified". If you think you might have to deal with mass inserts, or if you simply want to follow good practise, I can help you bulkify it. 

 

for (incident__C incident : trigger.new)
{
	app_user__c user;
	app_user__C[] queryResult;
        /*Check if the app_user__C already exists based on their email in the incident__C object. */
	queryResult = ([select id from app_user__C where email = :incident.email]);
	/*b) If not, create the app_user__C and copy 5 fields from the incident__c to populate the object*/
	if (queryResult.isEmpty())
	{
	    user =  new app_user__C(foo = incident.foo, bar = incident.bar);
            insert user;
	}
	else
	{
	    app_user__c user = queryResult[0];
	}
	/*c) On creation or successful query, link the App_user__C ID to the incident__C ticket so I can see all incident's linked to my users on their page in SFDC, just like I can with Accounts.*/
	incident.App_user__C = user.id;
}

 

 

LooseLipsLooseLips

Wow,

 

Thanks for the reply. I'll check that out.

 

Shouldn't be an issue with bulk objects as the tickets come in one at a time and will only ever have one user assigned against them.

 

I'll get back to you if I can get that working.

LooseLipsLooseLips

Ok Naomi - here's where I'm up to.

 

I have gone through the code and I can get it to compile but only if I add in a string for the query result....

 

= email__c

= incident.email__c

= incident__c.email__c

 

....do not compile.

 

Any ideas? I really appreciate this by the way.

 

Here's my code so far with all my small changes for real fields etc...

 

trigger App_UserIDUpdater on Incident__c (before insert, before update) {

for (incident__C incident : trigger.new)
{
    app_user__c user;
    app_user__C[] queryResult;   
    /*Check if the app_user__C already exists based on their email in the incident__C object. */
    queryResult = ([select id from app_user__C where Email__c = email__c]);
    
    /*b) If not, create the app_user__C and copy 5 fields from the incident__c to populate the object*/
    if (queryResult.isEmpty())
    {
        user =  new app_user__C(
                                email__c = incident.email__c,
                                firstname__c = incident.firstname__c, 
                                lastname__c = incident.surname__c, 
                                phone__c = incident.phone__c 
                               );
        insert user;
    }
    else
    {
     user = queryResult[0];
    }
   
    /*c) On creation or successful query, link the App_user__C ID to the incident__C ticket so I can see all incident's linked to my users on their page in SFDC, just like I can with Accounts.*/
    incident.App_user__C = user.id;
}

}

 

sfcksfck

In SOQL if you use a variable you have to put a colon before it. Looks like what you need is

 

:incident.email__c

 

 

 

sfcksfck

Oh, sorry, also there should be no brackets

 

queryResult = [select id from app_user__C where email = :incident.email];

 

LooseLipsLooseLips

Naomi - that's fantasitic.

 

Oddly it does work with brackets.

 

Ran first time and works like a charm.

 

Thank you so much for helping me so quickly.

 

I've accepted it as the solution.

 

Here's my full working code as it is now - you were right it needed :incident.email__c

 

trigger App_UserIDUpdater on Incident__c (before insert, before update) {

for (incident__C incident : trigger.new)
{
    app_user__c user;
    app_user__C[] queryResult;   
    /*Check if the app_user__C already exists based on their email in the incident__C object. */
    queryResult = ([select id from app_user__C where Email__c = :incident.email__c]);
    
    /*b) If not, create the app_user__C and copy 5 fields from the incident__c to populate the object*/
    if (queryResult.isEmpty())
    {
        user =  new app_user__C(
                                email__c = incident.email__c,
                                firstname__c = incident.firstname__c, 
                                lastname__c = incident.surname__c, 
                                phone__c = incident.phone__c 
                               );
        insert user;
    }
    else
    {
     user = queryResult[0];
    }
   
    /*c) On creation or successful query, link the App_user__C ID to the incident__C ticket so I can see all incident's linked to my users on their page in SFDC, just like I can with Accounts.*/
    incident.App_user__C = user.id;
}

}

 

 

 

This was selected as the best answer
TestCommunity1TestCommunity1

Hi,

 

Is it possible to insert rows into an Object before insert into another object using import?

We have a case where Contact object have to have Account name while adding a new contact, but its not possible to add new Contact if the account name is not present in Accounts.

Since, account name is a lookup field and hence can only be taken from among existing rows in Accounts.

 

I wanted to write a trigger before insert into Contacts which checks if Account Name exists in account or not, if not then make an entry into Account with the same name and then proceed to put entry into Contacts.

 

Can someone please tell me if it is possible to achieve using a trigger? Any suggestion on how to do this.

 

Thanks...