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
SalesforceAddictSalesforceAddict 

i have made fields i want to help for apex class code

Opportunity
 Objectstaging__c
Fields:
  1. Individual_or_Organization__c (Text)
  2. Organization_Name__c
  3. First_Name__c
  4. Last_Name__c
  5. Postal_Code__c
  6. Date_Recieved__c (Date)
  7. Amount__c (Currency)
  8. Error_Message__c (Long Text)
  9. Description__c
 
Requirement:
 
Case1.  If  Individual_or_Organization__c==I  ,
  • Search for existing contact with matching FistName , LastName and Postal Code. If contact not found then create new contact.
 
  • If staging record contains value in Organization_Name__c  and Postal_Code__c Fields then search for account with matching name and postal code. If account found then link contact record with this account.
 
  • Create new opportunity record for that contact.
Niraj Kr SinghNiraj Kr Singh
Hi Sachin,

Try this code and if it works for you marked it of your ans to help others.
public class MapStagingToAccountAndContact {
	//Varibales
	
	public MapStagingToAccountAndContact() {
		//Do-Initialization
	}
	
	//Call this method by passing a staging__c record
	public void mapAccountAndContact(staging__c objStaging) {
		List<Opportunity> newOpptyList = new List<Opportunity>();
		
		if(objStaging != null && objStaging.Individual_or_Organization__c == 'I') {
			Account objAccount = searchAccount(objStaging);
			
			if(objAccount != null) {
				List<Contact> linkContactList = searchContact(objAccount, objStaging);
				try{
					if(linkContactList != null && !linkContactList.isEmpty()) {
						upsert linkContactList;
						
						//Create new opportunity;
						for(Contact objCon : linkContactList) {
							newOpptyList.add(new Opportunity(Name = objCon.FullName,
															 StageName = 'Prospecting',
															 AccountId = objAccount.Id,
															 ContactId = objCon.Id
															));							//Add other required fields if needed.
							
						}
						
						if(newOpptyList != null && !newOpptyList.isEmpty()) {
							insert newOpptyList;
						}
					}
				} catch(Exception ex) {
					system.debug('---Exception--' + ex);
				}
			}
		}
	}
	
	private Account searchAccount(staging__c objStaging) {
		if(objStaging.Organization_Name__c != null && objStaging.Postal_Code__c != null) {
			return [SELECT Id, Name FROM Account WHERE Name = objStaging.Organization_Name__c AND BillingPostalCode = objStaging.Postal_Code__c Limit 1];
			//Assuming here, there will be only one account with these combination in your system
		}
	}
	
	private List<Contact> searchContact(Account objAccount, staging__c objStaging) {
		List<Contact> linkContactList = new List<Contact>();
		if(objStaging.First_Name__c != null && objStaging.Last_Name__c != null && objStaging.Postal_Code__c != null) {
			List<Contact> existingContactList = [SELECT Id, FirstName, LastName, MailingPostalCode FROM Contact 
												WHERE FirstName = objStaging.First_Name__c AND LastName = objStaging.Last_Name__c AND MailingPostalCode = objStaging.Postal_Code__c];
			//For existing contacts
			if(existingContactList != null) {
				for(Contact objCon : existingContactList) {
					objCon.AccountId = objAccount.Id;
					linkContactList.add(objCon);
				}
			} else {
				//create new contact
				linkContactList.add(new Contact(FirstName = objStaging.First_Name__c, 
												LastName = objStaging.Last_Name__c,
												MailingPostalCode = objStaging.Postal_Code__c
												AccountId = objAccount.Id));
			}
			
		}
		return linkContactList;
	}
}

Run this script from your developer console to process:
staging__c objStaging = [SELECT Id, Individual_or_Organization__c,Organization_Name__c,First_Name__c,Last_Name__c,Postal_Code__c,Date_Recieved__c,Amount__c,Error_Message__c,Description__c 
						FROM staging__c Limit 1];
MapStagingToAccountAndContact objMapStaging = new MapStagingToAccountAndContact();
objMapStaging.mapAccountAndContact(objStaging);

Let me know if you face any problem.


 
SalesforceAddictSalesforceAddict
bro can you please tell me what type of variable i have to define
SalesforceAddictSalesforceAddict
and what i have to do in constructor
 
Niraj Kr SinghNiraj Kr Singh
No need to do anythings in place of variables and inside constructor. That is the structure only and i have given comment for the future purpose if needed.
Note: You can add fields for Contact/Opportunity according to your requirements at line no: 23 / 62.
You do not have to any things for your case1.

If it solves your problem mark your ans.